Hadoop Bloom filter应用示例
1. 简介
参见《Hadoop in Action》P102 以及 《Hadoop实战(第2版)》(陆嘉恒)P69
2. 案例
网上大部分的说明仅仅是按照《Hadoop in Action》中的示例代码给出,这里是Hadoop0.20.2版本,在该版本中已经实现了BloomFilter。
案例文件如下:
customers.txt
1,Stephanie Leung,555-555-5555
2,Edward Kim,123-456-7890
3,Jose Madriz,281-330-8004
4,David Stork,408-555-0000
-----------------------------------------------------------------
orders.txt
3,A,12.95,02-Jun-2008
1,B,88.25,20-May-2008
2,C,32.00,30-Nov-2007
3,D,25.02,22-Jan-2009
5,E,34.59,05-Jan-2010
6,F,28.67,16-Jan-2008
7,G,49.82,24-Jan-2009
两个文件通过customer ID关联。
3. 代码
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- import java.util.ArrayList;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.FSDataInputStream;
- import org.apache.hadoop.fs.FileSystem;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.input.FileSplit;
- import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
- import org.apache.hadoop.util.GenericOptionsParser;
- import org.apache.hadoop.util.bloom.BloomFilter;
- import org.apache.hadoop.util.bloom.Key;
- import org.apache.hadoop.util.hash.Hash;
- public class BloomMRMain {
- public static class BloomMapper extends Mapper<Object, Text, Text, Text> {
- BloomFilter bloomFilter = new BloomFilter(10000, 6, Hash.MURMUR_HASH);
- protected void setup(Context context) throws IOException ,InterruptedException {
- Configuration conf = context.getConfiguration();
- String path = "hdfs://localhost:9000/user/hezhixue/input/customers.txt";
- Path file = new Path(path);
- FileSystem hdfs = FileSystem.get(conf);
- FSDataInputStream dis = hdfs.open(file);
- BufferedReader reader = new BufferedReader(new InputStreamReader(dis));
- String temp;
- while ((temp = reader.readLine()) != null) {
- // System.out.println("bloom filter temp:" + temp);
- String[] tokens = temp.split(",");
- if (tokens.length > 0) {
- bloomFilter.add(new Key(tokens[0].getBytes()));
- }
- }
- }
- protected void map(Object key, Text value, Context context) throws IOException ,InterruptedException {
- //获得文件输入路径
- String pathName = ((FileSplit) context.getInputSplit()).getPath().toString();
- if (pathName.contains("customers")) {
- String data = value.toString();
- String[] tokens = data.split(",");
- if (tokens.length == 3) {
- String outKey = tokens[0];
- String outVal = "0" + ":" + tokens[1] + "," + tokens[2];
- context.write(new Text(outKey), new Text(outVal));
- }
- } else if (pathName.contains("orders")) {
- String data = value.toString();
- String[] tokens = data.split(",");
- if (tokens.length == 4) {
- String outKey = tokens[0];
- System.out.println("in map and outKey:" + outKey);
- if (bloomFilter.membershipTest(new Key(outKey.getBytes()))) {
- String outVal = "1" + ":" + tokens[1] + "," + tokens[2]+ "," + tokens[3];
- context.write(new Text(outKey), new Text(outVal));
- }
- }
- }
- }
- }
- public static class BloomReducer extends Reducer<Text, Text, Text, Text> {
- ArrayList<Text> leftTable = new ArrayList<Text>();
- ArrayList<Text> rightTable = new ArrayList<Text>();
- protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException ,InterruptedException {
- leftTable.clear();
- rightTable.clear();
- for (Text val : values) {
- String outVal = val.toString();
- System.out.println("key: " + key.toString() + " : " + outVal);
- int index = outVal.indexOf(":");
- String flag = outVal.substring(0, index);
- if ("0".equals(flag)) {
- leftTable.add(new Text(outVal.substring(index+1)));
- } else if ("1".equals(flag)) {
- rightTable.add(new Text(outVal.substring(index + 1)));
- }
- }
- if (leftTable.size() > 0 && rightTable.size() > 0) {
- for(Text left : leftTable) {
- for (Text right : rightTable) {
- context.write(key, new Text(left.toString() + "," + right.toString()));
- }
- }
- }
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
- if (otherArgs.length != 2) {
- System.err.println("Usage: BloomMRMain <in> <out>");
- System.exit(2);
- }
- Job job = new Job(conf, "BloomMRMain");
- job.setJarByClass(BloomMRMain.class);
- job.setMapperClass(BloomMapper.class);
- job.setReducerClass(BloomReducer.class);
- job.setInputFormatClass(TextInputFormat.class);
- job.setOutputFormatClass(TextOutputFormat.class);
- job.setMapOutputKeyClass(Text.class);
- job.setMapOutputValueClass(Text.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(Text.class);
- FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
- FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }
- }
Hadoop Bloom filter应用示例的更多相关文章
- Hadoop Bloom Filter 使用
1.Bloom Filter 默认的 BloomFilter filter =new BloomFilter(10,2,1); // 过滤器长度为10 ,用2哈希函数,MURMUR_HASH (1) ...
- Bloom Filter 原理与应用
介绍 Bloom Filter是一种简单的节省空间的随机化的数据结构,支持用户查询的集合.一般我们使用STL的std::set, stdext::hash_set,std::set是用红黑树实现的,s ...
- Hadoop0.20.2 Bloom filter应用演示样例
1. 简单介绍 參见<Hadoop in Action>P102 以及 <Hadoop实战(第2版)>(陆嘉恒)P69 2. 案例 网上大部分的说明不过依照<Hadoop ...
- Skip List & Bloom Filter
Skip List | Set 1 (Introduction) Can we search in a sorted linked list in better than O(n) time?Th ...
- Bloom Filter:海量数据的HashSet
Bloom Filter一般用于数据的去重计算,近似于HashSet的功能:但是不同于Bitmap(用于精确计算),其为一种估算的数据结构,存在误判(false positive)的情况. 1. 基本 ...
- 探索C#之布隆过滤器(Bloom filter)
阅读目录: 背景介绍 算法原理 误判率 BF改进 总结 背景介绍 Bloom filter(后面简称BF)是Bloom在1970年提出的二进制向量数据结构.通俗来说就是在大数据集合下高效判断某个成员是 ...
- Bloom Filter 布隆过滤器
Bloom Filter 是由伯顿.布隆(Burton Bloom)在1970年提出的一种多hash函数映射的快速查找算法.它实际上是一个很长的二进制向量和一些列随机映射函数.应用在数据量很大的情况下 ...
- Bloom Filter学习
参考文献: Bloom Filters - the math http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html B ...
- 【转】探索C#之布隆过滤器(Bloom filter)
原文:蘑菇先生,http://www.cnblogs.com/mushroom/p/4556801.html 背景介绍 Bloom filter(后面简称BF)是Bloom在1970年提出的二进制向量 ...
随机推荐
- 黑马程序员——JAVA基础之JDK1.5新特性高级for循环和可变参数
------- android培训.java培训.期待与您交流! ---------- 高级for循环 格式: for(数据类型 变量名 : 被遍历的集合(Collection)或者数组) { ...
- 【转贴】Cortex系列M0-4简单对比
转载网址:http://blog.sina.com.cn/s/blog_7dbd9c0e01018e4l.html 最近搞了块ST的Cortex-M4处理器,然后下了本文档.分享一下. 针对目前进入大 ...
- Thinking Clearly about Performance
http://queue.acm.org/detail.cfm?id=1854041 The July/August issue of acmqueue is out now acmqueue is ...
- Apache ab并发负载压力测试
由于现在网站都需要能够承受高并发要求的能力,所以当我们写完代码后,如果需要上线,最好都经过压力测试后,这样比较好 运行: 在Windows系统下,打开cmd命令行窗口,定位到apache安装目录的bi ...
- JavaScript中类的实现机制
理解类的实现机制在前面已经讲过,在JavaScript中可以使用function关键字来定义一个“类”.现在介绍如何为类添加成员.其过程很简单,在函数内通过this指针引用的变量或者方法都会成为类的成 ...
- CentOS6.6 安装 Tengine 笔记
Tengine官网上有个非常简单的教程,中间并未涉及到一些常用的设置,所以仅供参考.一下午为本人的安装步骤及过程. 1.安装必要的编译环境好 由于Tengine安装需要使用源代码自行编译,所以在安装前 ...
- SwitchyOmega
SwitchyOmega下载安装地址: http://switchyomega.com/download.html GFWList.bak.txt教程 {"+GFWed":{&qu ...
- linux jdk+mysql+tomcat+nginx 项目部署步骤
1.下载linux jdk1.7.0_79.tar.gz ; 下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jdk7-dow ...
- Android退出程序时的"再按一次退出"实现
android 个人觉得当用户按下后退键时,出现"再按一次退出"的提示防止误操作比弹出一个对话框是会更人性化的.话不多说上代码: 第一种:重写onKeyDown来监听: long ...
- ettercap ARP dns 欺骗
1.arp 这个简单,太熟了.略过1 2.dns 根据arp欺骗的步骤. 多了个etter.dns文件 找到它:locate etter.dns 进入后添加dns正向解析 启动,选 ...