hadoop编程(3)-MapReduce案例:文本去重

来源:互联网 发布:java线程机制 编辑:程序博客网 时间:2024/06/03 14:38

思路

把每行文本全部作为键,忽视值。

因为相同的键会发送给同一个Reduce任务进行归并,归并时同样key的所有value将归并为一个列表,当然我们并不关注这个列表,我们继续把key写出即可。

这个过程利用了MapReduce归并的特性自然对数据集去重。

代码

  public class DistinctMapper extends Mapper<LongWritable,Text,Text,NullWritable>{    @Override    protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {      context.write(value,NullWritable.get());    }  }
public  class DistinctReducer extends Reducer<Text,NullWritable,Text,NullWritable> {  @Override  protected void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException {    context.write(key,NullWritable.get());  }}

单元测试代码

public class DistinctTest {  MapReduceDriver<LongWritable, Text, Text, NullWritable, Text, NullWritable> mapReduceDriver;  @Before  public void setUp() {    mapReduceDriver = MapReduceDriver.newMapReduceDriver(new DistinctMapper(), new DistinctReducer());  }  @Test  public void map() throws Exception {    mapReduceDriver        .withInput(new LongWritable(0), new Text("hello"))        .withInput(new LongWritable(1), new Text("hello"))        .withInput(new LongWritable(2), new Text("hi"))        .withOutput(new Text("hello"), NullWritable.get())        .withOutput(new Text("hi"), NullWritable.get())        .runTest();  }}
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 往复式剃须刀哪个牌子好 飞机可以带剃须刀吗 飞利浦剃须刀充电指示灯说明 米家剃须刀无法充电解决方法 手动剃须刀会越刮越粗么 超人剃须刀和飞科哪个好 飞利浦剃须刀充电指示灯 剃须刀哪个牌子的好 什么牌子的剃须刀好 什么牌子的剃须刀好用 什么样的剃须刀好用 飞利浦剃须刀价格表 男士剃须刀什么牌子好 飞利浦剃须刀哪个型号好 飞利浦剃须刀配件网上商城 飞利浦电动剃须刀价格 飞利浦剃须刀旗舰店 飞利浦哪款剃须刀好用 剃须刀手动好还是电动好 电动和手动剃须刀哪个好 手动和电动剃须刀哪个好 手动剃须刀好还是电动的好 手动剃须刀哪个牌子的好 电动剃须刀怎么清洗 剃须刀电动的好还是手动的好 日本剃须刀哪个牌子好 飞科剃须刀价格图片 飞利浦剃须刀刀头价格 飞科电动剃须刀价格 飞利浦哪款剃须刀性价比高 博朗和松下剃须刀哪个好 男人用什么剃须刀好 什么剃须刀好用又便宜 剃须刀哪个牌子好用 飞科fs711 飞科电动刮胡刀 飞利浦电须刀 刮胡刀电动的好还是手动的好 手动刮胡刀好还是电动的好 松下刮胡刀 女士香水淡香型排名