栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

redis中HyperLogLog的使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

redis中HyperLogLog的使用

redis中HyperLogLog的使用
  • 1.pv和uv的概念?
  • 2.使用HyperLogLog统计个数
    • 2.1 实现原理
    • 2.2 pom.xml
    • 2.3 JedisUtils
    • 2.4 测试模仿统计网站的用户个数
  • 2.pfmerge命令

1.pv和uv的概念?

pv即为page view,代表网页的浏览次数。uv即为user view,代表用户个数

2.使用HyperLogLog统计个数 2.1 实现原理

使用pfadd来添加数据,类似于sadd,会自动去重!使用pfcount来统计个数,类似于scard

2.2 pom.xml


    4.0.0

    org.example
    distribute-lock
    1.0-SNAPSHOT

    
        
        
            com.fasterxml.jackson.core
            jackson-databind
            2.12.3
        
        
        
               redis.clients
               jedis
               3.2.0
               jar
               compile
        
    


2.3 JedisUtils
package com.yl;

import org.apache.commons.pool2.impl.GenericObjectPoolConfig;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

public class JedisUtils {
    private static JedisPool jedisPool = null;

    public static Jedis getJedisObject() {
        if (jedisPool == null) {
            GenericObjectPoolConfig config = new GenericObjectPoolConfig();
            //最大空闲数
            config.setMaxIdle(400);
            //最大连接数
            config.setMaxTotal(2000);
            //连接最大等待时间,-1代表没有限制
            config.setMaxWaitMillis(300000);
            
            jedisPool = new JedisPool(config,"192.168.244.129",6379,30000,"root123");
        }
        try {
            //通过连接池获取jedis对象
            Jedis jedis = jedisPool.getResource();
            jedis.auth("root123");
            return jedis;
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
}

2.4 测试模仿统计网站的用户个数
package com.yl;

import redis.clients.jedis.Jedis;

public class HyperLogLogTest {
    public static void main(String[] args) {
        Jedis jedis = JedisUtils.getJedisObject();
        for (int i = 1; i <= 10000;i++) {
            jedis.pfadd("uv","uv"+i);
        }
        long pfcount = jedis.pfcount("uv");
        System.out.println(pfcount);
    }
}

结果:理论上是有10000个的,但结果只显示9859,是有误差的,但是统计uv对精确度要求不高,可以接受!

2.pfmerge命令

1.作用:合并结果集

pfmerge uv1 uv2

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/309641.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号