zoukankan      html  css  js  c++  java
  • redis的三个特殊数据结构(geospatial、bitmaps、hyperloglogs)

    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) "xe5xb9xbfxe4xb8x9c"
    2) "xe6x9dxadxe5xb7x9e"
    3) "xe4xb8x8axe6xb5xb7"
    4) "xe5x8cx97xe4xbaxac"
    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:获取元素的 hash 值
    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:给定一个坐标点,查找附近指定距离范围内的元素,相当于附近的人
    语法: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:与georadius功能相同,不同的是georadiusbymember的中心点是geo中的成员而不是经纬度
    语法: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)

    1、使用bitmaps记录周一到周日的打卡(周一至周日用0-6标识)

     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

    pfadd:添加元素
    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%左右的误差。






  • 相关阅读:
    webService 的使用
    前端框架——树形结构Ztree的使用
    vue使用问题总结(长期更新)
    yum安装配置MySQL数据库
    kworkerds挖矿木马
    zabbix 中文乱码
    GIT 仓库的搭建
    ELK 收集交换机日志(以华为交换机为例)
    Zabbix 邮箱告警(Python脚本)
    Tomcat8性能优化
  • 原文地址:https://www.cnblogs.com/zouhong/p/13764282.html
Copyright © 2011-2022 走看看