Spring中使用Redis之基数(HyperLogLog)类型

一、Redis基数(HyperLogLog)简介
基数是一种算法,基数并表示存储元素,存储元素小号内存空间比较大,而是某一个有重复元素的数据的集合(一般是很大的数据集合)评估所需要的空间单元数,所以它没有办法进行存储。举个例子,一本英文著作由数百万个单词组成你的内存却不足以存储它们,那么我们分析一下业务。英文单词本身数有限的,在这本书中的几百万个单词中有许多重复的单词,除去重复的单词,这本书也就是几千到一万多个单词而已,那么内存就足够去存储它们。Redis对基数数据结构对支持数从版本2.8.9开始的。

二、基数(HyperLogLog)的基本命令
【1】pfadd key element,功能:添加指定元素到HyperLogLog中,如果已经存储了该元素,则返回0,添加失败。
【2】pfcount key,功能:返回HyperLogLog的基数值。
【3】pfmerge desKey key1 [key2 …],功能:合并多个HyperLogLog,并且将其保存在desKey中。

三、Spring中使用Redis操作基数(HyperLogLog)类型

步骤一:创建Maven项目,在pom.xml文件中导入一下三个依赖。

     <dependencies>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-nop</artifactId>
            <version>1.7.2</version>
        </dependency>
        <!-- 导入Spring中的redis依赖 -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-redis</artifactId>
            <version>2.1.6.RELEASE</version>
        </dependency>
        <!-- 导入jedis依赖 -->
        <dependency>
            <groupId>redis.clients</groupId>
            <artifactId>jedis</artifactId>
            <version>2.9.1</version>
        </dependency>
    </dependencies>


步骤二:在applicationContext.xml文件中配置Redis。

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd">

    <!-- Jedis 的连接池配置 -->
    <bean id="poolConfig" class="redis.clients.jedis.JedisPoolConfig">
        <!-- 最大空闲连接数量-->
        <property name="maxIdle" value="50"/>
        <!-- 最大连接数量-->
        <property name="maxTotal" value="100"/>
        <!-- 最大等待时间-->
        <property name="maxWaitMillis" value="20000"/>
    </bean>

    <bean id="stringRedisSerializer" class="org.springframework.data.redis.serializer.StringRedisSerializer"/>

    <!-- Redis服务中心-->
    <bean id="connectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory">
        <property name="hostName" value="localhost"/>
        <property name="port" value="6379"/>
        <property name="poolConfig" ref="poolConfig"/>
    </bean>

    <!-- Redis模板类-->
    <bean id="redisTemplate" class="org.springframework.data.redis.core.RedisTemplate">
        <property name="connectionFactory" ref="connectionFactory"/>
        <property name="defaultSerializer" ref="stringRedisSerializer"/>
        <property name="valueSerializer" ref="stringRedisSerializer"/>
    </bean>
</beans>

步骤三:创建一个RedisHyperLogLogDemo类,使用Spring测试Redis哈希操作,如下所示:

package demo;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.data.redis.core.RedisTemplate;

/**
 * @author czd
 */
public class RedisHyperLogLogDemo {
    public static void main(String[] args) {
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");
        RedisTemplate redisTemplate = applicationContext.getBean(RedisTemplate.class);

        String key = "HyperLogLog";

        //给基数添加yuan's元素,相当于pfadd key element
        redisTemplate.opsForHyperLogLog().add(key, "a", "b", "c");
        redisTemplate.opsForHyperLogLog().add("hyperLogLog", "d", "e", "f");

        //返回HyperLogLog的基数值,相当于pfcount key
        Long size = redisTemplate.opsForHyperLogLog().size(key);
        System.out.println("HyperLogLog的基数值: " + size);

        //获取两个基数集合的并集,相当于pfmerge desKey key1 [key2 …]
        redisTemplate.opsForHyperLogLog().union("desHyperLogLog",key,"hyperLogLog");
    }
}

  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值