hadoop —— MapReduce例子 (数据排序)
参考:http://eric-gcm.iteye.com/blog/1807468
file1.txt:
2
32
654
32
15
756
65223
file2.txt:
5956
22
650
92
file3.txt:
26
54
6
JAVA代码:
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.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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser; public class Sort { // map将输入中的value化成IntWritable类型,作为输出的key
public static class Map extends
Mapper<Object, Text, IntWritable, IntWritable> { private static IntWritable data = new IntWritable(); // 实现map函数
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException { String line = value.toString();
data.set(Integer.parseInt(line));
context.write(data, new IntWritable(1));
}
} // reduce将输入中的key复制到输出数据的key上,
// 然后根据输入的value-list中元素的个数决定key的输出次数
// 用全局linenum来代表key的位次
public static class Reduce extends
Reducer<IntWritable, IntWritable, IntWritable, IntWritable> { private static IntWritable linenum = new IntWritable(1); // 实现reduce函数
public void reduce(IntWritable key, Iterable<IntWritable> values,
Context context)
throws IOException, InterruptedException { for (IntWritable val : values) { context.write(linenum, key);
linenum = new IntWritable(linenum.get() + 1);
}
}
} public static void main(String[] args) throws Exception { Configuration conf = new Configuration(); // 这句话很关键
conf.set("mapred.job.tracker", "172.16.11.74:9001"); String[] ioArgs = new String[] { "sort_in", "sort_out" };
String[] otherArgs = new GenericOptionsParser(conf, ioArgs)
.getRemainingArgs(); if (otherArgs.length != 2) { System.err.println("Usage: Data Sort <in> <out>");
System.exit(2);
} Job job = new Job(conf, "Data Sort");
job.setJarByClass(Sort.class); // 设置Map和Reduce处理类
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class); // 设置输出类型
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class); // 设置输入和输出目录
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
Sort
运行结果:
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
具体打包运行步骤:
参考上一篇博文:http://www.cnblogs.com/-wangjiannan/p/3590324.html
知识点:
MapReduce的默认排序规则是按照key值进行排序的。
如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,
如果key为封装为String的Text类型,那么MapReduce按照字典顺序对字符串排序。
代码理解:
map阶段:
1. String line = value.toString();
实现的map方法中,针对文本的一行(line)处理,遍历每行的代码框架内部实现了
2. context.write(data, new IntWritable(1));
每一行:key是data(强转成IntWritable类型的 line),value是IntWritable类型的 1
3. 所有行默认排序好了,而且是按递增顺序的
若有重复的行,那么data对应的value合并成一个集合{Values}({IntWritable类型的 1+})
reduce阶段:
1. reduce(IntWritable key, Iterable<IntWritable> values, Context context)
每一行:key是map阶段后的data,values是data对应的集合{Values}
2. for (IntWritable val : values) { context.write(linenum, key); linenum = new IntWritable(linenum.get() + 1); }
这行代码的作用是输出: 行号 data
同时:行号递增,若有重复的行,则换行输出
hadoop —— MapReduce例子 (数据排序)的更多相关文章
- hadoop —— MapReduce例子 (数据去重)
参考:http://eric-gcm.iteye.com/blog/1807468 例子1: 概要:数据去重 描述:将file1.txt.file2.txt中的数据合并到一个文件中的同时去掉重复的内容 ...
- Hadoop MapReduce 二次排序原理及其应用
关于二次排序主要涉及到这么几个东西: 在0.20.0 以前使用的是 setPartitionerClass setOutputkeyComparatorClass setOutputValueGrou ...
- Hadoop MapReduce例子-新版API多表连接Join之模仿订单配货
文章为作者原创,未经许可,禁止转载. -Sun Yat-sen University 冯兴伟 一. 项目简介: 电子商务的发展以及电商平台的多样化,类似于京东和天猫这种拥有过亿用户的在线购 ...
- hadoop mapreduce实现数据去重
实现原理分析: map函数数将输入的文本按照行读取, 并将Key--每一行的内容 输出 value--空. reduce 会自动统计所有的key,我们让reduce输出key-> ...
- hadoop —— MapReduce例子 (求平均值)
参考:http://eric-gcm.iteye.com/blog/1807468 math.txt: 张三 88 李四 99 王五 66 赵六 77 china.txt: 张三 78 李四 89 王 ...
- Hadoop! | 大数据百科 | 数据观 | 中国大数据产业观察_大数据门户
你正在使用过时的浏览器,Amaze UI 暂不支持. 请 升级浏览器 以获得更好的体验! 深度好文丨读完此文,就知道Hadoop了! 来源:BiThink 时间:2016-04-12 15:1 ...
- Hadoop MapReduce执行过程详解(带hadoop例子)
https://my.oschina.net/itblog/blog/275294 摘要: 本文通过一个例子,详细介绍Hadoop 的 MapReduce过程. 分析MapReduce执行过程 Map ...
- Hadoop学习笔记—11.MapReduce中的排序和分组
一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...
- Hadoop Mapreduce分区、分组、二次排序过程详解[转]
原文地址:Hadoop Mapreduce分区.分组.二次排序过程详解[转]作者: 徐海蛟 教学用途 1.MapReduce中数据流动 (1)最简单的过程: map - reduce (2) ...
随机推荐
- 上传项目至svn服务器,从svn上获取项目
1.在桌面右键->TortoiseSVn->Repo_brower->输入地址,进入 ,ok 2.在地址目录上右键==>>add folder==>>选择你要 ...
- Android开发人员不得不收集的代码(转)
App相关→AppUtils.java 安装App installApp 卸载指定包名的App uninstallApp 获取当前App信息 getAppInfo 获取所有已安装App信息 getAl ...
- 自定义序列化4 (MFC调用C#的.dll)
CLR:CLR常用简写词语,CLR是公共语言运行时,Common Language Runtime)和Java虚拟机一样也是一个运行时环境,它负责资源管理(内存分配和垃圾收集),并保证应用和底层操作系 ...
- FTP 连接报错
Filezilla 站点管理器=>选中FTP站点=>加密(只使用普通FTP)
- XXE攻击
1.背景 现在很多应用都存在XXE(XML External Entity attack)漏洞,就是xml外部实体攻击,比如facebook,很多XML的解析器默认是含有XXE漏洞的. 2.xml的定 ...
- 安装 r 里的 igraph 报错
转载来源:http://genek.tv/article/40 1186 0 0 安装 r 里的 igraph 报错: foreign-graphml.c: In function ‘igraph_w ...
- linux 经常使用命令
帮助信息 ./configure -help|grep mysql 安装php ./configure --prefix=/usr/local/fastphp --with-mysql=mysqlnd ...
- python读写数据篇
一.读写数据1.读数据 #使用open打开文件后一定要记得调用文件对象的close()方法.比如可以用try/finally语句来确保最后能关闭文件.file_object = open('thefi ...
- Struts2 ModelDriven接口使用
用户在做http请求时一般都有两种方式:get和post方式.get方式用来获取查询相关信息,既向服务器获得信息,而post方式用来更新信息既向服务器提交数据.通常情况下,用get方式向服务器获取信息 ...
- 基于EasyNVR+EasyDSS H5视频直播二次开发实现业务需求:直接使用播放页面
之前的"网页直播.微信直播技术解决方案:EasyNVR与EasyDSS流媒体服务器组合之区分不同场景下的easynvr"有介绍一些功能.由于客户需求,我们定制一下功能.给该套方案添 ...