2014-06-04 11:55 451人阅读 评论(0) 收藏 举报

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. 代码

  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.ArrayList;
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.FSDataInputStream;
  7. import org.apache.hadoop.fs.FileSystem;
  8. import org.apache.hadoop.fs.Path;
  9. import org.apache.hadoop.io.Text;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.mapreduce.Mapper;
  12. import org.apache.hadoop.mapreduce.Reducer;
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  15. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  17. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  18. import org.apache.hadoop.util.GenericOptionsParser;
  19. import org.apache.hadoop.util.bloom.BloomFilter;
  20. import org.apache.hadoop.util.bloom.Key;
  21. import org.apache.hadoop.util.hash.Hash;
  22. public class BloomMRMain {
  23. public static class BloomMapper extends Mapper<Object, Text, Text, Text> {
  24. BloomFilter bloomFilter = new BloomFilter(10000, 6, Hash.MURMUR_HASH);
  25. protected void setup(Context context) throws IOException ,InterruptedException {
  26. Configuration conf = context.getConfiguration();
  27. String path = "hdfs://localhost:9000/user/hezhixue/input/customers.txt";
  28. Path file = new Path(path);
  29. FileSystem hdfs = FileSystem.get(conf);
  30. FSDataInputStream dis = hdfs.open(file);
  31. BufferedReader reader = new BufferedReader(new InputStreamReader(dis));
  32. String temp;
  33. while ((temp = reader.readLine()) != null) {
  34. //              System.out.println("bloom filter temp:" + temp);
  35. String[] tokens = temp.split(",");
  36. if (tokens.length > 0) {
  37. bloomFilter.add(new Key(tokens[0].getBytes()));
  38. }
  39. }
  40. }
  41. protected void map(Object key, Text value, Context context) throws IOException ,InterruptedException {
  42. //获得文件输入路径
  43. String pathName = ((FileSplit) context.getInputSplit()).getPath().toString();
  44. if (pathName.contains("customers")) {
  45. String data = value.toString();
  46. String[] tokens = data.split(",");
  47. if (tokens.length == 3) {
  48. String outKey = tokens[0];
  49. String outVal = "0" + ":" + tokens[1] + "," + tokens[2];
  50. context.write(new Text(outKey), new Text(outVal));
  51. }
  52. } else if (pathName.contains("orders")) {
  53. String data = value.toString();
  54. String[] tokens = data.split(",");
  55. if (tokens.length == 4) {
  56. String outKey = tokens[0];
  57. System.out.println("in map and outKey:" + outKey);
  58. if (bloomFilter.membershipTest(new Key(outKey.getBytes()))) {
  59. String outVal = "1" + ":" + tokens[1] + "," + tokens[2]+ "," + tokens[3];
  60. context.write(new Text(outKey), new Text(outVal));
  61. }
  62. }
  63. }
  64. }
  65. }
  66. public static class BloomReducer extends Reducer<Text, Text, Text, Text> {
  67. ArrayList<Text> leftTable = new ArrayList<Text>();
  68. ArrayList<Text> rightTable = new ArrayList<Text>();
  69. protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException ,InterruptedException {
  70. leftTable.clear();
  71. rightTable.clear();
  72. for (Text val : values) {
  73. String outVal = val.toString();
  74. System.out.println("key: " + key.toString() + " : " + outVal);
  75. int index = outVal.indexOf(":");
  76. String flag = outVal.substring(0, index);
  77. if ("0".equals(flag)) {
  78. leftTable.add(new Text(outVal.substring(index+1)));
  79. } else if ("1".equals(flag)) {
  80. rightTable.add(new Text(outVal.substring(index + 1)));
  81. }
  82. }
  83. if (leftTable.size() > 0 && rightTable.size() > 0) {
  84. for(Text left : leftTable) {
  85. for (Text right : rightTable) {
  86. context.write(key, new Text(left.toString() + "," + right.toString()));
  87. }
  88. }
  89. }
  90. }
  91. }
  92. public static void main(String[] args) throws Exception {
  93. Configuration conf = new Configuration();
  94. String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  95. if (otherArgs.length != 2) {
  96. System.err.println("Usage: BloomMRMain <in> <out>");
  97. System.exit(2);
  98. }
  99. Job job = new Job(conf, "BloomMRMain");
  100. job.setJarByClass(BloomMRMain.class);
  101. job.setMapperClass(BloomMapper.class);
  102. job.setReducerClass(BloomReducer.class);
  103. job.setInputFormatClass(TextInputFormat.class);
  104. job.setOutputFormatClass(TextOutputFormat.class);
  105. job.setMapOutputKeyClass(Text.class);
  106. job.setMapOutputValueClass(Text.class);
  107. job.setOutputKeyClass(Text.class);
  108. job.setOutputValueClass(Text.class);
  109. FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  110. FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  111. System.exit(job.waitForCompletion(true) ? 0 : 1);
  112. }
  113. }

