Hadoop 学习笔记 (十) MapReduce实现排序 全局变量
一些疑问:
1 全排序的话,最后的应该sortJob.setNumReduceTasks(1);
2 如果多个reduce task都去修改 一个静态的 IntWritable ,IntWritable会乱序吧~
输入数据:
file1
2
32
654
32
15
756
65223
file2
5956
22
650
92
file3
26
54
6 import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 MySort { public static class IntSortMapper extends Mapper<Object, Text, IntWritable, NullWritable>{ private IntWritable val = new IntWritable(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String line = value.toString().trim();
val.set(Integer.parseInt(line));
context.write(val, NullWritable.get());
}
} public static class IntSortReducer extends Reducer<IntWritable, NullWritable, IntWritable,IntWritable>{
private IntWritable k = new IntWritable();
public void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException{
k.set();
for (NullWritable value : values) {
context.write(k, key);
}
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String dir_in = "hdfs://localhost:9000/in_sort";
String dir_out = "hdfs://localhost:9000/out_sort"; Path in = new Path(dir_in);
Path out = new Path(dir_out); Configuration conf = new Configuration();
Job sortJob = new Job(conf, "my_sort"); sortJob.setJarByClass(MySort.class); sortJob.setInputFormatClass(TextInputFormat.class);
sortJob.setMapperClass(IntSortMapper.class);
//sortJob.setCombinerClass(SortReducer.class);
//countJob.setPartitionerClass(HashPartitioner.class);
sortJob.setMapOutputKeyClass(IntWritable.class);
sortJob.setMapOutputValueClass(NullWritable.class); FileInputFormat.addInputPath(sortJob, in); sortJob.setReducerClass(IntSortReducer.class);
sortJob.setNumReduceTasks();
sortJob.setOutputKeyClass(IntWritable.class);
sortJob.setOutputValueClass(IntWritable.class);
//countJob.setOutputFormatClass(SequenceFileOutputFormat.class); FileOutputFormat.setOutputPath(sortJob, out); sortJob.waitForCompletion(true); } }
结果:
修改reduce函数(不是用Iterable)
public static class IntSortReducer extends Reducer<IntWritable, NullWritable, IntWritable,IntWritable>{
private IntWritable k = new IntWritable();
public void reduce(IntWritable key, NullWritable value, Context context) throws IOException, InterruptedException{
k.set();
//for (NullWritable value : values) {
context.write(k, key);
//}
}
}
结果:(不是很理解,为啥去掉iterable后就只输出一个value key哪去了呢)
import java.io.IOException; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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 MySort { public static class IntSortMapper extends Mapper<Object, Text, IntWritable, NullWritable>{ private IntWritable val = new IntWritable(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException{
String line = value.toString().trim();
val.set(Integer.parseInt(line));
context.write(val, NullWritable.get());
}
} public static class IntSortReducer extends Reducer<IntWritable, NullWritable, IntWritable,IntWritable>{
private static IntWritable num = new IntWritable();
public void reduce(IntWritable key, Iterable<NullWritable> values, Context context) throws IOException, InterruptedException{ for (NullWritable value : values) {
context.write(num, key);
num = new IntWritable(num.get() + );
}
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String dir_in = "hdfs://localhost:9000/in_sort";
String dir_out = "hdfs://localhost:9000/out_sort"; Path in = new Path(dir_in);
Path out = new Path(dir_out); Configuration conf = new Configuration();
Job sortJob = new Job(conf, "my_sort"); sortJob.setJarByClass(MySort.class); sortJob.setInputFormatClass(TextInputFormat.class);
sortJob.setMapperClass(IntSortMapper.class);
//sortJob.setCombinerClass(SortReducer.class);
//countJob.setPartitionerClass(HashPartitioner.class);
sortJob.setMapOutputKeyClass(IntWritable.class);
sortJob.setMapOutputValueClass(NullWritable.class); FileInputFormat.addInputPath(sortJob, in); sortJob.setReducerClass(IntSortReducer.class);
sortJob.setNumReduceTasks();
sortJob.setOutputKeyClass(IntWritable.class);
sortJob.setOutputValueClass(IntWritable.class);
//countJob.setOutputFormatClass(SequenceFileOutputFormat.class); FileOutputFormat.setOutputPath(sortJob, out); sortJob.waitForCompletion(true); } }
1 2
2 6
3 15
4 22
5 26
6 32
7 32
8 54
9 92
10 650
11 654
12 756
13 5956
14 65223
Hadoop 学习笔记 (十) MapReduce实现排序 全局变量的更多相关文章
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
- Hadoop学习笔记: MapReduce二次排序
本文给出一个实现MapReduce二次排序的例子 package SortTest; import java.io.DataInput; import java.io.DataOutput; impo ...
- hadoop 学习笔记:mapreduce框架详解
开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...
- Hadoop学习笔记:MapReduce框架详解
开始聊mapreduce,mapreduce是hadoop的计算框架,我学hadoop是从hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...
- 【Big Data - Hadoop - MapReduce】hadoop 学习笔记:MapReduce框架详解
开始聊MapReduce,MapReduce是Hadoop的计算框架,我学Hadoop是从Hive开始入手,再到hdfs,当我学习hdfs时候,就感觉到hdfs和mapreduce关系的紧密.这个可能 ...
- hadoop 学习笔记:mapreduce框架详解(转)
原文:http://www.cnblogs.com/sharpxiajun/p/3151395.html(有删减) Mapreduce运行机制 下面我贴出几张图,这些图都是我在百度图片里找到的比较好的 ...
- Hadoop学习笔记—12.MapReduce中的常见算法
一.MapReduce中有哪些常见算法 (1)经典之王:单词计数 这个是MapReduce的经典案例,经典的不能再经典了! (2)数据去重 "数据去重"主要是为了掌握和利用并行化思 ...
- Hadoop学习笔记: MapReduce Java编程简介
概述 本文主要基于Hadoop 1.0.0后推出的新Java API为例介绍MapReduce的Java编程模型.新旧API主要区别在于新API(org.apache.hadoop.mapreduce ...
- hadoop 学习笔记 (十) mapreduce2.0
MapReduce的特色---不擅长的方面 >实时计算 像mysql一样,在毫秒级或者秒级内返回结果 >流式计算 Mapreduce的输入数据时静态的,不能动态变化 MapReduce自身 ...
- 三、Hadoop学习笔记————从MapReduce到Yarn
Yarn减轻了JobTracker的负担,对其进行了解耦
随机推荐
- way/XMPP
http://git.oschina.net/way/XMPP
- careercup-栈与队列 3.1
3.1 描述如何只用一个数组来实现三个栈. 解答 我们可以很容易地用一个数组来实现一个栈,压栈就往数组里插入值,栈顶指针加1: 出栈就直接将栈顶指针减1:取栈顶值就把栈顶指针指向的单元的值返回: 判断 ...
- 【转】iOS应用崩溃日志揭秘
这篇文章还可以在这里找到 英语 If you're new here, you may want to subscribe to my RSS feed or follow me on Twitter ...
- 纯IPv6环境App适配的坑
来源:伯乐在线专栏作者 - MrPeak 链接:http://ios.jobbole.com/86580/ 苹果从2016年6月1号开始,强制所有app必须支持纯IPv6的网络环境.这项举措将对IPv ...
- ios下微信标题修改
很多开发过微信的人估计都遇到过这样的问题,ios下微信页面标题更改不了,而安卓却可以直接用:document.title="你的标题". 下面是解决这个问题的hack: 1.jqu ...
- Html+Css+Js_之table每隔3行显示不同的两种颜色
<html> <head> <script type="text/javascript"> /** 最近因项目的需求,有这样的一个问题: 一个t ...
- iOS 知识-常用小技巧大杂烩
原文链接:http://www.jianshu.com/p/7c3ee5e67d03. 自己看的. 1,打印View所有子视图 po [[self view]recursiveDescription] ...
- AngularJS 路由:ui-router
UI-Router是Angular-UI提供的客户端路由框架,它解决了原生的ng-route的很多不足:视图不能嵌套.这意味着$scope会发生不必要的重新载入.这也是我们在Onboard中引入ui- ...
- Introduction to object
1 Declarations VS definitions (Page 81) declarations: This function or variable exists somew ...
- Memcached认知[分布式]
Memcached是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载. Memcached的服务器客户端通信使用简单的基于文本行的协议. Memcached基于一个存储键/值对的 ...