三种特殊数据类型
Geospatial(地理位置)
- 有效的经度从-180度到180度。
- 有效的纬度从-85.05112878度到85.05112878度。
geoadd 添加城市的地理位置坐标
- 可以一次性添加多个
geoadd key longitud(经度) latitude(纬度) member(城市名) ...
复制代码
geopos 获取城市的地理位置的坐标。
- 可以一次性获取多个
geopos key member(城市名) ...
复制代码
geodist 计算两个位置之间的距离。
- m :米,默认单位。
- km :千米。
- mi :英里。
- ft :英尺。
geodist key member1 member2 km/m/mi/ft(单位)
复制代码
georadius 根据经纬度坐标来获取指定范围内的地理位置集合。
georadius key longitud(经度) latitude(纬度) km/m/mi/ft(单位)
复制代码
georadiusbymember 根据储存在位置集合里面的某个地点获取指定范围内的地理位置集合。
georadiusbymember key member(城市名) km/m/mi/ft(单位)
复制代码
geohash 返回一个或多个位置对象的 geohash 值。
georadiusbymember key member1 member2....
复制代码
测试用例
127.0.0.1:6379> geoadd china:city 88.31104 43.36378 wulumuqi #添加乌鲁木齐的地理位置
(integer) 1
127.0.0.1:6379> geoadd china:city 116.23128 40.22077 beijing #添加北京的地理位置
(integer) 1
127.0.0.1:6379> geoadd china:city 106.54041 29.40268 chongqing #添加重庆的地理位置
(integer) 1
127.0.0.1:6379> geoadd china:city 126.95717 45.54774 haerbin #添加哈尔滨的地理位置
(integer) 1
127.0.0.1:6379> geoadd china:city 117.20767 39.077969 tianjin #添加天津的地理位置
(integer) 1
127.0.0.1:6379> geopos china:city haerbin #得到哈尔滨的地理位置
1) 1) "126.95716828107833862"
2) "45.54773904285200103"
127.0.0.1:6379> geopos china:city haerbin chongqing #得到哈尔滨 重庆的地理位置
1) 1) "126.95716828107833862"
2) "45.54773904285200103"
2) 1) "106.54040783643722534"
2) "29.40268053517299762"
127.0.0.1:6379> geodist china:city haerbin chongqing #得到哈尔滨到重庆的距离,单位为m
"2529857.4392"
127.0.0.1:6379> geodist china:city haerbin chongqing km #得到哈尔滨到重庆的距离,单位为km
"2529.8574"
127.0.0.1:6379> georadius china:city 110 30 30 km #得到经纬度为(110,30)30km内的城市
(empty array)
127.0.0.1:6379> georadius china:city 110 30 100 km #得到经纬度为(110,30)50km内的城市
(empty array)
127.0.0.1:6379> georadius china:city 110 30 500 km #得到经纬度为(110,30)500km内的城市
1) "chongqing"
127.0.0.1:6379> georadius china:city 110 30 500 km withcoord #得到经纬度为(110,30)500km内的城市,并返回它的经纬度
1) 1) "chongqing"
2) 1) "106.54040783643722534"
2) "29.40268053517299762"
127.0.0.1:6379> georadius china:city 110 30 500 km withcoord withdist #得到经纬度为(110,30)500km内的城市,并返回它的经纬度以及和(110,30)之间的距离
1) 1) "chongqing"
2) "340.7667"
3) 1) "106.54040783643722534"
2) "29.40268053517299762"
127.0.0.1:6379> georadiusbymember china:city beijing 500 km withdist withcoord #得到 北京500km范围内的城市,并给出距离和经纬度
1) 1) "tianjin"
2) "152.1447"
3) 1) "117.20767110586166382"
2) "39.07796974192802253"
2) 1) "beijing"
2) "0.0000"
3) 1) "116.23128265142440796"
2) "40.22076905438526495"
127.0.0.1:6379> geohash china:city wulumuqi chongqing tianjin #得到 乌鲁木齐 重庆 天津的地理位置的geohash值
1) "tzwy9w4hk60"
2) "wm5z22s7520"
3) "wwgq71uj8t0"
复制代码
Hyperloglog
简介
- redis用来做基数统计的数据结构算法
- 只占用12KB的内存
- 允许统计误差
- 基数:A,B两个集合中不同的数的总个数
pfadd 创建集合的元素
pfadd key value1 value2 ....
复制代码
pfcount 统计集合中的基数数量
pfcount key
复制代码
pfmerge 合并集合
# 合并key1 key2 .. 到 key
pfmerge key key1 key2 ....
复制代码
测试用例
127.0.0.1:6379> pfadd hykey1 a b c d e f g h #创建第一个集合
(integer) 1
127.0.0.1:6379> pfcount hykey1 #统计第一个集合的基数
(integer) 8
127.0.0.1:6379> pfadd hykey2 g h i j k l m n o p q #创建第二个集合
(integer) 1
127.0.0.1:6379> pfcount hykey2 #统计第二个集合的基数
(integer) 11
127.0.0.1:6379> pfmerge hykey hykey1 hykey2 #合并hykey1 hykey2 到 hykey
OK
127.0.0.1:6379> pfcount hykey #统计合并后的集合的基数
(integer) 17
127.0.0.1:6379>
复制代码
Bitmap
简介
- 位图,进行位存储
- 存储的只有两种状态,比如说 晚点签到,就只有签了和没签这两种情况 (0 1)
setbit 存储数据
setbit key member 0/1
复制代码
getbit 读取数据的状态
getbit key member
复制代码
bitcount 统计数据(状态为1的数据)
bitcount key 0/1
复制代码
测试用例
127.0.0.1:6379> setbit bp1 1 1 #存储数据
(integer) 0
127.0.0.1:6379> setbit bp1 2 1
(integer) 0
127.0.0.1:6379> setbit bp1 3 0
(integer) 0
127.0.0.1:6379> setbit bp1 4 1
(integer) 0
127.0.0.1:6379> setbit bp1 5 1
(integer) 0
127.0.0.1:6379> setbit bp1 6 0
(integer) 0
127.0.0.1:6379> setbit bp1 7 0
(integer) 0
127.0.0.1:6379> getbit bp1 5 #读取数据的状态
(integer) 1
127.0.0.1:6379> getbit bp1 6
(integer) 0
127.0.0.1:6379> bitcount bp1 #读取数据的状态为1的数量
(integer) 4
复制代码
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END