本文主要包含mapreduce编程实例,mapreduce实例,hadoop mapreduce实例,mapreduce的应用实例,mapreduce排序等服务器相关知识,网友希望可以进行参考
MapReduce排序及实例,MapReduce排序实例
排序可分为四种排序:
普通排序
部分排序
全局排序
二次排序(比如有两列数据,第一列相同时,需要对第二列进行排序。)
普通排序
普通排序是Mapreduce本身就自带排序功能;
Text对象是不适合排序的;IntWritable,LongWritable等实现了WritableComparable类型的对象都是可以排序的;
部分排序
map和reduce处理过程中默认包含了对key的排序,如果不要求全排序,可以直接把结果输出,那么每个输出文件中包含的就是安装key执行排序的结果;
全局排序
Hadoop平台并没有提供全局数据排序,而在大规模数据处理中进行数据的全局排序是非常普遍的需求;使用hadoop进行大量的数据排序最直观的方法是把文件所以内容给map之后,map不做任何处理,直接输出给一个reduce(一个reduce处理的话,不是很适合大规模的数据,效率不高。),利用hadoop自己的shuffle机制,对所有数据进行排序,而后由reduce直接输出;
如果要对大规模数据处理中进行数据的全局排序的话,
主要思路就是将数据按照区间进行分割,比如对整数排序,
[0,10000]的在partition 0中,(10000,20000]在partition 1中,
在数据分布均匀的情况下,每个分区内的数据量基本相同,这种就是比较理想的情况了,但是实际中数据往往分布不均匀,出现了数据倾斜的情况,这时按照之前的分区划分数据就不合适了,此时就需要一定的帮助——采样器;
package Sort;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.NullWritable;
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;
public class Demo {
private final static String INPUT_PATH = "hdfs://liguodong:8020/input";
private final static String OUTPUT_PATH = "hdfs://liguodong:8020/output";
public static class MyMapper extends Mapper<LongWritable, Text, LongWritable, NullWritable>{
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] values = value.toString().split("\\s+");
context.write(new LongWritable(Long.parseLong(values[0])), NullWritable.get());
}
}
public static class MyReducer extends Reducer<LongWritable, NullWritable, LongWritable, NullWritable>{
@Override
protected void reduce(LongWritable key, Iterable<NullWritable> values,
Context context)
throws IOException, InterruptedException {
context.write(key, NullWritable.get());
}
}
public static class MyPartitioner extends Partitioner<LongWritable, NullWritable>{
@Override
public int getPartition(LongWritable key, NullWritable value,
int numPartitions) {
if(key.get() <= 100){
return 0%numPartitions;
}
if(key.get()>100 && key.get()<1000){
return 1%numPartitions;
}
return 2;
}
}
public static void main(String[] args) throws
ClassNotFoundException, IOException, InterruptedException, URISyntaxException {
Configuration conf = new Configuration();
final FileSystem fileSystem = FileSystem.get(new URI(INPUT_PATH),conf);
if(fileSystem.exists(new Path(OUTPUT_PATH)))
{
fileSystem.delete(new Path(OUTPUT_PATH),true);
}
Job job = Job.getInstance(conf, "shuffle sort");
job.setJarByClass(Demo.class);
FileInputFormat.addInputPath(job, new Path(INPUT_PATH));
job.setMapperClass(MyMapper.class);
job.setMapOutputKeyClass(LongWritable.class);
job.setMapOutputValueClass(NullWritable.class);
job.setPartitionerClass(MyPartitioner.class);
job.setReducerClass(MyReducer.class);
job.setOutputKeyClass(LongWritable.class);
job.setOutputValueClass(NullWritable.class);
job.setNumReduceTasks(3);
FileOutputFormat.setOutputPath(job, new Path(OUTPUT_PATH));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
[root@liguodong file]

