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; /** * mapreduce排序 * * @author asheng file1:1.txt 2 32 654 32 15 756 65223 * * file2:2.txt 5956 22 650 92 * * file3:3.txt 26 54 6 * 对file1,file2,file3进行排序,能够第一想到的便是mapreduce自动排序,但是这里面有问题: * Reduce排序只是对发送到自己所在的节点的数据进行排序,不能保证整体的顺序 * 所以这里要自定义Partition,保证Partition后,Reduce上的数据在整体上是有序的,然后在reduce内进行排序 */ public class Sort { public static class Map extends Mapper<Object, Text, IntWritable, IntWritable> { private IntWritable data = new IntWritable(); public void map(Object key, Text value, Context context) throws IOException, InterruptedException { data.set(Integer.parseInt(value.toString())); context.write(data, new IntWritable(1)); } } public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable> { private IntWritable data = new IntWritable(1); public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { for (IntWritable v : values) { System.out.println(v); context.write(data, key); data = new IntWritable(data.get() + 1); } } } 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 && keynumber >= bound * (i - 1)) { return i - 1; } } return 0; } } public static void main(String[] args) throws IOException, InterruptedException, ClassNotFoundException { Configuration conf = new Configuration(); Job job = new Job(conf, "sort"); job.setJarByClass(Sort.class); job.setMapperClass(Map.class); job.setReducerClass(Reduce.class); job.setPartitionerClass(Partition.class); job.setOutputKeyClass(IntWritable.class); job.setOutputValueClass(IntWritable.class); FileInputFormat.setInputPaths(job, "/home/asheng/hadoop/in"); FileOutputFormat .setOutputPath(job, new Path("/home/asheng/hadoop/out")); job.waitForCompletion(true); } }
相关推荐
MapReduce之自定义 OutPutFormat,通过一个案例,实现自定义的一个OutPutFormat,来更加的深刻的理解MR的过程
17、MapReduce的分区Partition介绍 网址:https://blog.csdn.net/chenwewi520feng/article/details/130454574 本文介绍MR的分区Partition。 本文分为2个部分,即介绍与示例。 前提依赖:hadoop环境可正常使用。
自定义MapReduce的InputFormat,实现提取指定开始与结束限定符的内容。
NULL 博文链接:https://username2.iteye.com/blog/2274802
实现mr的wordcount功能和自定义分区的功能、自定义排序功能;com.ellis.mr1为类似wc功能,com.ellis.mr2为自定义分区功能,com.ellis.mr3为自定义排序功能
自定义分隔符读取源码,改编自LineRecordReader,主要处理格式复杂的日志
mapreduce实现全栈排序,简单算法已经在文档中说明,想要了解的可以查看!
mapreduce二次排序,年份升序,按照年份聚合,气温降序
MapReduce二次排序代码实现。 二次排序这里指:在以key为关键字的排序基础上,同时按照value排序
对相同分区的数据,按照key进行排序(默认按照字典排序)、分组。相同key的value放在一个集合中。 (可选)分组后对数据进行归约。 注意:MapReduce中,Mapper可以单独存在,但是Reducer不能存在。
很简单的,面向新手的一个MapReduce实例,对数据进行排序,对大数据理解很有帮助。
利用采样器实现mapreduce任务输出全排序大数据-MapReduce
这段Java代码是一个Hadoop MapReduce程序,用于处理输入数据并计算每个不同词汇的最高分数。它包含了配置和运行MapReduce作业的逻辑,以及Mapper和Reducer类的定义。主要功能是读取输入数据,将数据拆分成词汇和相关...
16、MapReduce的基本用法示例-自定义序列化、排序、分区、分组和topN 网址:https://blog.csdn.net/chenwewi520feng/article/details/130454036 本文介绍MapReduce常见的基本用法。 前提是hadoop环境可正常运行。 ...
mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce mapreduce ...
在hadoop平台上,用mapreduce编程实现大数据的词频统计
基于MapReduce,实现并行计算环境下的网页排序算法
1)一个 MapReduce 作业由 Map 阶段和 Reduce 阶段两部分组成,这两阶段会对数据排序,从这个意义上说,MapReduce 框架本质就是一个 Distributed Sort。 2)在 Map 阶段,Map Task 会在本地磁盘输出一个按照 key ...
数据排序MapReduce实例 对输入文件中每行进行排序,输入文件中的每行内容为一个数字,即一个数据。
通过自定义数据类型,可以使用Hadoop处理单表数据查询以及多表相互关联的应用场景。