Hadoop Bloom filter应用示例的更多相关文章

  1. Hadoop Bloom Filter 使用

    1.Bloom Filter  默认的 BloomFilter filter =new BloomFilter(10,2,1); // 过滤器长度为10 ,用2哈希函数,MURMUR_HASH (1) ...

  2. Bloom Filter 原理与应用

    介绍 Bloom Filter是一种简单的节省空间的随机化的数据结构,支持用户查询的集合.一般我们使用STL的std::set, stdext::hash_set,std::set是用红黑树实现的,s ...

  3. Hadoop0.20.2 Bloom filter应用演示样例

    1. 简单介绍 參见<Hadoop in Action>P102 以及 <Hadoop实战(第2版)>(陆嘉恒)P69 2. 案例 网上大部分的说明不过依照<Hadoop ...

  4. Skip List & Bloom Filter

    Skip List | Set 1 (Introduction)   Can we search in a sorted linked list in better than O(n) time?Th ...

  5. Bloom Filter:海量数据的HashSet

    Bloom Filter一般用于数据的去重计算,近似于HashSet的功能:但是不同于Bitmap(用于精确计算),其为一种估算的数据结构,存在误判(false positive)的情况. 1. 基本 ...

  6. 探索C#之布隆过滤器(Bloom filter)

    阅读目录: 背景介绍 算法原理 误判率 BF改进 总结 背景介绍 Bloom filter(后面简称BF)是Bloom在1970年提出的二进制向量数据结构.通俗来说就是在大数据集合下高效判断某个成员是 ...

  7. Bloom Filter 布隆过滤器

    Bloom Filter 是由伯顿.布隆(Burton Bloom)在1970年提出的一种多hash函数映射的快速查找算法.它实际上是一个很长的二进制向量和一些列随机映射函数.应用在数据量很大的情况下 ...

  8. Bloom Filter学习

    参考文献: Bloom Filters - the math    http://pages.cs.wisc.edu/~cao/papers/summary-cache/node8.html    B ...

  9. 【转】探索C#之布隆过滤器(Bloom filter)

    原文:蘑菇先生,http://www.cnblogs.com/mushroom/p/4556801.html 背景介绍 Bloom filter(后面简称BF)是Bloom在1970年提出的二进制向量 ...

随机推荐

  1. 释放Linux系统预留的硬盘空间【转】

    http://www.cnblogs.com/ggjucheng/archive/2012/10/07/2714294.html 前言 大多数文件系统都会保留一部分空间留作紧急情况时用(比如硬盘空间满 ...

  2. nginx反向代理原理和配置讲解

    最近有打算研读nginx源代码,看到网上介绍nginx可以作为一个反向代理服务器完成负载均衡.所以搜罗了一些关于反向代理服务器的内容,整理综合. 一  概述 反向代理(Reverse Proxy)方式 ...

  3. oracle的高可用与负载均衡

    浏览了一下Oracle官方的网页以及非官方的ppt,简单了解了一下Oracle提供的高可用方案.1. RACRAC,  Real Application Clusters多个Oracle服务器组成一个 ...

  4. bing统计【转自CSDN博客】

    文章来源:http://blog.csdn.net/aa512690069/article/details/17918799 其原文是微软一个小题目:http://hero.csdn.net/Ques ...

  5. OpenJudge计算概论-最长平台

    /*=========================================================== 最长平台 总时间限制: 1000ms 内存限制: 65536kB 描述 已知 ...

  6. charles proxy

    charles proxy  一个比较好用的代理工具,可以进行数据的移动,以及pc 数据的抓包.

  7. 解决国内无法下载SDK问题——安卓镜像服务器列表

    mirrors.opencas.cn 80  稍快ubuntu.buct.cn    80 快 Android SDK在线更新镜像服务器 大连东软信息学院镜像服务器地址: http://mirrors ...

  8. 2 TKinter绑定事件

    通过command属性给button绑定事件 目的:点击一下按扭,在窗口中出现一行字 第一种方法(command): #!/usr/bin/env python # _*_ coding:utf-8 ...

  9. html之span标签

    对于文档中的行内元素最好使用span来组合它们,这样就可以通过样式来格式化它们. span没有任何的样式,当对它应用样式时,才会产生变化 id和class属性是span标签的好伴侣,这样做既可以增加适 ...

  10. 【Android代码片段之八】监听Android屏幕是否锁屏

    实现方法:1)通过BroadcastReceiver接收广播Intent.ACTION_SCREEN_ON和Intent.ACTION_SCREEN_OFF可以判断屏幕状态是否锁屏,但是只有屏幕状态发 ...