Huawei Cloud Yaoyun Server L instance evaluation | Installing BloomFilter in the Redis Docker container & using the Redis plug-in version of the Bloom filter in Spring

Insert image description here

Preface

Recently, Huawei Cloud Yaoyun Server L instance was released, and I also built one to play with. During the process, I encountered various problems and learned a lot about operation and maintenance in the process of solving the problems.

This blog introduces how to install BloomFilter in the Redis docker container and combine it with spring.

The list of other related Huawei Cloud Yaoyun Server L instance evaluation articles is as follows:

lead out


1. Install BloomFilter in the Redis Docker container;
2. Use the Redis plug-in version of the Bloom filter in Spring;
Insert image description here

1. Install BloomFilter in Redis

1.Official website reference documents

https://redis.io/resources/modules/

Insert image description here

https://redis.io/docs/data-types/probabilistic/bloom-filter/

https://github.com/RedisBloom/RedisBloom

Insert image description here

2.Clone official website code

apt-get install -y git

Insert image description here

git clone --recursive https://github.com/RedisBloom/RedisBloom.git

Insert image description here

3. Install cmake

sudo apt install cmake

Insert image description here

Error reported, you need to install cmake

Insert image description here

sudo apt install cmake

Insert image description here

4. Compile as a whole

make command to compile

Insert image description here

Compiled so file

Insert image description here

5. Copy the so file and configure it

docker cp redisbloom.so redis_6379:/usr/local/etc/redis

Insert image description here

loadmodule /root/Redis/RedisBloom/bin/linux-x64-release/redisbloom.so

Insert image description here

View log

Insert image description here

6. Preliminary use

docker exec -it redis_6379 bash

Insert image description here

root@706d04b2ea4d:/data# redis-cli
127.0.0.1:6379> auth XXX
OK
127.0.0.1:6379> BF.ADD bmFilter tom123
(integer) 1
127.0.0.1:6379> BF.exists bmFilter tom123
(integer) 1
127.0.0.1:6379> BF.exists bmFilter tom124
(integer) 0
127.0.0.1:6379> 

Insert image description here

Other commands

Insert image description here

Command Description
BF.ADD Add an element to the bloom filter
BF.EXISTS Determine whether the element will be in the bloom filter
BF.INFO Returns information about bloom filters
BF.INSERT Add multiple elements to the filter. If the key does not exist, it creates a new filter.
BF.MADD Add multiple elements to bloom filter
BF.MEXISTS Determine whether multiple elements will be included in the Bloom filter
BF.RESERVE Create a bloom filter. Set false positive rate and capacity
BF.SCANDUMP Start incrementally saving Bloom filters.
BF.LOADCHUNK Restore bloom filters previously saved using BF.SCANDUMP.

2. Combined with lua script and used in spring

Insert image description here

1. How to write Lua scripts

127.0.0.1:6379> BF.ADD bmFilter tom123
-- lua脚本
local key1 = KEYS[1]      --key是从1开始
local argv1 = ARGV[1]      --第一个值
-- 使用redis的命令 BF.ADD
local retVal = redis.call('BF.ADD',key1,argv1)
-- 将结果返回
return retVal
    @Bean
    public RedisScript<Long> bloomFilter(){
    
    
        DefaultRedisScript redisScript = new DefaultRedisScript<>();
        redisScript.setResultType(Long.class);
        // lua脚本的位置
        redisScript.setLocation(
                new ClassPathResource("/lua/bloom-demo.lua") // 关联lua脚本
        );
        return redisScript;
    }
package com.tianju.fresh.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class BloomFilterTest {
    
    

    @Resource
    private RedisScript bloomFilter;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void testLuaBloom(){
    
    
        List<String> keys = new ArrayList<>();
        keys.add("testFilter"); // redis的bloom的key
        Object result = stringRedisTemplate.opsForValue().getOperations()
                .execute(bloomFilter, keys, "pet365");
        System.out.println("Lua脚本返回结果:"+result);
    }
}

Insert image description here

2. Ways without using lua scripts

Insert image description here

package com.tianju.fresh.service;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.script.DefaultRedisScript;
import org.springframework.data.redis.core.script.RedisScript;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;

import static java.util.stream.Collectors.toList;

@SpringBootTest
@RunWith(SpringJUnit4ClassRunner.class)
public class BloomFilterTest {
    
    

    @Resource
    private RedisScript bloomFilter;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Test
    public void testLuaBloom2(){
    
    
        List<String> keys = new ArrayList<>();
        keys.add("helloFilter"); // redis的key
        String script = "return redis.call('BF.ADD',KEYS[1],ARGV[1])";
        Long result = stringRedisTemplate.opsForValue()
                .getOperations()
                .execute(
                        new DefaultRedisScript<>(script, Long.class),
                        keys,
                        "apple"
                );
        System.out.println("无lua脚本:"+result);
    }

    public static void main(String[] args) {
    
    
        System.out.println(Stream.of("abcd").collect(toList()));
    }
}

Insert image description here


Summarize

1. Install BloomFilter in the Redis Docker container;
2. Use the Redis plug-in version of the Bloom filter in Spring;

Guess you like

Origin blog.csdn.net/Pireley/article/details/133268762