hadoop学习--数据排序
来源:互联网 发布:在淘宝上买电动汽车 编辑:程序博客网 时间:2024/05/18 10:36
输入:
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,表示次序。
代码如下:
- import java.io.IOException;
- import java.util.Iterator;
- import java.util.*;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.conf.Configured;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapred.FileInputFormat;
- import org.apache.hadoop.mapred.FileOutputFormat;
- import org.apache.hadoop.mapred.JobClient;
- import org.apache.hadoop.mapred.JobConf;
- import org.apache.hadoop.mapred.TextInputFormat;
- import org.apache.hadoop.mapred.MapReduceBase;
- import org.apache.hadoop.mapred.Mapper;
- import org.apache.hadoop.mapred.OutputCollector;
- import org.apache.hadoop.mapred.Reducer;
- import org.apache.hadoop.mapred.Reporter;
- import org.apache.hadoop.mapred.TextOutputFormat;
- import org.apache.hadoop.util.Tool;
- import org.apache.hadoop.util.ToolRunner;
- import org.apache.hadoop.io.Writable;
- import org.apache.hadoop.io.*;
- public class Sort extends Configured implements Tool {
- public static class MapClass extends MapReduceBase
- implements Mapper<LongWritable, Text, IntWritable, IntWritable> {
- public IntWritable count = new IntWritable();
- public void map(LongWritable key, Text value,
- OutputCollector<IntWritable, IntWritable> output,
- Reporter reporter) throws IOException {
- count.set(Integer.parseInt(value.toString()));
- output.collect(count, new IntWritable(1));
- }
- }
- public static class Reduce extends MapReduceBase
- implements Reducer<IntWritable, IntWritable, IntWritable, IntWritable> {
- public IntWritable valuecount = new IntWritable();
- public IntWritable num = new IntWritable();
- public int linenum = 0;
- public void reduce(IntWritable key, Iterator<IntWritable> values,
- OutputCollector<IntWritable, IntWritable> output,
- Reporter reporter) throws IOException {
- while (values.hasNext()) {
- linenum += values.next().get();
- num.set(linenum);
- output.collect(num, key);
- }
- }
- }
- public int run(String[] args) throws Exception {
- Configuration conf = getConf();
- JobConf job = new JobConf(conf, Sort.class);
- Path in = new Path(args[0]);
- Path out = new Path(args[1]);
- FileInputFormat.setInputPaths(job, in);
- FileOutputFormat.setOutputPath(job, out);
- job.setJobName("Sort");
- job.setMapperClass(MapClass.class);
- job.setReducerClass(Reduce.class);
- job.setInputFormat(TextInputFormat.class);
- job.setOutputFormat(TextOutputFormat.class);
- job.setOutputKeyClass(IntWritable.class);
- job.setOutputValueClass(IntWritable.class);
- JobClient.runJob(job);
- return 0;
- }
- public static void main(String[] args) throws Exception {
- int res = ToolRunner.run(new Configuration(), new Sort(), args);
- System.exit(res);
- }
- }
代码分析:
因为这里输入类型使用的是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实战》(陆嘉恒)
- hadoop学习--数据排序
- hadoop学习--数据排序
- hadoop学习笔记之数据排序
- hadoop mapreduce数据排序
- hadoop 数据排序
- Hadoop 数据排序(一)
- hadoop编程实例--数据排序
- Hadoop 案例3----数据排序
- Hadoop 2.x 数据排序
- hadoop mapreduce做数据排序
- 大数据hadoop学习
- hadoop实战学习之用MapReduce简单对整形数据进行全局排序
- (hadoop学习-1)mapreduce实现数据过滤、聚合与排序
- hadoop学习之数据复制
- hadoop学习之数据完整性
- Hadoop大数据学习线路图
- Hadoop大数据学习线路图
- hadoop大数据学习路线
- 迭代删除(Iterator.remove())时的java.lang.IllegalStateException原因及解决办法
- Android 单线程模式有两个简单的规则
- [C#技术] .NET种Json时对单引号和特殊字符串的处理
- java简单抽奖程序
- 【android.content.res.Resources$NotFoundException】解决方案
- hadoop学习--数据排序
- GetMessage和PeekMessage的区别
- ios开发UIImage imageNamed方法的误用
- 安卓推送技术探讨
- 数据库连接池浅析
- WordPress配置smtp服务器出错
- hadoop学习--单表关联
- 快速排序时间复杂度为O(n×log(n))的证明
- 经典电影收藏-美国-新球大战