Redis的三种特殊数据类型

文章目录

 1.Geospatial地理位置

 2.Bitmap 位图,数据结构

3.HyperLogLog 基数统计

1、Geospatial地理位置详解

package com.tianwang.springboot_porpdd.fiveRedisType.specialType;

import redis.clients.jedis.GeoCoordinate;
import redis.clients.jedis.GeoRadiusResponse;
import redis.clients.jedis.GeoUnit;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.params.geo.GeoRadiusParam;

import java.util.HashMap;
import java.util.Map;

/**
 * @author WangYan
 * @date 2021/9/17 11:49
 * redis 特殊类型
 * Geospatial 地理位置
 */
public class Geospatial {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost", 6379);

        /**
         * geoadd 添加地理位置
         * 规则 : 两极无法直接添加,一般都会下载城市数据,直接通过java程序一次性导入!
         */
        // 方式一: 参数: key Map<String,GeoCoordinate>  GeoCoordinate 有参构造 : 经度、纬度
        Map<String, GeoCoordinate> var2 = new HashMap<>();
        var2.put("上海市",new GeoCoordinate(121.472644,31.231706));
        var2.put("北京市",new GeoCoordinate(116.405285,39.904989));
        jedis.geoadd("myMap",var2);

        // 方式二:  参数: key,经度、纬度,名称(城市名)
        jedis.geoadd("myMap",120.153576,30.287459,"杭州市");
        jedis.geoadd("myMap",113.665412,34.757975,"郑州市");

        /***
         * geopos
         * 获取地理位置
         */
        // [(121.47264629602432,31.23170490709807)]
        jedis.geopos("myMap", "上海市");
        //[(121.47264629602432,31.23170490709807), (116.40528291463852,39.9049884229125)]
        jedis.geopos("myMap", "上海市","北京市");

        /**
         *
         * geodist
         * 返回两个给定位置之间的距离。
         *
         * 指定单位的参数 unit 必须是以下单位的其中一个:
         * m 表示单位为米。
         * km 表示单位为千米。
         * mi 表示单位为英里。
         * ft 表示单位为英尺。
         *
         * 如果两个位置之间的其中一个不存在, 那么命令返回空值。
         */
        jedis.geodist("myMap", "上海市", "郑州市",GeoUnit.KM);

        /**
         * geohash
         * 返回一个或多个位置元素的 Geohash 表示
         * 将二维的经纬度转换为一维字符串,如果两个字符串越像,那么则越接近!
         */
        jedis.geohash("myMap","上海市", "郑州市");

        /**
         * georadius
         * 以给定的经纬度为中心, 返回键包含的位置元素当中, 与中心的距离不超过给定最大距离的所有位置元素。
         * 以 精度 110 ,维度 30为中心 找 10000km 内的集合元素位置
         */
        for (GeoRadiusResponse myMap : jedis.georadius("myMap", 110, 30, 10000, GeoUnit.KM)) {
            // 打印出来的结果集 : 杭州市 上海市 郑州市 北京市
            myMap.getMemberByString();
        }

        // count(2) : 指定数量 2
        for (GeoRadiusResponse myMap : jedis.georadius("myMap", 110, 30, 10000, GeoUnit.KM, GeoRadiusParam.geoRadiusParam().count(2))) {
            // 打印出来的结果集 : 郑州市 杭州市
            myMap.getMemberByString();
        }

        // withCoord : 显示经度纬度
        for (GeoRadiusResponse myMap : jedis.georadius("myMap", 110, 30, 10000, GeoUnit.KM, GeoRadiusParam.geoRadiusParam().withCoord())) {
            // (120.15357345342636,30.287457907215327)
            //(121.47264629602432,31.23170490709807)
            //(113.66541177034378,34.757976032595344)
            //(116.40528291463852,39.9049884229125)
            System.out.println(myMap.getCoordinate());
        }

        /**
         * georadiusByMember
         * 找出指定元素周围的其他元素1!
         * 参数 : radius 距离  GeoUnit : 单位(M, KM, MI, FT;)
         */
        for (GeoRadiusResponse response : jedis.georadiusByMember("myMap", "郑州市", 100, GeoUnit.KM)) {
            // 郑州市
            System.err.println(response.getMemberByString());
        }
    }
}

2、Hyperloglog基数统计

package com.tianwang.springboot_porpdd.fiveRedisType.specialType;

import redis.clients.jedis.Jedis;

/**
 * @author WangYan
 * @date 2021/9/18 14:59
 * redis 特殊类型
 * HyperLogLog 基数(不重复的元素 !)统计    容错率 : 0.81% !
 * 应用场景 : 统计UV 、 页面访问量
 * 优点:占用内存是固定的 !非常小!
 */
public class HyperLogLog {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost", 6379);


        /**
         * pfadd
         * 正常存取
         */
        jedis.pfadd("PFA1","A","B","C","D","E","F");

        /**
         * pfcount
         * 获取数量
         */
        jedis.pfcount("PFA1");

        /**
         * pfmerge
         * 将两个集合合并成一个新的集合,不重复元素 !
         */
        jedis.pfadd("PFA2","F","G");
        jedis.pfmerge("PFA3", "PFA1", "PFA2");
        jedis.pfcount("PFA3");
    }
}

3、Bitmap位图场景详解

package com.tianwang.springboot_porpdd.fiveRedisType.specialType;

import redis.clients.jedis.Jedis;

/**
 * @author WangYan
 * @date 2021/9/18 15:49
 * redis 特殊类型
 * Bitmap 位图,数据结构 ! 都是操作二进制位来进行记录,就只有 0 和 1两个状态 !
 * 应用场景 : 1.统计用户信息分为: 活跃、不活跃 !! 2.钉钉打卡!!
 */
public class Bitmap {
    public static void main(String[] args) {
        Jedis jedis = new Jedis("localhost", 6379);

        /**
         * 模拟: 周一到周五  打卡情况
         * 1 打卡  0 未打卡
         * 正常存储
         */
        jedis.setbit("MyWeekend", Long.parseLong("1"),"0");
        jedis.setbit("MyWeekend", Long.parseLong("2"),"1");
        jedis.setbit("MyWeekend", Long.parseLong("3"),"1");
        jedis.setbit("MyWeekend", Long.parseLong("4"),"0");
        jedis.setbit("MyWeekend", Long.parseLong("5"),"0");

        // 查看周三打卡情况
        jedis.getbit("MyWeekend", Long.parseLong("1"));

        // 统计打卡天数
        jedis.bitcount("MyWeekend");
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

逸航不吃瓜

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值