数据排序是许多实际任务在执行时要完成的第一项工作,比如学生成绩评比、数据建立索引等。这个实例和数据去重类似,都是先对原始数据进行初步处理,为进一步的数据操作打好基础。

1.实例描述

  对输入文件中的数据进行排序。输入文件中的每行内容均为一个数字,即一个数据。要求在输出中每行有两个间隔的数字,其中,第二个数字代表原始数据,第一个数字这个原始数据在原始数据集中的位次。

  样例输入:

  file1:

  2
  32
  654
  32
  15
  756
  65223

  file2:

  5956
  22
  650
  92

  file3:

  26
  54
  6

  样例输出:

  

2.设计思路

  这个实例仅仅要求对输入数据进行排序,熟悉MapReduce过程的读者很快会想到在MapReduce过程中就有排序。是否可以利用这个默认的排序、而不需要自己在实现具体的排序呢?答案是肯定的。但是在使用之前首先要了解MapReduce过程中的默认排序规则。它是按照key值进行排序,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序;如果key为封装String的Text类型,那么MapReduce按照字典顺序对字符串排序。需要注意的是,Reduce自动排序的数据仅仅是发送到自己所在节点的数据,使用默认的排序并不能保证全局的顺序,因为在排序前还有一个partition的过程,默认无法保证分割后各个Reduce上的数据整体是有序的。所以要想使用默认的排序过程,还必须定义自己的Partition类,保证执行Partition过程之后所有Reduce上的数据在整体上是有序的,然后再对局部Reduce上的数据进行默认排序,这样才能保证所有数据有序。了解了这个细节,我们就知道,首先应该使用封装int的IntWritable型数据结构,也就是将读入的数据在Map中转化为IntWritable型,然后作为key值输出(value任意);其次需要重写partition,保证整体有序,具体做法是用输入数据的最大值除以系统partition数量的商作为分割数据的边界增量,也就是说分割数据的边界为此商的1倍、2倍至numPartitions-1倍,这样就能保证执行partition后的数据是整体有序的;然后Reduce获得<key,value-list>之后,根据value-list中元素的个数将输入的key作为value的输出次数,输出的key是一个全局变量,用于统计当前key的位次。需要注意的是,这个程序中没有配置Combiner,也就是说在MapReduce过程中不使用Combiner。这主要是因为使用Map和Reduce就已经能够完成任务了。

3.程序代码:

  程序代码如下:

 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.Partitioner;
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();
@Override
protected void map(Object key, Text value,Mapper<Object, Text, IntWritable, IntWritable>.Context context)
throws IOException, InterruptedException {
// super.map(key, value, context);
String line = value.toString();
data.set(Integer.parseInt(line));
context.write(data, new IntWritable(1));
}
}
// reduce 将输入的key复制到输出的value上,然后根据输入的 value-list 中元素的个数决定key的输出次数
// 用全局linenum来代表key的位次
public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{
private static IntWritable linenum = new IntWritable(1);
@Override
protected void reduce(IntWritable key, Iterable<IntWritable> values,Reducer<IntWritable, IntWritable, IntWritable, IntWritable>.Context context)
throws IOException, InterruptedException {
// super.reduce(arg0, arg1, arg2);
for(IntWritable val : values){
context.write(linenum, key);
linenum = new IntWritable(linenum.get()+1);
}
}
}
// 自定义Partition函数,此函数根据输入数据的最大值和 MapReduce 框架中 Partition 的数量获取将输入数据按照大小分块的边界,
//然后根据输入数值和边界的关系返回对应的 Partition ID
public static class Partition extends Partitioner<IntWritable, IntWritable>{
@Override
public int getPartition(IntWritable key, IntWritable value, int numPartitions) {
int Maxnumber = 65223;
int bound = Maxnumber/numPartitions + 1;
int keynumber = key.get();
for (int i = 0; i < numPartitions; i++) {
if (keynumber < bound * (i+1) && keynumber >= bound *i) {
return i;
}
}
return -1;
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length!=2){
System.out.println("Usage:Score Avg");
System.exit(2);
}
Job job = new Job(conf,"Sort");
job.setJarByClass(Sort.class);
job.setMapperClass(Map.class);
job.setPartitionerClass(Partition.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true)?0:1);
} }

  

