摘要

Bloomfilter运行在一台机器的内存上,不方便持久化(机器down掉就什么都没啦),也不方便分布式程序的统一去重。我们可以将数据进行持久化,这样就克服了down机的问题,常见的持久化方法包括持久化到本地磁盘或结合Redis进行持久化。本文主要介绍持久化到本地的操作。


关于BloomFilter的基本原理、jar包及入门Demo,请参考我的博客:布隆过滤器

数据持久化
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream; import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels; public class Demo1 { public static void main(String[] args) throws FileNotFoundException { BloomFilter<Integer> filter = BloomFilter.create(
Funnels.integerFunnel(),
500,
0.01); //导入数据到filter
for(int i = 0; i < 100; i++ )
{
filter.put(i);
} //数据持久化到本地
File f= new File("d:" + File.separator + "test2");
OutputStream out = null;
out = new FileOutputStream(f); try {
filter.writeTo(out);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} //测试验证
for(int i = 0 ; i < 10; i++)
{
boolean result = filter.mightContain(i); if(result)
{
System.out.println("i = " + i + " " + result);
}
}
}
}
读取持久化数据
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnels; public class Demo2 { public static void main(String[] args) throws FileNotFoundException { BloomFilter<Integer> filter = BloomFilter.create(
Funnels.integerFunnel(),
500,
0.01); //将之前持久化的数据加载到Filter
File f= new File("d:" + File.separator + "test2") ;
InputStream in = null;
in = new FileInputStream(f);
try {
filter = BloomFilter.readFrom(in,Funnels.integerFunnel());
} catch (IOException e) {
e.printStackTrace();
} //测试验证
for(int i = 0 ; i < 10; i++)
{
boolean result = filter.mightContain(i); if(result)
{
System.out.println("i = " + i + " " + result);
}
}
}
}
Demo说明
Demo1:初始化filter对象,并导入测试数据,然后结合writeTo()方法将数据持久化到本地磁盘;
Demo1:初始化filter对象,读取Demo1持久化到磁盘的数据,然后将数据导入到filter;
测试验证:Demo1和Demo2都对创建后的filter进行了测试验证。
更多参考

基于Redis的Bloomfilter去重(附代码)

布隆过滤器

Guava学习笔记:Google Guava 类库简介

google/guava

布隆过滤器(BloomFilter)持久化的更多相关文章

  1. Spark布隆过滤器(bloomFilter)

    数据过滤在很多场景都会应用到,特别是在大数据环境下.在数据量很大的场景实现过滤或者全局去重,需要存储的数据量和计算代价是非常庞大的.很多小伙伴第一念头肯定会想到布隆过滤器,有一定的精度损失,但是存储性 ...

  2. HBase之八--(3):Hbase 布隆过滤器BloomFilter介绍

    布隆过滤器( Bloom filters) 数据块索引提供了一个有效的方法,在访问一个特定的行时用来查找应该读取的HFile的数据块.但是它的效用是有限的.HFile数据块的默认大小是64KB,这个大 ...

  3. 白话布隆过滤器BloomFilter

    通过本文将了解到以下内容: 查找问题的一般思路 布隆过滤器的基本原理 布隆过滤器的典型应用 布隆过滤器的工程实现 场景说明: 本文阐述的场景均为普通单机服务器.并非分布式大数据平台,因为在大数据平台下 ...

  4. 【浅析】|白话布隆过滤器BloomFilter

    通过本文将了解到以下内容: 查找问题的一般思路 布隆过滤器的基本原理 布隆过滤器的典型应用 布隆过滤器的工程实现 场景说明: 本文阐述的场景均为普通单机服务器.并非分布式大数据平台,因为在大数据平台下 ...

  5. Hbase 布隆过滤器BloomFilter介绍

    转载自:http://blog.csdn.net/opensure/article/details/46453681 1.主要功能 提高随机读的性能 2.存储开销 bloom filter的数据存在S ...

  6. 海量数据处理之布隆过滤器BloomFilter算法

    Bloom Filter是由Bloom在1970年提出的一种多哈希函数映射的快速查找算法.通常应用在一些需要快速判断某个元素是否属于集合,但是并不严格要求100%正确的场合.使用场景:数据量为100亿 ...

  7. SpringBoot(18)---通过Lua脚本批量插入数据到Redis布隆过滤器

    通过Lua脚本批量插入数据到布隆过滤器 有关布隆过滤器的原理之前写过一篇博客: 算法(3)---布隆过滤器原理 在实际开发过程中经常会做的一步操作,就是判断当前的key是否存在. 那这篇博客主要分为三 ...

  8. guava布隆过滤器

    pom引入依赖 <dependency> <groupId>com.google.guava</groupId> <artifactId>guava&l ...

  9. 浅谈布隆过滤器Bloom Filter

    先从一道面试题开始: 给A,B两个文件,各存放50亿条URL,每条URL占用64字节,内存限制是4G,让你找出A,B文件共同的URL. 这个问题的本质在于判断一个元素是否在一个集合中.哈希表以O(1) ...

随机推荐

  1. python数据处理之基本函数

    一.基本函数篇 1)python strip()函数介绍 函数原型 声明:s为字符串,rm为要删除的字符序列 s.strip(rm) 删除s字符串中开头.结尾处,位于 rm删除序列的字符 s.lstr ...

  2. Codeforces 626B Cards(模拟+规律)

    B. Cards time limit per test:2 seconds memory limit per test:256 megabytes input:standard input outp ...

  3. [bzoj3124] [Sdoi2013]直径

    看了child学长的题解才知道怎么写TAT http://www.cnblogs.com/ctlchild/p/5160272.html 以前不知道直径都是过重心的..代码改着改着就和标程完全一样了Q ...

  4. hdu_1042(模拟大数乘法)

    计算n! #include<cstring> #include<cstdio> using namespace std; ]; int main() { int n; whil ...

  5. Let the Balloon Rise(水)

    题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...

  6. flume1.8 Channel类型介绍(四)

    1. Flume Channel Channels是events在agent上进行的存储库.Source添加events,Sink移除events. 1.1 Memory Channel(内存Chan ...

  7. mysql中配置ssl_key、ssl-cert、ssl-ca的路径及建立ssl连接

    1.创建 CA 私钥和 CA 证书 (1)下载并安装openssl,将bin目录配置到环境变量: (2)设置openssl.cfg路径(若不设置会报错,找不到openssl配置文件) \bin\ope ...

  8. ECharts 环形饼图 动态获取json数据

    ECharts  环形饼图 动态获取json数据 效果图如下: 一.html部分 <div id="secondPieChart" style="width:100 ...

  9. python数据类型(二)

    一.List(列表) List(列表) 是 Python 中使用最频繁的数据类型. 列表可以完成大多数集合类的数据结构实现.列表中元素的类型可以不相同,它支持数字,字符串甚至可以包含列表(所谓嵌套). ...

  10. 在虚拟机(VMware)中安装Linux CentOS 6.4系统(图解) 转

    一.下载最新版本Linux CentOS     1.打开官网地址:http://www.centos.org/,点击Downloads->Mirrors         2.点击CentOS ...