geospatial
應用:存儲位置信息,可以很方便計算和管理位置信息
redis的geospatial在redis 3.2版本就推出來了!這個功能可以推算地理位置的信息,兩地之間的距離,方圓幾里的人
可以查詢一些測試數據:http://www.jsons.cn/lngcode/
geospatial只有如下6個命令:
geoadd:添加地理位置
規則:兩極無法添加,我們一般會下載城市數據,直接通過java程序一次性導入。
(flushdb 用於清空當前數據庫中所有的key、dbsize 返回當前庫的key數量 info查看版本、內核信息
clear)
geoadd:添加一個或多個位置到一個key中
語法:geoadd key longitude latitude member [longitude latitude member ...]
localhost:6379> geoadd China:city 116.40 39.9 北京 # 添加一個位置 (integer) 1 localhost:6379> geoadd China:city 121.47 31.23 上海 120.15 30.28 杭州 113.27 23.13 廣東 # 添加多個位置 (integer) 3 localhost:6379> zrange China:city 0 -1 # 查看用zrange,因為geo本質是一個有序集合 1) "\xe5\xb9\xbf\xe4\xb8\x9c" 2) "\xe6\x9d\xad\xe5\xb7\x9e" 3) "\xe4\xb8\x8a\xe6\xb5\xb7" 4) "\xe5\x8c\x97\xe4\xba\xac" localhost:6379> exit [root@VM_0_2_centos bin]# redis-cli -h localhost -p 6379 --raw # --raw 解決中文亂碼問題 localhost:6379> zrange China:city 0 -1 廣東 杭州 上海 北京 localhost:6379> zrange China:city 0 -1 withscores 廣東 4046533759716104 杭州 4054134257390783 上海 4054803462927619 北京 4069885360207904
geopos:查看某個key成員的地理位置
語法:geopos key member [member ...]
localhost:6379> geopos China:city 北京 116.39999896287918091 39.90000009167092543 localhost:6379> geopos China:city 北京 上海 116.39999896287918091 39.90000009167092543 121.47000163793563843 31.22999903975783553
geodist:計算兩點之間的距離,可以選擇計算單位,默認單位是m
語法:geodist key member1 member2 [m|km|ft|mi]
localhost:6379> geodist China:city 北京 上海 1067378.7564 localhost:6379> geodist China:city 北京 上海 m 1067378.7564 localhost:6379> geodist China:city 北京 上海 km 1067.3788
geohash 可以獲取元素的經緯度編碼字符串,上面已經提到,它是 base32 編碼。 你可
以使用這個編碼值去 http://geohash.org/${hash}中進行直接定位,它是 geohash 的標准編碼。該命令將返回11個字符的geohansh字符串
值.
語法:geohash key member [member ...]
localhost:6379> geohash China:city 上海 wtw3sj5zbj0
獲取hash碼,請求鏈接http://geohash.org/wtw3sj5zbj0,結果如下

語法:georadius key longitude latitude radius m|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
先查找安徽的坐標:117.25 , 31.83
localhost:6379> georadius China:city 117.25 31.83 500 km # 查看方圓500km內的城市 杭州 上海 localhost:6379> georadius China:city 117.25 31.83 500 km withcoord # withcoord 結果帶上坐標 杭州 120.15000075101852417 30.2800007575645509 上海 121.47000163793563843 31.22999903975783553 localhost:6379> georadius China:city 117.25 31.83 500 km withdist # withdist 帶上距離 杭州 325.6740 上海 405.5792 localhost:6379> georadius China:city 117.25 31.83 500 km withdist count 1 asc # 由近到遠取一個 杭州 325.6740
語法:georadiusbymember key longitude latitude radius m|km|ft|mi [withcoord] [withdist] [withhash] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
localhost:6379> georadiusbymember China:city 上海 500 km withdist 杭州 164.5694 上海 0.0000
zrem:應為geo的本質是zset,所以刪除也是用zrem
localhost:6379> zrange China:city 0 -1 杭州 上海 北京 localhost:6379> zrem China:city 北京 1 localhost:6379> zrange China:city 0 -1 杭州 上海
zrange key start stop 返回有序集中指定區間內的成員 (下標參數 start 和 stop 都以 0 為底,也就是說,以 0 表示有序集第一個成員,以 1 表示有序集第二個成員,以此類推。
你也可以使用負數下標,以 -1 表示最后一個成員, -2 表示倒數第二個成員,以此類推。 )
bitmaps
Redis提供的Bitmaps這個“數據結構”可以實現對位的操作。Bitmaps本身不是一種數據結構,實際上就是字符串,但是它可以對字符串的位進行操作。
可以把Bitmaps想象成一個以位為單位數組,數組中的每個單元只能存0或者1,數組的下標在bitmaps中叫做偏移量。單個bitmaps的最大長度是512MB,即2^32個比特位。
setbit:設置值
語法:setbit key offset value
理解:key是數組,offset是數組的下標,value是這個下標位置的狀態值(0或1)

getbit:獲取key中一個下標的狀態
語法:getbit key offset
2、查看某一天是否打卡

bitcount:統計一個key中狀態為1的下標數量
語法:bitcount key [start end]
3、查看一周簽到打卡的數量
bitop:用來做bitmaps之間的運算
語法:bitop operation destkey key [key ...]
operation:表明操作類型,有四個可選值
- and:與
- or:或
- not:非
- xor:異或
destkey:目標key,運算的結果會存到這里
key [key ...] 被運算的一個或多個bitmaps的key

bitpos:計算Bitmaps中第一個值為targetBit的偏移量:
語法:bitpos key bit [start] [end]
localhost:6379> bitpos sign 1 # sing中第一個狀態為1的下標 0 localhost:6379> bitpos sign 0 # sing中第一個狀態為0的下標 2
小結:我們在生活中,或編程中,只要只有兩個標志位(0 1)我們都可以考慮使用bitmaps來進行操作,這 樣的話,十分方便,還有就是能夠極大的節約內存!
hyperloglogs
pfcount:統計基數
pfmerge: 合並多個數據集
localhost:6379> pfadd mykey a b c c c c c c d e 1 localhost:6379> pfadd mykey2 e f e d f c d s f d d d d d d 1 localhost:6379> pfcount mykey 5 localhost:6379> pfcount mykey2 5 localhost:6379> pfcount mykey mykey2 7 localhost:6379> pfmerge mergekey mykey mykey2 # 合並mykey和mykey2寫入到mergekey OK localhost:6379> pfcount mergekey 7
在需要對大數據進行不重復數統計時推薦用hyperloglogs,但是如果需要精確統計的話就不要使用,因為hyperloglogs存在1%左右的誤差。