最近有碰到布隆过滤器,发现redis本身没有实现它。如果使用需要再安装这个模。
有兴趣的可以自己学习下
先看下介绍
The RedisBloom module provides four data structures: a scalable Bloom filter, a cuckoo filter, a count-min sketch, and a top-k. These data structures trade perfect accuracy for extreme memory efficiency, so they're especially useful for big data and streaming applications.
Bloom and cuckoo filters are used to determine, with a high degree of certainty, whether an element is a member of a set.
A count-min sketch is generally used to determine the frequency of events in a stream. You can query the count-min sketch get an estimate of the frequency of any given event.
A top-k maintains a list of k most frequently seen items.
简单意思就是:
RedisBloom模块提供了四种数据结构:可伸缩的Bloom filter、cuckoo filter、count-min sketch和top-k。这些数据结构以完美的准确性换取了极高的内存效率,因此它们对于大数据和流媒体应用程序特别有用。
Bloom和cuckoo过滤器用于高度精确地确定一个元素是否属于一个集合的成员。
count-min sketch 通常用于确定流中事件的频率。您可以查询count-min sketch,以获得对任何给定事件的频率的估计。
top-k维护一个包含k个最常见项的列表。
简单来说就是,利用少量内存完成大量数据处理。
下面逐个了解下
Bloom filter
使用场景
判断一个元素在不在一个集合中,我们可以用hashset,复杂度O(1)即可判断。
但是当数据量很大时,需要的存储很大。
可以考虑使用bit位来判断,但是又会有hash冲突的问题。为了降低冲突率,可以采用多个hash 来实现。
基本原理
Bloom filter 本质是一个bit数组。每个元素都只占用 1 bit,每个元素只能是 0 或者 1。
对于一个key,利用 k 个hash 函数进行映射得到 n1, n2, n3。然后标记数组下标 n1, n2, n3 的位置为 1。
因为存在哈希冲突,即某个bit位已经因为其他key变成1。因此当一个key 映射的所有位置都已经是1,此时无法判断当前key是否存在。当然,如果一个key 映射的所有位置不全是1, 则说明当前key 不存在。即所谓的假正
例如,如果 "hello" 的映射为{1,4,9},此时发现这三个位置都是1,则不一定能准确判断出 "hello" 存在集合中。