- 目前仅redis4.0以上版本支持
安装插件
git clone https://github.com/RedisBloom/RedisBloom.git
cd RedisBloom
make //编译 会生成一个rebloom.so文件
redis-server --loadmodule /path/to/rebloom.so
redis-cli -h 127.0.0.1 -p 6379
添加依赖,目前redis的jar包不支持布隆过滤器命令
<dependency>
<groupId>com.redislabs</groupId>
<artifactId>jrebloom</artifactId>
<version>1.0.1</version>
</dependency>
代码示例
private void addTobloomFilter(String userid, String[] hashs) {
Client client = null;
try {
// 布隆过滤器去重
Calendar cal = Calendar.getInstance();
int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
client = new Client("192.168.17.49", 6379, 100000, 1000);
String bloomKey = "rebloom:" + userid + "_" + dayOfYear;
client.addMulti(bloomKey, hashs);
} catch (Exception e) {
e.printStackTrace();
} finally {
client.close();
}
}
private boolean[] bloomFilter(String userid, String[] hashs) {
boolean[] exists = new boolean[hashs.length];
Client client = null;
try {
// 布隆过滤器去重
Calendar cal = Calendar.getInstance();
int dayOfYear = cal.get(Calendar.DAY_OF_YEAR);
client = new Client("192.168.17.49", 6379, 100000, 1000);
String bloomKey = "rebloom:" + userid;
try {
// 新建过滤器 没找到判断过滤器是否存在的接口
// 参数分别为(过滤器名称,预期数据量,容错率)
client.createFilter(bloomKey + "_" + dayOfYear, 1000000, 0.0001);
} catch (Exception e) {
// System.out.println(e.getMessage());
}
for (int k : getKeys()) {//7天内有任意一天存在则为存在
boolean[] e = client.existsMulti(bloomKey + "_" + k, hashs);
Stream.iterate(0, n -> n + 1)
.limit(hashs.length).forEach(index -> {
exists[index] = exists[index] || e[index];
});
}
} catch (Exception e) {
e.printStackTrace();
} finally {
client.close();
}
LOGGER.info("rebloom-hash是否被去重:");
LOGGER.info(JSON.toJSONString(hashs));
LOGGER.info(JSON.toJSONString(exists));
return exists;
}
布隆过滤器特点:
预期数据量越大占用内存越大,容错率越低占用内存越大
判断结果为不存在那肯定是不存在,判断结果为存在其实可能不存在
不支持删除元素