• 布隆过滤器的使用场景



    利用布隆过滤器减少磁盘 IO 或者网络请求,因为一旦一个值必定不存在的话,就可以直接结束查询,比如以下场景:

    • 大数据去重;
    • 网页爬虫对 URL 的去重,避免爬取相同的 URL 地址;
    • 反垃圾邮件,从数十亿个垃圾邮件列表中判断某邮箱是否垃圾邮箱;
    • 缓存击穿,将已存在的缓存放到布隆中,当黑客访问不存在的缓存时迅速返回避免缓存及数据库挂掉。
       

    使用GUAVA实现布隆过滤器

    1. <dependency>
    2. <groupId>com.google.guava</groupId>
    3. <artifactId>guava</artifactId>
    4. <version>29.0-jre</version>
    5. </dependency>
    1. /**
    2. * Guava版布隆过滤器
    3. *
    4. */
    5. public class BloomFilterTest {
    6. /**
    7. * @param expectedInsertions 预期插入值
    8. * 这个值的设置相当重要,如果设置的过小很容易导致饱和而导致误报率急剧上升,如果设置的过大,也会对内存造成浪费,所以要根据实际情况来定
    9. * @param fpp 误差率,例如:0.001,表示误差率为0.1%
    10. * @return 返回true,表示可能存在,返回false一定不存在
    11. */
    12. public static boolean isExist(int expectedInsertions, double fpp) {
    13. // 创建布隆过滤器对象
    14. BloomFilter<Integer> filter = BloomFilter.create(Funnels.integerFunnel(), 500, 0.01);
    15. // 判断指定元素是否存在
    16. System.out.println(filter.mightContain(10));
    17. // 将元素添加进布隆过滤器
    18. filter.put(10);
    19. // 再判断指定元素是否存在
    20. System.out.println(filter.mightContain(10));
    21. return filter.mightContain(10);
    22. }
    23. public static void main(String[] args) {
    24. boolean exist = isExist(100000000, 0.001);
    25. }
    26. }

    1. public class BloomFilterTest1 {
    2. /**
    3. * 100万
    4. */
    5. public static final int INSERTIONS = 1000000;
    6. public static void main(String[] args) {
    7. // 初始化一个存储string数据的布隆过滤器,默认fpp(误差率) 0.03
    8. BloomFilter<String> bf = BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), INSERTIONS);
    9. Set<String> set = new HashSet<String>(INSERTIONS);
    10. List<String> list = new ArrayList<String>(INSERTIONS);
    11. for (int i = 0; i < INSERTIONS; i++) {
    12. String uuid = UUID.randomUUID().toString();
    13. bf.put(uuid);
    14. set.add(uuid);
    15. list.add(uuid);
    16. }
    17. /**
    18. * 布隆过滤器误判的次数
    19. */
    20. int wrong = 0;
    21. /**
    22. * 布隆过滤器正确次数
    23. */
    24. int right = 0;
    25. int total = 10000;
    26. for (int i = 0; i < total; i++) {
    27. String str = "";
    28. if (i % 100 == 0) {
    29. str = list.get(i / 100);
    30. } else {
    31. str = UUID.randomUUID().toString();
    32. }
    33. /*
    34. String str = i % 100 == 0 ? list.get(i / 100) : UUID.randomUUID().toString();
    35. */
    36. if (bf.mightContain(str)) {
    37. if (set.contains(str)) {
    38. right++;
    39. } else {
    40. wrong++;
    41. }
    42. }
    43. }
    44. //right100
    45. System.out.println("right:" + right);
    46. //因为误差率为3%,所以一万条数据wrong的值在300左右
    47. System.out.println("wrong:" + wrong);
    48. }
    49. }

  • 相关阅读:
    Hive(14):Hive调优之Explain查看执行计划
    骨传导耳机伤耳朵吗?带你一分钟了解骨传导耳机
    CDH 01CentOS配置(markdown新版二)
    《风向》——如何应对互联网变革下的知识焦虑不确定与个人成长
    CentOS配置本地yum源
    实现物联网的技术要素
    vue之下拉菜单
    用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
    【无标题】储能电池GB/T 36276测试,储能电池IEC62619测试,储能电池UL1973测试
    canal同步mysql数据变化到kafka(centos部署)
  • 原文地址:https://blog.csdn.net/xixiyuguang/article/details/126848191