geospatial地理位置
朋友的定位,附近的人,打车距离计算
getadd
getadd :添加地理位置
#规则:两极无法直接添加,我们一般会直接下载城市数据,直接通过Java程序一次性导入‘
#有效的精度从-180到180
#有效的纬度从-85.05112878度到85.05112878度
###############################################
127.0.0.1:6379> geoadd china:city 107.02 33.07 hanzhong
(integer) 1
127.0.0.1:6379> geoadd china:city 108.96 34.28 xi'an
Invalid argument(s)
127.0.0.1:6379> geoadd china:city 108.96 34.28 xian
(integer) 1
127.0.0.1:6379> geoadd china:city 121,44 31.21 shanghai 114.10 22.54 shenzhen
(error) ERR value is not a valid float
127.0.0.1:6379> geoadd china:city 121.44 31.21 shanghai 114.10 22.54 shenzhen
(integer) 2
127.0.0.1:6379> geoadd china;city 116.40 39.90 beijing
(integer) 1
getpos:获得当前定位,一定是一个坐标值
geodist:两个位置之间的距离
指定单位的参数 unit 必须是以下单位的其中一个:
- m 表示单位为米。
- km 表示单位为千米。
- mi 表示单位为英里。
- ft 表示单位为英尺。
127.0.0.1:6379> geopos china:city hanzhong
1) 1) "107.01999753713607788"
2) "33.0699987543028513"
#########################################################
127.0.0.1:6379> geodist china:city beijing hanzhong
"1130478.4787"
127.0.0.1:6379> geodist china:city beijing hanzhong km
"1130.4785"
127.0.0.1:6379> geodist china:city beijing shanghai
"1068255.5464"
127.0.0.1:6379> geodist china:city beijing shanghai km
"1068.2555"
georadi us 💛以给定的经纬度为中心,找出某一半径内的元素
我附近的人,(获得所有附近的人的地址,定位!)通过半径来查询
127.0.0.1:6379> georadius china:city 110 30 1000 km
1) "hanzhong"
2) "xian"
3) "shenzhen"
127.0.0.1:6379> georadius china:city 110 30 100 km
(empty array)
127.0.0.1:6379> georadius china:city 110 30 500 km
1) "hanzhong"
2) "xian"
127.0.0.1:6379> georadius china:city 110 30 500 km withdist
1) 1) "hanzhong"
2) "443.1275"
2) 1) "xian"
2) "486.0106"
#########################################################
127.0.0.1:6379> georadius china:city 110 30 500 km withcoord
1) 1) "hanzhong"
2) 1) "107.01999753713607788"
2) "33.0699987543028513"
2) 1) "xian"
2) 1) "108.96000176668167114"
2) "34.28000112885764139"
##########################################################
127.0.0.1:6379> georadius china:city 110 30 1000 km withdist withcoord count 2
1) 1) "hanzhong"
2) "443.1275"
3) 1) "107.01999753713607788"
2) "33.0699987543028513"
2) 1) "xian"
2) "486.0106"
3) 1) "108.96000176668167114"
2) "34.28000112885764139"
georadiusbymember :😋以成员为中心,指定半径查询,类似georadius,
127.0.0.1:6379> georadiusbymember china:city beijing 1000 km
1) "beijing"
2) "xian"
geohash 🎃将返回一个或多个位置元素的geohash表示
该命令返回11个字符的geohash字符串!!
127.0.0.1:6379> GEOHASH china:city beijing hanzhong shenzhen
1) "wx4fbxxfke0"
2) "wmuh5k1wdw0"
3) "ws10hrm1480"
#将二维经纬度转换为一维字符串,字符串越接近距离越近
geospatial底层是Zset,所以可以用Zset的命令操作geo
127.0.0.1:6379> zrange china:city 0 -1
1) "hanzhong"
2) "xian"
3) "shenzhen"
4) "shanghai"
5) "beijing"
127.0.0.1:6379> zrem china:city beijing
(integer) 1
127.0.0.1:6379> zrange china:city 0 -1
1) "hanzhong"
2) "xian"
3) "shenzhen"
4) "shanghai"
Hyperloglog
Redis HyperLogLog 是用来做基数统计的算法,HyperLogLog 的优点是,在输入元素的数量或者体积非常非常大时,计算基数所需的空间总是固定的、并且是很小的。
花费 12 KB 内存,就可以计算接近 2^64 个不同元素的基数。
因为 HyperLogLog 只会根据输入元素来计算基数,而不会储存输入元素本身,所以 HyperLogLog 不能像集合那样,返回输入的各个元素。
其底层使用string数据类型
什么是基数?
数据集中不重复的元素的个数。
127.0.0.1:6379> pfadd mykey a b c d e f g h i j#创建第一组mykey
(integer) 1
127.0.0.1:6379> pfcount #统计mykey元素的基数数量
(error) ERR wrong number of arguments for 'pfcount' command
127.0.0.1:6379> pfcount mykey
(integer) 10
127.0.0.1:6379> pfadd mykey2 i j z x c v b n m #创建第二组元素mykey2
(integer) 1
127.0.0.1:6379> pfcount mykey2
(integer) 9
127.0.0.1:6379> pfmerge mykey3 mykey mykey2#合并两组mykey mykey2=>mykey3 并集
OK
127.0.0.1:6379> pfcount mykey3 #看并集的数量
(integer) 15
应用场景:
网页的访问量(UV):一个用户多次访问,也只能算作一个人。
传统实现,存储用户的id,然后每次进行比较。当用户变多之后这种方式及其浪费空间,而我们的目的只是计数,Hyperloglog就能帮助我们利用最小的空间完成。
Bitmap
位存储
统计用户信息,活跃,不活跃!登录,未登录!打卡,365打卡!两个状态的都可以使用bitmaps!
Bitmap 位图,数据结构!都是操作二进制位来进行记录,就只有0和1 两个状态!
365天=365bit 1 字节=8bit 46个字节左右!
127.0.0.1:6379> setbit sign 0 1
(integer) 0
127.0.0.1:6379> setbit sign 1 0
(integer) 0
127.0.0.1:6379> setbit sign 2 1
(integer) 0
127.0.0.1:6379> setbit sign 3 1
(integer) 0
127.0.0.1:6379> setbit sign 4 0
(integer) 0
127.0.0.1:6379> setbit sign 5 1
(integer) 0
127.0.0.1:6379> setbit sign 6 1
(integer) 0
查看某一天是否打卡
127.0.0.1:6379> getbit sign 3
(integer) 1
127.0.0.1:6379> getbit sign 5
(integer) 1
查看打卡天数
127.0.0.1:6379> bitcount sign
(integer) 5