习题来源:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html
file1
2012-3-1 a
2012-3-2 b
2012-3-3 c
2012-3-4 d
2012-3-5 a
2012-3-6 b
2012-3-7 c
2012-3-3 c
file2
2012-3-1 b
2012-3-2 a
2012-3-3 b
2012-3-4 d
2012-3-5 a
2012-3-6 c
2012-3-7 d
2012-3-3 c import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.NullWritable;
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.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; public class MyDedup { public static class LineNullMapper extends Mapper<Object, Text, Text, NullWritable>{
public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
context.write(value, NullWritable.get());
}
} public static class SortReducer extends Reducer<Text, NullWritable, Text, NullWritable>{
public void reduce(Text key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException{
context.write(key, NullWritable.get());
}
}
如果把Iterable<NullWritable> values 替换为 NullWritable values 如果是不用Iterable迭代器的话,则不进行分组么?
结果是:只排序了并没有完成去重
2012-3-1 a
2012-3-1 b
2012-3-2 a
2012-3-2 b
2012-3-3 b
2012-3-3 c
2012-3-3 c
2012-3-3 c
2012-3-4 d
2012-3-4 d
2012-3-5 a
2012-3-5 a
2012-3-6 b
2012-3-6 c
2012-3-7 c
2012-3-7 d

public static void main(String[] args) throws Exception {

        String dir_in = "hdfs://localhost:9000/in_dedup";
String dir_out = "hdfs://localhost:9000/out_dedup"; Path in = new Path(dir_in);
Path out = new Path(dir_out); Configuration conf = new Configuration();
Job sortJob = new Job(conf, "my_dedup"); sortJob.setJarByClass(MyDedup.class); sortJob.setInputFormatClass(TextInputFormat.class);
sortJob.setMapperClass(LineNullMapper.class);
sortJob.setCombinerClass(SortReducer.class);
//countJob.setPartitionerClass(HashPartitioner.class);
sortJob.setMapOutputKeyClass(Text.class);
sortJob.setMapOutputValueClass(NullWritable.class); FileInputFormat.addInputPath(sortJob, in); sortJob.setReducerClass(SortReducer.class);
// countJob.setNumReduceTasks(1);
sortJob.setOutputKeyClass(Text.class);
sortJob.setOutputValueClass(NullWritable.class);
//countJob.setOutputFormatClass(SequenceFileOutputFormat.class); FileOutputFormat.setOutputPath(sortJob, out); sortJob.waitForCompletion(true); } }
运行结果:
2012-3-1 a
2012-3-1 b
2012-3-2 a
2012-3-2 b
2012-3-3 b
2012-3-3 c
2012-3-4 d
2012-3-5 a
2012-3-6 b
2012-3-6 c
2012-3-7 c
2012-3-7 d

 

mapreduce (六) MapReduce实现去重 NullWritable的使用的更多相关文章

  1. Hadoop阅读笔记(二)——利用MapReduce求平均数和去重

    前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...

  2. mapreduce (五) MapReduce实现倒排索引 修改版 combiner是把同一个机器上的多个map的结果先聚合一次

    (总感觉上一篇的实现有问题)http://www.cnblogs.com/i80386/p/3444726.html combiner是把同一个机器上的多个map的结果先聚合一次现重新实现一个: 思路 ...

  3. mapreduce (二) MapReduce实现倒排索引(一) combiner是把同一个机器上的多个map的结果先聚合一次

    1 思路:0.txt MapReduce is simple1.txt MapReduce is powerfull is simple2.txt Hello MapReduce bye MapRed ...

  4. MapReduce编程:单词去重

    编程实现单词去重要用到NullWritable类型. NullWritable: NullWritable 是一种特殊的Writable 类型,由于它的序列化是零长度的,所以没有字节被写入流或从流中读 ...

  5. 实验六 MapReduce实验:二次排序

    实验指导: 6.1 实验目的基于MapReduce思想,编写SecondarySort程序. 6.2 实验要求要能理解MapReduce编程思想,会编写MapReduce版本二次排序程序,然后将其执行 ...

  6. hadoop —— MapReduce例子 (数据去重)

    参考:http://eric-gcm.iteye.com/blog/1807468 例子1: 概要:数据去重 描述:将file1.txt.file2.txt中的数据合并到一个文件中的同时去掉重复的内容 ...

  7. MapReduce(一) mapreduce基础入门

    一.mapreduce入门 1.什么是mapreduce 首先让我们来重温一下 hadoop 的四大组件:HDFS:分布式存储系统MapReduce:分布式计算系统YARN: hadoop 的资源调度 ...

  8. mapreduce (四) MapReduce实现Grep+sort

    1.txt dong xi cheng xi dong cheng wo ai beijing tian an men qiche dong dong dong 2.txt dong xi cheng ...

  9. mapreduce (三) MapReduce实现倒排索引(二)

    hadoop api http://hadoop.apache.org/docs/r1.0.4/api/org/apache/hadoop/mapreduce/Reducer.html 改变一下需求: ...

随机推荐

  1. 来看看Github上流行的编码规范

      Popular Coding Convention on Github是一个有趣的网站,它根据Github的代码提交情况分析了几种语言流行的代码规范,目前支持对JavaScript,Java,Py ...

  2. 用户浏览器关闭cookie处理方法

    方法一: function getSessionId(){ var c_name = "jsessionid"; // alert("cookie:"+docu ...

  3. 开源 免费 java CMS - FreeCMS2.1 会员站内信

    项目地址:http://www.freeteam.cn/ 站内信 1.1.1 写信 从左側管理菜单点击写信进入. 输入收信人.标题.内容后点击发送button. 1.1.2 收件箱 从左側管理菜单点击 ...

  4. 一、Bitmap的recycle问题

    尽管Android有自己的垃圾回收机制,对于是不是要我们自己调用recycle,还的看情况而定.假设仅仅是使用少量的几张图片,回收与否关系不大.但是若有大量bitmap须要垃圾回收处理,那必定垃圾回收 ...

  5. WebLogic: The Definitive Guide examined WebLogic's security mechanisms--reference

    reference from: http://www.onjava.com/pub/a/onjava/excerpt/weblogic_chap17/index1.html?page=1 ...... ...

  6. python s12 day2

    python s12 day2   入门知识拾遗 http://www.cnblogs.com/wupeiqi/articles/4906230.html 基本数据类型 注:查看对象相关成员 var, ...

  7. Rouh set 入门知识3(上下近似集,正负域,边界域)

    在RS中,引入两个概念:一个是下近似集,另一个是上近似集.下近似集是指当一个集合不能利用有效的等价关系被恰当的分类是时,则可通过另外的集合来达到这个集合的近似.形式上,设X⊆U是任一子集,R是U上的等 ...

  8. HTML5 文件域+FileReader 分段读取文件(四)

    一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...

  9. 关于Android4.x系统默认显示方向各种修改

    1.设置属性值 在device.mk文件中加入PRODUCT_PROPERTY_OVERRIDES += \ ro.sf.hwrotation=180 2.设置屏幕默认显示方向 在frameworks ...

  10. PL/SQL Developer远程连接Oracle数据库

    首先打开电脑,到pl/sql安装的指定目录[D:\app\DZL\product\11.2.0\dbhome_1\NETWORK\ADMIN]找到[tnsnames.ora]     打开[tnsna ...