问题描述

将乱序数字按照升序排序。

思路描述

按照mapreduce的默认排序,依次输出key值。

代码

 package org.apache.hadoop.examples;

 import java.io.IOException;
import java.util.Iterator;
import java.util.StringTokenizer;
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; public class sort {
public sort() {
} public static void main(String[] args) throws Exception {
Configuration conf = new Configuration(); String fileAddress = "hdfs://localhost:9000/user/hadoop/"; //String[] otherArgs = (new GenericOptionsParser(conf, args)).getRemainingArgs();
String[] otherArgs = new String[]{fileAddress+"number.txt", fileAddress+"output"};
if(otherArgs.length < 2) {
System.err.println("Usage: sort <in> [<in>...] <out>");
System.exit(2);
} Job job = Job.getInstance(conf, "sort");
job.setJarByClass(sort.class);
job.setMapperClass(sort.TokenizerMapper.class);
//job.setCombinerClass(sort.SortReducer.class);
job.setReducerClass(sort.SortReducer.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class); for(int i = 0; i < otherArgs.length - 1; ++i) {
FileInputFormat.addInputPath(job, new Path(otherArgs[i]));
} FileOutputFormat.setOutputPath(job, new Path(otherArgs[otherArgs.length - 1]));
System.exit(job.waitForCompletion(true)?0:1);
} public static class TokenizerMapper extends Mapper<Object, Text, IntWritable, IntWritable> { public TokenizerMapper() {
} public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString()); while(itr.hasMoreTokens()) {
context.write(new IntWritable(Integer.parseInt(itr.nextToken())), new IntWritable(1));
} }
} public static class SortReducer extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> { private static IntWritable num = new IntWritable(1); public SortReducer() {
} public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { for(Iterator<IntWritable> i$ = values.iterator(); i$.hasNext();i$.next()) {
context.write(num, key);
}
num = new IntWritable(num.get()+1);
}
} }

注:不能有combiner操作。

不然就会变成

MapReduce编程:数字排序的更多相关文章

  1. 【原创】MapReduce编程系列之二元排序

    普通排序实现 普通排序的实现利用了按姓名的排序,调用了默认的对key的HashPartition函数来实现数据的分组.partition操作之后写入磁盘时会对数据进行排序操作(对一个分区内的数据作排序 ...

  2. Hadoop MapReduce编程学习

    一直在搞spark,也没时间弄hadoop,不过Hadoop基本的编程我觉得我还是要会吧,看到一篇不错的文章,不过应该应用于hadoop2.0以前,因为代码中有  conf.set("map ...

  3. hadoop2.2编程:使用MapReduce编程实例(转)

    原文链接:http://www.cnblogs.com/xia520pi/archive/2012/06/04/2534533.html 从网上搜到的一篇hadoop的编程实例,对于初学者真是帮助太大 ...

  4. MapReduce编程基础

    MapReduce编程基础 1. WordCount示例及MapReduce程序框架 2.  MapReduce程序执行流程 3.  深入学习MapReduce编程(1) 4. 参考资料及代码下载 & ...

  5. MapReduce编程模型及其在Hadoop上的实现

    转自:https://www.zybuluo.com/frank-shaw/note/206604 MapReduce基本过程 关于MapReduce中数据流的传输过程,下图是一个经典演示:  关于上 ...

  6. Hadoop学习笔记—11.MapReduce中的排序和分组

    一.写在之前的 1.1 回顾Map阶段四大步骤 首先,我们回顾一下在MapReduce中,排序和分组在哪里被执行: 从上图中可以清楚地看出,在Step1.4也就是第四步中,需要对不同分区中的数据进行排 ...

  7. 基于Hadoop 2.6.0运行数字排序的计算

    上个博客写了Hadoop2.6.0的环境部署,下面写一个简单的基于数字排序的小程序,真正实现分布式的计算,原理就是对多个文件中的数字进行排序,每个文件中每个数字占一行,排序原理是按行读取后分块进行排序 ...

  8. [Hadoop入门] - 1 Ubuntu系统 Hadoop介绍 MapReduce编程思想

    Ubuntu系统 (我用到版本号是140.4) ubuntu系统是一个以桌面应用为主的Linux操作系统,Ubuntu基于Debian发行版和GNOME桌面环境.Ubuntu的目标在于为一般用户提供一 ...

  9. mapreduce编程模型你知道多少?

    上次新霸哥给大家介绍了一些hadoop的相关知识,发现大家对hadoop有了一定的了解,但是还有很多的朋友对mapreduce很模糊,下面新霸哥将带你共同学习mapreduce编程模型. mapred ...

随机推荐

  1. webservice学习教程(一):理论

    一. WebService到底是什么? webservice是一种跨平台,跨语言的规范,用于不同平台,不同语言开发的应用之间的交互 WebService是一个SOA(面向服务的编程)的架构,它是不依赖 ...

  2. [js]展开运算符

    function f(...args){ console.log(args); } f(1,2,3,4,5) [...args] = [1,2,3,4] function f(...args){ co ...

  3. REDHAT YUM本地源的搭建和使用

    yum源一般分为两种,本地yum源和本地网络yum源,前者是通过文件提供安装包,后者是通过网络下载安装包: 由于Redhat7.3的yum源需要注册付费,所以往往会出现下载yum源安装包失败,如下图: ...

  4. spring-springmvc-jdbc小案例

    此案例是为ssm作铺垫的. 创建一个银行账户和基金账户,然后通过银行账户购买基金.由spring.springmvc.spring自带的c3p0实现. 废话不多说.如下 涉及到的 jar包(多了): ...

  5. Qt QDateEdit QDateTimeEdit

    展示一个效果,然后附上一个“笑话~~”...回想起来都是搞笑的. 笑话来了,,,,几个月前,为了做出时间选择界面,我亲自“创造”了一个...今天发现了QDateEdit的属性CalendarPopup ...

  6. 接口自动化测试持续集成--Soapui接口测试

    接口测试目前笔者掌握的工具有三种: 一.python+requests+jenkins,优点:代码实现接口测试,对测试代码书写比较自由等:缺点:需要测试者需要有一定的代码基础: 二.jmeter+an ...

  7. Nginx 配置文件优化

    user www www; #用户&组 worker_processes auto; #通常是CPU核的数量存储数据的硬盘数量及负载模式,不确定时将其设置为可用的CPU内核数(设置为“auto ...

  8. Dapper 批量插入

    环境 Mssql 自带的Dapper.Net 批量插入 是一条条循环插入 这里改成了单条 Ps:主要此方法要控制字符串长度哦,每个数据库对单条sql字符长度的限制是不一样的. /// <summ ...

  9. 【SW4STM32生成 hex文件的设置方法】

    SW4STM32生成 hex文件的设置方法 开发环境:WIN7_64 + SW4STM32  联系方式:yexiaopeng1992@126.com 修改: 2018年1月21日 在这周,有一个热心的 ...

  10. 【错误总结1:unity StartCoroutine 报 NullReferenceException 错误】

    今天在一个项目中,写了一个单例的全局类,该类的作用是使用协程加载场景.但在StartCoroutine 这一步报了NullReferenceException 的错.仔细分析和搜索之后,得到错误原因. ...