Hadoop 排序的更多相关文章

  1. 转载:Hadoop排序工具用法小结

    本文转载自Silhouette的文章,原文地址:http://www.dreamingfish123.info/?p=1102 Hadoop排序工具用法小结 发表于 2014 年 8 月 25 日 由 ...

  2. hadoop排序组合键的使用情况

    于hadoop当处理复杂的业务,需要使用组合键,与单纯的复杂的继承Writable接口,但继承WritableComparable<T>接口.事实上.WritableComparable& ...

  3. hadoop排序 -- 全排序

    目录 一.关于Reducer全排序 1.1. 什么叫全排序 1.2. 分区的标准是什么 二.全排序的三种方式 2.1. 一个Reducer 2.2. 自定义分区函数 2.3. 采样 一.关于Reduc ...

  4. 一起学Hadoop——TotalOrderPartitioner类实现全局排序

    Hadoop排序,从大的范围来说有两种排序,一种是按照key排序,一种是按照value排序.如果按照value排序,只需在map函数中将key和value对调,然后在reduce函数中在对调回去.从小 ...

  5. hadoop 补充(转)

    1.输入文件: 文件是MapReduce任务的数据的初始存储地.正常情况下,输入文件一般是存在HDFS里.这些文件的格式可以是任意的:我们可以使用基于行的日志文件,也可以使用二进制格式,多行输入记录或 ...

  6. hadoop的一些名词解释

    在网上收集了一些mapreduce中常用的一些名词的解释,分享一下: Shuffle(洗牌):当第一个map任务完成后,节点可能还要继续执行更多的map 任务,但这时候也开始把map任务的中间输出交换 ...

  7. hadoop各个名词的理解

    Hadoop家族的各个成员 hadoop这个词已经流行好多年了,一提到大数据就会想到hadoop,那么hadoop的作用是什么呢? 官方定义:hadoop是一个开发和运行处理大规模数据的软件平台.核心 ...

  8. 笔记 Hadoop

    今天有缘看到董西成写的<Hadoop技术内幕:深入解析MapReduce架构设计与实现原理>,翻了翻觉得是很有趣的而且把hadoop讲得很清晰书,就花了一下午的时间大致拜读了一下(仅浏览了 ...

  9. Hadoop常用操作汇总

    Hadoop Streaming示例程序(wordcount) run_hadoop_word_counter.sh $HADOOP_BIN streaming \ -input "${IN ...

随机推荐

  1. linux 下修改网关mac地址

    以rtl8196e为例 eth0:mac 地址设为123456789012 # flash set hw_nic0_addr  123456789012 eth1:mac 地址设为1122334455 ...

  2. 出错:Error creating bean with name 'studentServiceImpl': Unsatisfied dependency expressed through field 'studentMapper';

    详细错误: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with nam ...

  3. MySQL慢查询日志汇总

    概念: MySQL的慢查询日志是MySQL提供的一种日志记录,它用来记录在MySQL中响应时间超过阀值的语句,具体指运行时间超过long_query_time值的SQL,则会被记录到慢查询日志中.lo ...

  4. JDBC 连接

    转载至:https://www.liyongzhen.com/ 在这一小节,我们将学习DriverManager对象和connection对象. DriverManager对象用于从驱动里获取一个co ...

  5. pta总结3

    7-1 抓老鼠啊~亏了还是赚了? (20 分) 某地老鼠成灾,现悬赏抓老鼠,每抓到一只奖励10元,于是开始跟老鼠斗智斗勇:每天在墙角可选择以下三个操作:放置一个带有一块奶酪的捕鼠夹(T),或者放置一块 ...

  6. 小米Note 2简单卡刷开发版启用root超级权限的步骤

    小米的机器不同手机型号一般MIUI官方论坛都提供两个不同版本,分别为稳定版和开发版,稳定版没有提供Root超级权限管理,开发版中就开启了Root超级权限,较多时候我们需要使用的一些功能强大的软件,都需 ...

  7. XMLHttpRequest中常用的方法

    a.) abort() 停止当前请求b.) getAllResponseHeaders() 把HTTP请求的所有响应首部作为键值对返回c.) open("method", &quo ...

  8. 用Python建立连接直接读取与更改Rockwell Control Logix Controller的tag值

    请参考如下连接的开源项目 源代码在本地测试好用. 注: 这里所用的Python 版本是 2.7.11 https://github.com/dmroeder/pylogix

  9. MongoDB与Spring整合(支持事务)——SpringDataMongoDB

    1.将MongoDB设置为复制集模式 a.修改 mongod.cfg 文件,添加replSetName复制集名称 #replication: replication: replSetName: &qu ...

  10. 【转】JY 博客

    http://www.lovewebgames.com/demo.html http://www.lovewebgames.com/