当前位置:网站首页>redis探索之布隆过滤器
redis探索之布隆过滤器
2022-06-26 05:17:00 【青铜大神】
什么是布隆过滤器?
布隆过滤器(Bloom Filter)是1970年由布隆提出的。他对判断海量元素是否存在的问题进行了研究,也就是到底需要多大的位图和多少个哈希函数发表了一篇论文,提出的这个容器就叫做布隆过滤器。
优点:
相比其他的数据结构,布隆过滤器在空间和时间方面都有巨大的优势。布隆过滤器的插入、查询时间复杂度都是O(N)。
布隆过滤器不需要存储元素本身,所以保密性有保证。
缺点:
随着存入元素数量的增加,误算率随之增加。
很难删除其中元素。
布隆过滤器的使用
我使用的是guava的布隆过滤器,先学习学习怎么用。
public class BloomFilterTest {
/** 预计插入的数据 */
private static Integer expectedInsertions = 10000000;
/** 误判率 */
private static Double fpp = 0.001;
/** 布隆过滤器 */
private static BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), expectedInsertions, fpp);
public static void main(String[] args) {
// 插入 1千万数据
for (int i = 0; i < expectedInsertions; i++) {
bloomFilter.put(i);
}
// 用1千万数据测试误判率
int count = 0;
for (int i = expectedInsertions; i < expectedInsertions * 2; i++) {
if (bloomFilter.mightContain(i)) {
count++;
}
}
System.out.println("一共误判了:" + count);
System.out.println("误判率:" + (100.0 * count / expectedInsertions) + "%");
}
}结果

实现原理
我们都知道hashMap,其实布隆过滤器跟hashMap有一定的相似性。

但是hashMap的hash碰撞几率太大了,我们就得优化。怎么优化呢,如果觉得家里不安全,我们就给门加锁,不怕麻烦就多加几把锁,减少别人的钥匙能开我家门的可能性。

这样是不是碰撞的几率就小了。校验是否存在时,也是把多把钥匙拿出来,试一试能不能开这个门。
源码分析
源码中有四个私有变量
// 存储数据映射的数组/bitmap:长度根据预估数据量和误判率计算,其中误判率与数组大小成反比。
private final LockFreeBitArray bits;
// 执行hash算法的次数:根据bits数组长度和预估数据量计算,与误判率成反比。
private final int numHashFunctions;
// 数据类型
private final Funnel<? super T> funnel;
// hash算法
private final BloomFilter.Strategy strategy;put的流程,经过numHashFunctions次hash,每次都去寻找存放位置,看是否有效,有效就对应累加。
mightContain的流程,经过numHashFunctions次hash,每次都去寻找存放位置,看是否有效,经过numHashFunctions次hash如果每次都有效就证明存在。
public <T> boolean put(@ParametricNullness T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies.LockFreeBitArray bits) {
long bitSize = bits.bitSize();
byte[] bytes = Hashing.murmur3_128().hashObject(object, funnel).getBytesInternal();
long hash1 = this.lowerEight(bytes);
long hash2 = this.upperEight(bytes);
boolean bitsChanged = false;
long combinedHash = hash1;
// 经过numHashFunctions次hash
for(int i = 0; i < numHashFunctions; ++i) {
bitsChanged |= bits.set((combinedHash & 9223372036854775807L) % bitSize);
combinedHash += hash2;
}
return bitsChanged;
}
public <T> boolean mightContain(@ParametricNullness T object, Funnel<? super T> funnel, int numHashFunctions, BloomFilterStrategies.LockFreeBitArray bits) {
long bitSize = bits.bitSize();
byte[] bytes = Hashing.murmur3_128().hashObject(object, funnel).getBytesInternal();
long hash1 = this.lowerEight(bytes);
long hash2 = this.upperEight(bytes);
long combinedHash = hash1;
for(int i = 0; i < numHashFunctions; ++i) {
// 如果bitmap中没有对应值则认为没有该值,返回false
if (!bits.get((combinedHash & 9223372036854775807L) % bitSize)) {
return false;
}
combinedHash += hash2;
}
return true;
}实际应用场景
(1) 典中典就是缓存穿透。
(2) 垃圾邮件的过滤,通过把垃圾邮件地址加入布隆过滤器,当布隆过滤器判定地址存在于垃圾地址列表,再去进行校验。
边栏推荐
- ThreadPoolExecutor implements file uploading and batch inserting data
- 红队得分方法统计
- Secondary bootloader about boot28 Precautions for ASM application, 28035
- Modify the case of the string title(), upper(), lower()
- How does P2P technology reduce the bandwidth of live video by 75%?
- C# 39. string类型和byte[]类型相互转换(实测)
- Transport layer TCP protocol and UDP protocol
- Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
- skimage. morphology. medial_ axis
- As promised: Mars, the mobile terminal IM network layer cross platform component library used by wechat, has been officially open source
猜你喜欢

递归遍历目录结构和树状展现

SOFA Weekly | 开源人—于雨、本周 QA、本周 Contributor

The first gift of the project, the flying oar contract!

cartographer_local_trajectory_builder_2d

LeetCode 19. 删除链表的倒数第 N 个结点

How to select the data transmission format of instant messaging application

apktool 工具使用文档

Zuul 實現動態路由

cartographer_fast_correlative_scan_matcher_2d分支定界粗匹配

Learn from small samples and run to the sea of stars
随机推荐
[greedy college] recommended system engineer training plan
第九章 设置结构化日志记录(一)
app 应用安装到手机,不显示图标,引发的思考
skimage.morphology.medial_axis
Generalized linear model (logistic regression, Poisson regression)
Day3 data type and Operator jobs
递归遍历目录结构和树状展现
vscode config
Implementation of IM message delivery guarantee mechanism (II): ensure reliable delivery of offline messages
Schematic diagram of UWB ultra high precision positioning system
The localstorage browser stores locally to limit the number of forms submitted when tourists do not log in.
skimage. morphology. medial_ axis
apktool 工具使用文档
Tp5.0 framework PDO connection MySQL error: too many connections solution
6.1 - 6.2 Introduction à la cryptographie à clé publique
【红队】要想加入红队,需要做好哪些准备?
6.1 - 6.2 公钥密码学简介
Zuul 實現動態路由
Technical past: tcp/ip protocol that has changed the world (precious pictures, caution for mobile phones)
[latex] error type summary (hold the change)