在开发或者面试过程当中,时常遇到过海量数据须要查询,秒杀时缓存击穿怎么避免等等这样的问题呢?掌握好本篇介绍的知识点将有助于你在以后的工做、面试中策马奔腾。java
Bloom Filter,即传说中的布隆过滤器。它其实是一个很长的二进制向量和一系列随机映射函数。布隆过滤器能够用于检索一个元素是否在一个集合中。它的优势是空间效率和查询时间都远远超过通常的算法,缺点是有必定的误识别率和删除困难。面试
布隆过滤器的原理是,<font color="red">当一个元素被加入集合时,经过K个散列函数将这个元素映射成一个位数组中的K个点,把它们置为1。检索时,咱们只要看看这些点是否是都是1就(大约)知道集合中有没有它了:若是这些点有任何一个0,则被检元素必定不在;若是都是1,则被检元素极可能在。</font>这就是布隆过滤器的基本思想。算法
Bloom Filter跟单哈希函数Bit-Map不一样之处在于:Bloom Filter使用了k个哈希函数,每一个字符串跟k个bit对应。从而下降了冲突的几率。
Bloom Filter在避免缓存击穿中的应用方法:简而言之就是先把咱们数据库的数据都加载到咱们的过滤器中,好比数据库的id如今有:1,2,3...,n,以上面的原理图为例,将id全部值 通过三次hash以后,将hash获得的结果对应的地方由0修改成1。这样作以后,每次请求过来经过id查询数据,若是缓存没有命中,再在过滤器中查询,经过一样的hash算法将请求的id值进行运算,得到三个索引值,若是有任何一个对应索引的值为0,说明MySQL中也不存在该id,则直接报错返回。
<font color="#E96900">试想一想这样作的好处是什么?假设这样的一种场景,若是有1000个参数非法请求同时访问(所谓参数非法是指数据库也不存在这类的值,好比id全为负值),缓存中都没有命中,此时若是这1000个请求同时打到DB,数据库层是扛不住的,因此此时Bloom Filter就显得十分必要。</font>数据库
Bloom Filter之因此能作到在时间和空间上的效率比较高,是由于牺牲了判断的准确率、删除的便利性数组
## Bloom Filter 实现
在实现Bloom Filter时,绕不过的两点就是hash函数的选取以及bit数组的大小。
对于一个肯定的场景,咱们预估要存的数据量为n,指望的误判率为fpp,而后须要计算咱们须要的Bit数组的大小m,以及hash函数的个数k,并选择hash函数。
1 Bit数组大小选择
根据预估数据量n以及误判率fpp,bit数组大小的m的计算方式:
2 哈希函数选择
由预估数据量n以及bit数组长度m,能够获得一个hash函数的个数k:
3 应用测试
本篇采用的是Google的Bloom Filter,首先须要引入jar包:缓存
<dependency> <groupId>com.google.guava</groupId> <artifactId>guava</artifactId> <version>23.0</version> </dependency>
测试分两步:函数
一、往过滤器中放五千万个数,而后去验证这五千万个数是否能顺利经过过滤器;工具
二、另外找一万个不在过滤器中的数,检查Bloom Filter误判的概率。测试
import com.google.common.hash.BloomFilter; import com.google.common.hash.Funnels; /** * @author Carson Chu * @date 2020/3/15 14:48 * @description 布隆过滤器测试样例 */ public class BloomFilterTest { private static int capacity = 50000000; private static BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), capacity); // private static BloomFilter<Integer> bf = BloomFilter.create(Funnels.integerFunnel(), total, 0.001); public static void main(String[] args) { // 初始化50000000条数据到过滤器中 for (int i = 0; i < capacity; i++) { bf.put(i); } // 匹配已在过滤器中的值,是否有匹配不上的 for (int i = 0; i < capacity; i++) { if (!bf.mightContain(i)) { System.out.println("有坏人逃脱了~~~"); } } // 匹配不在过滤器中的10000个值,有多少匹配出来 int count = 0; for (int i = capacity; i < capacity + 10000; i++) { if (bf.mightContain(i)) { count++; } } System.out.println("误命中的数量:" + count); } }
运行结果表示,遍历这五千万个在过滤器中的数时,都被识别出来了。一万个不在过滤器中的数,误伤了297个,误判率是2.9%左右。
若是想要下降误判率该怎么作呢,不要急,源码为咱们提供了这一机制:google
@CheckReturnValue public static <T> BloomFilter<T> create(Funnel<? super T> funnel, int expectedInsertions) { return create(funnel, (long)expectedInsertions); } @CheckReturnValue public static <T> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions) { return create(funnel, expectedInsertions, 0.03D); } @CheckReturnValue public static <T> BloomFilter<T> create(Funnel<? super T> funnel, int expectedInsertions, double fpp) { return create(funnel, (long)expectedInsertions, fpp); } @CheckReturnValue public static <T> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions, double fpp) { return create(funnel, expectedInsertions, fpp, BloomFilterStrategies.MURMUR128_MITZ_64); } /* create()方法的最底层实现 */ @VisibleForTesting static <T> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions, double fpp, BloomFilter.Strategy strategy) { Preconditions.checkNotNull(funnel); Preconditions.checkArgument(expectedInsertions >= 0L, "Expected insertions (%s) must be >= 0", new Object[]{expectedInsertions}); Preconditions.checkArgument(fpp > 0.0D, "False positive probability (%s) must be > 0.0", new Object[]{fpp}); Preconditions.checkArgument(fpp < 1.0D, "False positive probability (%s) must be < 1.0", new Object[]{fpp}); Preconditions.checkNotNull(strategy); if (expectedInsertions == 0L) { expectedInsertions = 1L; } long numBits = optimalNumOfBits(expectedInsertions, fpp); int numHashFunctions = optimalNumOfHashFunctions(expectedInsertions, numBits); try { return new BloomFilter(new BitArray(numBits), numHashFunctions, funnel, strategy); } catch (IllegalArgumentException var10) { throw new IllegalArgumentException("Could not create BloomFilter of " + numBits + " bits", var10); } }
BloomFilter一共四个create方法,不过最终都是走向第四个。看一下每一个参数的含义:funnel
:数据类型(通常是调用Funnels
工具类中的)expectedInsertions
:指望插入的值的个数fpp
:错误率(默认值为0.03)strategy
:Bloom Filter的算法策略
错误率越大,所需空间和时间越小,错误率越小,所需空间和时间约大。
布隆过滤器主要是在解决缓存穿透问题的时候引出来的,了解他的原理并能实习运用,在开发和面试中都是大有裨益的。