欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

hyperloglog应用

程序员文章站 2022-07-11 12:23:22
...

1.hyperloglog是应用在基数计算上。因为它不去存储数据,而是计算存储key值的数量,在 Redis 里面,每个 HyperLogLog 键只需要花费 12 KB 内存,就可以计算接近 2^64 个不同元素的基 数。这和计算基数时,元素越多耗费内存就越多的集合形成鲜明对比。

2.hyperloglog基本redis语句

http://www.runoob.com/redis/redis-hyperloglog.html

3.基于redisTemplate操作

package com.panku.web.redis; 
import java.util.Collection; 
import java.util.Set;import javax.annotation.Resource;import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.Springframework.dao.DataAccessException; 
import org.springframework.data.redis.connection.DataType; 
import org.springframework.data.redis.connection.RedisConnection; 
import org.springframework.data.redis.core.HyperLogLogOperations; 
import org.springframework.data.redis.core.RedisCallback; 
import org.springframework.data.redis.core.RedisTemplate; 
import org.springframework.data.redis.core.ZSetOperations.TypedTuple; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 
/** 
 * Spring RedisTemplate操作-HyperLogLog操作 
 * @author ccx 
 * 
 */ 
@RunWith(SpringJUnit4ClassRunner.class)   
@ContextConfiguration(locations = "classpath:applicationContext.xml")   
public class RedisTemplateHyperLogLog { 
    @Resource(name="redisTemplate") 
    private RedisTemplate rt; 
     
    public void flushdb(){ 
        rt.execute(new RedisCallback () {
            public String doInRedis(RedisConnection connection) throws DataAccessException {
                connection.flushDb();
                return "ok";
            }
        });
    }
    @Test
    public void set(){
        flushdb();
        HyperLogLogOperations vo = rt.opsForHyperLogLog(); 
        vo.add("book", "a","b","c"); 
        vo.add("bag", "a","e","d"); 
        vo.add("del", "f","g","h"); 
         
         
         
    } 
    @Test 
    public void get(){ 
        HyperLogLogOperations vo = rt.opsForHyperLogLog(); 
        out(vo.size("book")); 
        out(vo.size("bag")); 
        out(vo.size("del")); 
        out(vo.size("book","bag","del")); 
        vo.delete("del"); 
        out(vo.size("book","bag","del")); 
        vo.union("total", "book","bag","del"); 
        out(vo.size("total")); 
    } 
     
    public void out(String str){ 
        System.out.println(str); 
    } 
    public void out(Collection list){ 
        System.out.println(list); 
    } 
    public void out(long num){ 
        System.out.println(num); 
    } 
    public void out(boolean num){ 
        System.out.println(num); 
    } 
    public void out(DataType num){ 
        System.out.println(num); 
    } 
    public void out(Object str){ 
        System.out.println(str); 
    } 
    public void out(Set > set){ 
        for(TypedTuple t : set){ 
            System.out.println(t.getValue()+"---"+t.getScore()); 
        } 
    } 
} 

https://www.aliyun.com/jiaocheng/775199.html?spm=5176.100033.2.5.htLH4i


相关标签: hyperloglog应用