使用jedis连接redis-cluster进行布隆过滤器功能的演示

前几篇分别进行了redis的五种基础数据结构的api演示,下面几篇会针对基于redis cluster集群做一些常用的应用场景演示demo。本篇使用纯redis演示布隆过滤(Bloom Filter)器的使用。以及bitmap的一些jedis api.

package com.coderman.jedis.clusterdemo.bloomfilter;

import com.coderman.jedis.clusterdemo.ClusterTest;
import org.junit.Test;

import java.util.BitSet;
import java.util.Random;

/**
 * @Author fanchunshuai
 * @Date 2020/1/7 11
 * @Description:
 * 纯redis实现的bloom过滤器
 */
public class RedisBloomFilterTest extends ClusterTest {
     
    //新年第一天登录
    private String key1 = "loginuser:2020:01:01";
    private String key2 = "loginuser:2020:01:02";
    private String key3 = "loginuser:2020:01:03";

    /**
     * 初始化登录人员数据
     */
    @Test
    public void addBit(){
     
        Random random = new Random();
        for (int i = 0;i< 10000;i++){
     
            //十万位登录用户随机登录
            cluster.setbit(key1,random.nextInt(100000),true);
            cluster.setbit(key2,random.nextInt(100000),true);
            cluster.setbit(key3,random.nextInt(100000),true);
        }
        System.out.println("随机数据生成完成");
    }


    /**
     * 获取登录用户数
     */
    @Test
    public void getUniqueCount(){
     
        BitSet loginUser = BitSet.valueOf(cluster.get(key1.getBytes()));
        long count1 = loginUser.cardinality();
        System.out.println("count1 = "+count1);
        BitSet loginUser2 = BitSet.valueOf(cluster.get(key2.getBytes()));
        long count2 = loginUser2.cardinality();
        System.out.println("count2 = "+count2);

        BitSet loginUser3 = BitSet.valueOf(cluster.get(key3.getBytes()));
        long count3 = loginUser3.cardinality();
        System.out.println("count3 = "+count3);
    }


    /**
     * 获取三天内所有登录过的用户数
     */
    @Test
    public void getAllLoginCount(){
     
        BitSet loginUser = BitSet.valueOf(cluster.get(key1.getBytes()));
        BitSet loginUser2 = BitSet.valueOf(cluster.get(key2.getBytes()));
        BitSet loginUser3 = BitSet.valueOf(cluster.get(key3.getBytes()));
        //并集运算
        loginUser3.or(loginUser2);
        loginUser3.or(loginUser);
        long count3 = loginUser3.cardinality();
        System.out.println("count3 = "+count3);
    }

    /**
     * 获取三天内每天都登录过的用户数
     */
    @Test
    public void getEveryLoginCount(){
     
        BitSet loginUser = BitSet.valueOf(cluster.get(key1.getBytes()));
        BitSet loginUser2 = BitSet.valueOf(cluster.get(key2.getBytes()));
        BitSet loginUser3 = BitSet.valueOf(cluster.get(key3.getBytes()));
        //交集运算
        loginUser3.and(loginUser2);
        loginUser3.and(loginUser);
        long count3 = loginUser3.cardinality();
        System.out.println("count3 = "+count3);
    }

    @Test
    public void testGetIsExit(){
     
        for (long i = 0;i < 10000;i++) {
     
            //判断10000位用户是否存在
            boolean b =  cluster.getbit(key2,i);
            if(b){
     
                System.out.println("login userid = "+i);
            }
        }
    }
}

关于布隆过滤器有很多博客,这里只是简单使用redis 的bitmap的api进行演示,后续如果想要在项目上使用,需要考虑一些性能还有高可用等问题。

你可能感兴趣的:(NoSql专题,redis,redis,java)