hadoop学习--数据排序

来源:互联网 发布:在淘宝上买电动汽车 编辑:程序博客网 时间:2024/05/18 10:36
对输入文件中的数据进行排序,输入文件中每行为一个数字。输出的每行为2个间隔的数字,第一个代表序号,第二个代表原始数字。

输入:

2
7
5
1
7
6
8
7

输出:

1 1
2 2
3 5
4 6
5 7
6 7
7 7
8 8

1、设计思路

在map过程中就有排序,因此利用这个默认的排序。并将其作为key值输出。reduce得到<Key,value-list>,将key值作为value输出,根据value-list的个数确定输出次数。此外还需要输出num,表示次序。

代码如下:

[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. import java.io.IOException;  
  2. import java.util.Iterator;  
  3. import java.util.*;  
  4.   
  5. import org.apache.hadoop.conf.Configuration;  
  6. import org.apache.hadoop.conf.Configured;  
  7. import org.apache.hadoop.fs.Path;  
  8. import org.apache.hadoop.io.Text;  
  9. import org.apache.hadoop.mapred.FileInputFormat;  
  10. import org.apache.hadoop.mapred.FileOutputFormat;  
  11. import org.apache.hadoop.mapred.JobClient;  
  12. import org.apache.hadoop.mapred.JobConf;  
  13. import org.apache.hadoop.mapred.TextInputFormat;  
  14. import org.apache.hadoop.mapred.MapReduceBase;  
  15. import org.apache.hadoop.mapred.Mapper;  
  16. import org.apache.hadoop.mapred.OutputCollector;  
  17. import org.apache.hadoop.mapred.Reducer;  
  18. import org.apache.hadoop.mapred.Reporter;  
  19. import org.apache.hadoop.mapred.TextOutputFormat;  
  20. import org.apache.hadoop.util.Tool;  
  21. import org.apache.hadoop.util.ToolRunner;  
  22. import org.apache.hadoop.io.Writable;  
  23. import org.apache.hadoop.io.*;  
  24.   
  25.   
  26. public class Sort extends Configured implements Tool {  
  27.       
  28.     public static class MapClass extends MapReduceBase  
  29.         implements Mapper<LongWritable, Text, IntWritable, IntWritable> {  
  30.         public  IntWritable count = new IntWritable();  
  31.         public void map(LongWritable key, Text value,  
  32.                         OutputCollector<IntWritable, IntWritable> output,  
  33.                         Reporter reporter) throws IOException {  
  34.             count.set(Integer.parseInt(value.toString()));  
  35.             output.collect(count, new IntWritable(1));  
  36.         }  
  37.     }  
  38.       
  39.     public static class Reduce extends MapReduceBase  
  40.         implements Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {  
  41.         public  IntWritable valuecount = new IntWritable();  
  42.         public  IntWritable num = new IntWritable();  
  43.         public int linenum = 0;  
  44.         public void reduce(IntWritable key, Iterator<IntWritable> values,  
  45.                            OutputCollector<IntWritable, IntWritable> output,  
  46.                            Reporter reporter) throws IOException {  
  47.               
  48.             while (values.hasNext()) {  
  49.                 linenum += values.next().get();  
  50.                 num.set(linenum);  
  51.                 output.collect(num, key);  
  52.             }  
  53.         }  
  54.     }  
  55.       
  56.     public int run(String[] args) throws Exception {  
  57.         Configuration conf = getConf();  
  58.           
  59.         JobConf job = new JobConf(conf, Sort.class);  
  60.           
  61.         Path in = new Path(args[0]);  
  62.         Path out = new Path(args[1]);  
  63.         FileInputFormat.setInputPaths(job, in);  
  64.         FileOutputFormat.setOutputPath(job, out);  
  65.           
  66.         job.setJobName("Sort");  
  67.         job.setMapperClass(MapClass.class);  
  68.         job.setReducerClass(Reduce.class);  
  69.           
  70.         job.setInputFormat(TextInputFormat.class);  
  71.         job.setOutputFormat(TextOutputFormat.class);  
  72.         job.setOutputKeyClass(IntWritable.class);  
  73.         job.setOutputValueClass(IntWritable.class);  
  74.           
  75.         JobClient.runJob(job);  
  76.           
  77.         return 0;  
  78.     }  
  79.       
  80.     public static void main(String[] args) throws Exception {   
  81.         int res = ToolRunner.run(new Configuration(), new Sort(), args);  
  82.           
  83.         System.exit(res);  
  84.     }  
  85. }  

代码分析:

因为这里输入类型使用的是TextInputFormat,它的输出key值是LongWritable类型,输出value值是Text类型,所以map的输入类型是<LongWritable,Text>。这里的输出类型是TextOutputFormat,它的键和值可以是任意类型。这里Map选择所需要的<IntWritable, IntWritable>,map的输出与reduce的输入格式保持一致即可。


运行:

在hadoop目录下,

编译

javac -classpath hadoop-core-1.2.1.jar:lib/commons-cli-1.2.jar -d Sort/classes Sort/src/Sort.java

打包

jar -cvf Sort/Sort.jar -C Sort/classes/ .

运行jar

bin/hadoop jar Sort/Sort.jar Sort /sort.txt output

这里确保HDFS下的/user/root/没有outout目录即可。

查看运行结果:

bin/hadoop fs -cat output/part-00000

1 1
2 2
3 5
4 6
5 7
6 7
7 7
8 8


参考资料:《Hadoop实战》(陆嘉恒)

0 0
原创粉丝点击