Redis其他數(shù)據(jù)類型
打開Xshell之后坞笙,如何打開6379筝尾?
[root@iZ2ze5s090y2d3q50krshoZ ~]# redis-cli -a 密碼
1痰驱、bitmap 位圖
help @string
GETBIT key offset
含義:對(duì) key 所儲(chǔ)存的字符串值哀卫,獲取指定偏移量上的位(bit)澜沟。
summary: Returns the bit value at offset in the string value stored at key
since: 2.2.0
舉例:
# 對(duì)不存在的 key 或者不存在的 offset 進(jìn)行 GETBIT, 返回 0
127.0.0.1:6379> EXISTS bit
(integer) 0
127.0.0.1:6379> GETBIT bit 113
(integer) 0
# 對(duì)已存在的 offset 進(jìn)行 GETBIT
127.0.0.1:6379> SETBIT bit 113 1
(integer) 0
127.0.0.1:6379> GETBIT bit 113
(integer) 1
SETBIT key offset value
summary: Sets or clears the bit at offset in the string value stored at key
since: 2.2.0
含義:對(duì) key 所儲(chǔ)存的字符串值晋控,設(shè)置或清除指定偏移量上的位(bit)汞窗。
舉例:
redis> SETBIT bit 10086 1
(integer) 0
redis> GETBIT bit 10086
(integer) 1
redis> GETBIT bit 100 # bit 默認(rèn)被初始化為 0
(integer) 0
BITFIELD key [GET type offset] [SET type offset value] [INCRBY type offset increment] [OVERFLOW WRAP|SAT|FAIL]
summary: Perform arbitrary bitfield integer operations on strings
since: 3.2.0
含義:對(duì)字符串執(zhí)行任意位于位域整數(shù)操作
BITCOUNT key [start end]
summary: Count set bits in a string
since: 2.6.0
含義:得到總數(shù)
舉例:
127.0.0.1:6379> SETBIT mykeys 10 1
(integer) 0
127.0.0.1:6379> SETBIT mykeys 11 1
(integer) 0
127.0.0.1:6379> SETBIT mykeys 21 1
(integer) 0
127.0.0.1:6379> SETBIT mykeys 10 2
(integer) 0
127.0.0.1:6379> BITCOUNT mykeys
(integer) 4
BITOP operation destkey key [key ...]
summary: Perform bitwise operations between strings
since: 2.6.0
含義:在字符串之間執(zhí)行位操作
公司記錄某個(gè)人一個(gè)月的打卡情況
一年365天怎么記錄一個(gè)人在某個(gè)網(wǎng)站的登錄,活躍情況
兩個(gè)狀態(tài)的都能用這種方法進(jìn)行性能壓榨
都是操作二進(jìn)制位來進(jìn)行記錄赡译,就只有0 和 1 兩個(gè)狀態(tài)仲吏!
365 天 = 365 bit 1字節(jié) = 8bit 46 個(gè)字節(jié)左右!
測(cè)試登錄次數(shù)
127.0.0.1:6379> setbit login 0 1
(integer) 0
127.0.0.1:6379> setbit login 3 1
(integer) 0
127.0.0.1:6379> bitcount login 0 -1
(integer) 2
周一:1 周二:0 周三:0 周四:1 ......
查看某一天是否有打卡!
統(tǒng)計(jì)操作裹唆,統(tǒng)計(jì) 打卡的天數(shù)誓斥!
2、Geospatial 地理位置
127.0.0.1:6379> help @geo
GEOADD key longitude latitude member [longitude latitude member ...]
summary: Add one or more geospatial items in the geospatial index represented using a sorted set
since: 3.2.0
GEODIST key member1 member2 [unit]
summary: Returns the distance between two members of a geospatial index
since: 3.2.0
GEOHASH key member [member ...]
summary: Returns members of a geospatial index as standard geohash strings
since: 3.2.0
GEOPOS key member [member ...]
summary: Returns longitude and latitude of members of a geospatial index
since: 3.2.0
/**初學(xué)者知道就行//
GEORADIUS key longitude latitude radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
summary: Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a point
since: 3.2.0
含義:查詢標(biāo)識(shí)地理空間索引的排序集许帐,以獲取與某一點(diǎn)的最大距離的成員
GEORADIUSBYMEMBER key member radius m|km|ft|mi [WITHCOORD] [WITHDIST] [WITHHASH] [COUNT count] [ASC|DESC] [STORE key] [STOREDIST key]
summary: Query a sorted set representing a geospatial index to fetch members matching a given maximum distance from a member
since: 3.2.0
朋友的定位劳坑,附近的人,打車距離計(jì)算成畦?
在Redis3.2 版本推出了Geospatial 距芬! 這個(gè)功能可以推算地理位置的信息,兩地之間的距離循帐。
可以查詢一些測(cè)試數(shù)據(jù):http://www.jsons.cn/lngcodeinfo/0706D99C19A781A3/
給定幾個(gè)城市的經(jīng)緯度:
太原:112.549248框仔,37.857014
北京:116.405285,39.904989
成都:104.065735拄养,30.659462
武漢:114.298572离斩,30.584355
西安: 108.948024,34.263161
洛陽(yáng):112.434468瘪匿,34.663041
getadd
# getadd 添加地理位置
# 規(guī)則:兩級(jí)無法直接添加跛梗,我們一般會(huì)下載城市數(shù)據(jù),直接通過java程序一次性導(dǎo)入棋弥!
# 有效的經(jīng)度從-180度到180度茄袖。
# 有效的緯度從-85.05112878度到85.05112878度。
# 當(dāng)坐標(biāo)位置超出上述指定范圍時(shí)嘁锯,該命令將會(huì)返回一個(gè)錯(cuò)誤。
# 127.0.0.1:6379> geoadd china:city 39.90 116.40 beijing
(error) ERR invalid longitude,latitude pair 39.900000,116.400000
# 參數(shù) key 值()
#插入城市的經(jīng)緯度
127.0.0.1:6379> geoadd china:city 112.549284 37.857014 taiyuan
127.0.0.1:6379> geoadd china:city 116.405285 39.904989 beijing
127.0.0.1:6379> geoadd china:city 104.065735 30.659462 chengdu
127.0.0.1:6379> geoadd china:city 114.298572 30.584355 wuhan
127.0.0.1:6379> geoadd china:city 108.948024 34.263161 xian
127.0.0.1:6379> geoadd china:city 112.434468 34.663041 luoyang
# 獲取指定的城市的經(jīng)度和緯度聂薪!
127.0.0.1:6379> geopos china:city beijing
1) 1) "116.40528291463851929"
2) "39.9049884229125027"
127.0.0.1:6379> geopos china:city beijing luoyang
1) 1) "116.40528291463851929"
2) "39.9049884229125027"
2) 1) "112.43446558713912964"
2) "34.6630405862934694"
#兩人之間的距離家乘!
#單位:
#m 表示單位為米。
#km 表示單位為千米藏澳。
#mi 表示單位為英里仁锯。
#ft 表示單位為英尺。
#georadius 以給定的經(jīng)緯度為中心翔悠, 找出某一半徑內(nèi)的元素
#我附近的人业崖? (獲得所有附近的人的地址,定位P畛睢)通過半徑來查詢双炕!
#獲得指定數(shù)量的人,200
#所有數(shù)據(jù)應(yīng)該都錄入:china:city 撮抓,才會(huì)讓結(jié)果更加請(qǐng)求妇斤!
#查詢太原到北京的距離
127.0.0.1:6379> geodist china:city beijing taiyuan
"404109.2735"
127.0.0.1:6379> geodist china:city beijing taiyuan km
"404.1093"
#找某個(gè)地點(diǎn)(微信定位)方圓1000km內(nèi)的城市,附近的朋友
127.0.0.1:6379> georadius china:city 110 32 500 km
1) "xian"
2) "luoyang"
3) "wuhan"
# 找出位于指定元素周圍的其他元素!
127.0.0.1:6379> GEORADIUSBYMEMBER china:city beijing 1000 km
1) "taiyuan"
2) "beijing"
3) "xian"
4) "luoyang"
# 查看地圖中全部的元素
127.0.0.1:6379> ZRANGE china:city 0 -1
1) "chengdu"
2) "xian"
3) "luoyang"
4) "wuhan"
5) "taiyuan"
6) "beijing"
# 移除指定元素站超!
127.0.0.1:6379> zrem china:city beijing
(integer) 1
3荸恕、 Hyperloglog(基數(shù))
推薦一個(gè)博客:
關(guān)于這個(gè)算法的一個(gè)博客:http://www.reibang.com/p/55defda6dcd2
場(chǎng)景如網(wǎng)頁(yè)的 UV (一個(gè)人訪問一個(gè)網(wǎng)站多次,但是還是算作一個(gè)人K老唷)
通過使用一種特殊的算法融求,而達(dá)到使用一個(gè)固定空間達(dá)到統(tǒng)計(jì)基數(shù)的作用。
這種算法有誤差算撮,是一種概率學(xué)問題生宛。0.81% 錯(cuò)誤率! 但對(duì)于統(tǒng)計(jì)UV這類的任務(wù)钮惠,是可以忽略不計(jì)的茅糜!
測(cè)試使用
如果允許有誤差,那么一定可以使用 Hyperloglog 素挽!
如果不允許誤差蔑赘,就使用 set 或者自己的數(shù)據(jù)類型即可!
127.0.0.1:6379> help @hyperloglog
PFADD key element [element ...]
summary: Adds the specified elements to the specified HyperLogLog.
since: 2.8.9
PFCOUNT key [key ...]
summary: Return the approximated cardinality of the set(s) observed by the HyperLogLog at key(s).
since: 2.8.9
PFMERGE destkey sourcekey [sourcekey ...]
summary: Merge N different HyperLogLogs into a single one.
since: 2.8.9
127.0.0.1:6379> pfadd pf a b c d e f g
(integer) 1
127.0.0.1:6379> pfadd pf2 c d e f g
(integer) 1
127.0.0.1:6379> pfcount pf pf2
(integer) 7
127.0.0.1:6379> pfmerge a pf pf2
OK