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:
Article directory
lead out
1. Install BloomFilter in the Redis Docker container;
2. Use the Redis plug-in version of the Bloom filter in Spring;
1. Install BloomFilter in Redis
1.Official website reference documents
https://redis.io/resources/modules/
https://redis.io/docs/data-types/probabilistic/bloom-filter/
https://github.com/RedisBloom/RedisBloom
2.Clone official website code
apt-get install -y git
git clone --recursive https://github.com/RedisBloom/RedisBloom.git
3. Install cmake
sudo apt install cmake
Error reported, you need to install cmake
sudo apt install cmake
4. Compile as a whole
make command to compile
Compiled so file
5. Copy the so file and configure it
docker cp redisbloom.so redis_6379:/usr/local/etc/redis
loadmodule /root/Redis/RedisBloom/bin/linux-x64-release/redisbloom.so
View log
6. Preliminary use
docker exec -it redis_6379 bash
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>
Other commands
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
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);
}
}
2. Ways without using lua scripts
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()));
}
}
Summarize
1. Install BloomFilter in the Redis Docker container;
2. Use the Redis plug-in version of the Bloom filter in Spring;