Hadoop 实战中的排序问题 - 百思不得其解,求高手解惑!

空山悟 2014-12-12 08:37:37
需求

  对输入文件中的数据进行排序。

  输入文件中的每行内容都是一个数字,要求在输出文件中每行有两个数字,第一个数字代表位次,第二个数字为原始数据。

  比如文件1包含以下数据:

  1

  3

  5

  2

  4

  6

  文件2包含以下数据:

  2

  4

  6

  3

  1

  5

  那么输出文件应当为:

  1  1

  2  1

  3  2

  4  2

  ...
第一列是位次,第二列是原始数据!


下面是代码:

package org.apache.hadoop.examples;

import java.io.IOException;

//导入各种Hadoop包
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 {

// Mapper类
public static class Map extends Mapper<Object, Text, IntWritable, IntWritable>{

// new一个值为空的IntWritable对象
private static IntWritable data = new IntWritable();

// 实现map函数
public void map(Object key, Text value, Context context) throws IOException, InterruptedException {

// 将切分后的value作为中间输出的key,然后value值为1。
String line = value.toString();
data.set(Integer.parseInt(line));
context.write(data, new IntWritable(1));
}
}

// Reducer类
public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {

// new一个值为1的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 class Partition extends Partitioner <IntWritable, IntWritable> {
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 Exception {

// 获取配置参数
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();

// 检查命令语法
if (otherArgs.length != 2) {
System.err.println("Usage: Dedup <in> <out>");
System.exit(2);
}

// 定义作业对象
Job job = new Job(conf, "Sort");
// 注册分布式类
job.setJarByClass(Sort.class);
// 注册Mapper类
job.setMapperClass(Map.class);
// 注册Reducer类
job.setReducerClass(Reduce.class);
// 注册Partition类
//job.setPartitionerClass(Partition.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);
}
}


这段代码在我自己的伪分布式上运行良好,但有几个问题始终想不明白。

1. 25和41行的变量设置为static属性是什么意思?是不是说这个变量在所有的Reduce节点之间是共享的?
2. 我明白Partition 大概的意思,在Partition之后,数据将会被切成块,划分到一个个Partition区间,也就是Reduce节点中去,但问题是这几个Reduce节点必然是并行执行的,那么我的位序统计代码:

// Reducer类
public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {

// new一个值为空的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);
}
}
}

如何有效工作?
这个变量linenum统计的仅仅是在单个的Reduce节点之内的位序吧?即使linenum在各个Reduce节点之间共享,难道就不会发生同步问题???
比如Partition区间划分为 0-5 和 6-10,那么两个区间分配到的数据分别为1,2 ,3和7, 9, 8,。那么当第一个Reduce节点统计完数据2之后(数据3还没统计),第二个节点统计到了数据77,就会把数据7算作位序3了,而实际上,为序为3的应该是第一个区间的3.
...全文
209 2 打赏 收藏 转发到动态 举报
写回复
用AI写文章
2 条回复
切换为时间正序
请发表友善的回复…
发表回复
  • 打赏
  • 举报
回复
各个Reducer节点上的Reduce任务是独立运行的。Reducer上的数据是有序的,每个Reducer会将结果输出到一个HDFS文件。
tchqiq 2014-12-12
  • 打赏
  • 举报
回复
楼上说的没错~ 能产生这样的结果是因为你的输出是一个reduce,这样linenum就等于是全局变量(就是所有数据汇总的一个计数)。你可以做这么个实验,在主类里设置reduce个数:
job.setNumReduceTasks(Integer.parseInt(2);
观察一下 同样,25行和41行的静态变量也只是在同个reduce里有效。

20,809

社区成员

发帖
与我相关
我的任务
社区描述
Hadoop生态大数据交流社区,致力于有Hadoop,hive,Spark,Hbase,Flink,ClickHouse,Kafka,数据仓库,大数据集群运维技术分享和交流等。致力于收集优质的博客
社区管理员
  • 分布式计算/Hadoop社区
  • 涤生大数据
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