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 改变一下需求: ...
随机推荐
- iOS开发总结-UIWebView 集成 浏览器
// // detailWebViewController.m // BJ // // Created by shirenfeng on 16/11/6. // Copyright © 2016年 c ...
- 设置U盘为第一启动顺序
本文转载:http://u.diannaodian.com/Article/1004.html 盘安装系统的首要步骤就是设置U盘为第一启动顺序.下面电脑店官网就来说说到底如何来设置U盘启动顺序吧. ...
- 《Java并发编程实战》第六章 任务运行 读书笔记
一. 在线程中运行任务 无限制创建线程的不足 .线程生命周期的开销很高 .资源消耗 .稳定性 二.Executor框架 Executor基于生产者-消费者模式.提交任务的操作相当于生产者.运行任务的线 ...
- 自己写的Dapper通用数据访问层
using Microsoft.Practices.EnterpriseLibrary.Data; using Microsoft.Practices.EnterpriseLibrary.Data.O ...
- freemarker if..else.. 的使用
FreeMarker是一款模板引擎,今天在做Pad端的时候正好用到,用法非常简单: 在xml配置页面的文件中,直接使用 <#if 1=1> //条件成立要显示的内容 </#if> ...
- 10.3 noip模拟试题
希望[题目描述]网页浏览器者有后退与前进按钮,一种实现这两个功能的方式是用两个栈,“前进栈”.“后退栈”.这里你需要实现以下几个功能:BACK: 如果“后退栈”为空则忽略此命令. 否则将当前两面压入“ ...
- 论前端css初始化的重要性
新手,求喷,刚刚知道每个浏览器都有对 标签的初始化,就造成我们网站开发者开发的web程序,会在不同的网站上有不同的样式风格,这给用户带来了很不好的体验,这也是浏览器本身的原因造成的,这时候,我们不可能 ...
- Objective-C 实例方法可见度,方法
一 实例方法可见度,方法 1.实例变量的可见度 可见度 特点 ...
- UILabel的高度自适应
_content = [UILabel new]; _content.text = @"日落时分,沏上一杯山茶,听一曲意境空远的<禅>,心神随此天籁,沉溺于玄妙的幻境里.仿佛我就 ...
- C# 多个线程一直跑着While(true)
在使用多线程的时候,开了多个线程一直在While(true),会造成CPU占用很高.这时候要在线程内加入一句Thread.Sleep(1),让他稍微睡一下.就不会消耗那么多CPU了. 代码: Thre ...