mapreduce (六) MapReduce实现去重 NullWritable的使用
习题来源: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的使用的更多相关文章
- Hadoop阅读笔记(二)——利用MapReduce求平均数和去重
前言:圣诞节来了,我怎么能虚度光阴呢?!依稀记得,那一年,大家互赠贺卡,短短几行字,字字融化在心里:那一年,大家在水果市场,寻找那些最能代表自己心意的苹果香蕉梨,摸着冰冷的水果外皮,内心早已滚烫.这一 ...
- mapreduce (五) MapReduce实现倒排索引 修改版 combiner是把同一个机器上的多个map的结果先聚合一次
(总感觉上一篇的实现有问题)http://www.cnblogs.com/i80386/p/3444726.html combiner是把同一个机器上的多个map的结果先聚合一次现重新实现一个: 思路 ...
- mapreduce (二) MapReduce实现倒排索引(一) combiner是把同一个机器上的多个map的结果先聚合一次
1 思路:0.txt MapReduce is simple1.txt MapReduce is powerfull is simple2.txt Hello MapReduce bye MapRed ...
- MapReduce编程:单词去重
编程实现单词去重要用到NullWritable类型. NullWritable: NullWritable 是一种特殊的Writable 类型,由于它的序列化是零长度的,所以没有字节被写入流或从流中读 ...
- 实验六 MapReduce实验:二次排序
实验指导: 6.1 实验目的基于MapReduce思想,编写SecondarySort程序. 6.2 实验要求要能理解MapReduce编程思想,会编写MapReduce版本二次排序程序,然后将其执行 ...
- hadoop —— MapReduce例子 (数据去重)
参考:http://eric-gcm.iteye.com/blog/1807468 例子1: 概要:数据去重 描述:将file1.txt.file2.txt中的数据合并到一个文件中的同时去掉重复的内容 ...
- MapReduce(一) mapreduce基础入门
一.mapreduce入门 1.什么是mapreduce 首先让我们来重温一下 hadoop 的四大组件:HDFS:分布式存储系统MapReduce:分布式计算系统YARN: hadoop 的资源调度 ...
- 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 ...
- mapreduce (三) MapReduce实现倒排索引(二)
hadoop api http://hadoop.apache.org/docs/r1.0.4/api/org/apache/hadoop/mapreduce/Reducer.html 改变一下需求: ...
随机推荐
- android edittext 点击回车会响应两次的解决方案
由于Key有Down和Up事件,所以会执行两次. class editTextOnKeyClickListener implements etOnKeyClickListener { @Overrid ...
- cubieboard变身AP 分类: ubuntu cubieboard 2014-11-25 14:04 277人阅读 评论(0) 收藏
加载bcmdhd模块:# modprobe bcmdhd 如果你希望开启 AP 模式,那么:# modprobe bcmdhd op_mode=2 在/etc/modules文件内添加bcmdhd o ...
- apache不解析php文档?提示需要下载(转)
在httpd.cong中 LoadModule php5_module modules/libphp5.so #这一行php5安装的时候就已经自动添加上了 AddType application/x- ...
- RPM制作
http://blog.csdn.net/justlinux2010/article/details/9905425
- Android4.0的Alertdialog对话框,设置点击其他位置不消失
Android4.0以上AlertDialog,包括其他自定义的dialog,在触摸对话框边缘外部,对话框消失. 可以设置这么一条属性,当然必须先AlertDialog.Builder.create( ...
- 怎么让自己的java系统使用支付接口
昨天花了好久的时间学习了支付接口的教,我看了前7集,就够用了,大家上网搜索一下传智播客在线支付还不错. 1.一开始有一个form表单 2.这个表单是他帮你写好的,有很多银行,银行的name都是特定的 ...
- EF 更新数据出现 System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: 异常
EF6更新 数据出现 System.Data.Entity.Infrastructure.DbUpdateConcurrencyException: Store update, insert, or ...
- c#中的interface abstract与virtual
interface用来声明接口1.只提供一些方法规约,不提供方法主体 如 public interface IPerson { void getName();//不包含方法主体 }2.方法不能 ...
- NRPE: Unable to read output 问题处理总结
自定义nagios监控命令check_disk_data,首先在nagios服务端command.cfg定义了#'check_disk_data' command definitiondefine c ...
- 【BZOJ1146】【树链剖分+平衡树】网络管理Network
Description M 公司是一个非常庞大的跨国公司,在许多国家都设有它的下属分支机构或部门.为了让分布在世界各地的N个部门之间协同工作,公司搭建了一个连接整个公司的通 信网络.该网络的结构由N个 ...