"数据排序"是许多实际任务执行时要完成的第一项工作,比如学生成绩评比、数据建立索引等。这个实例和数据去重类似,都是先对原始数据进行初步处理,为进一步的数据操作打好基础
对输入文件中数据进行排序。输入文件中的每行内容均为一个数字,即一个数据。要求在输出中每行有两个间隔的数字,其中,第一个代表原始数据在原始数据集中的位次,第二个代表原始数据。
样例输入如下所示:
1)file1
2
32
654
32
15
756
65223
2)file2
5956
22
650
92
3)file3
26
54
6
期望输出:
1 2
2 6
3 15
4 22
5 26
6 32
7 32
8 54
9 92
10 650
11 654
12 756
13 5956
14 65223
这个实例仅仅要求对输入数据进行排序
分析:
MapReduce过程中就有排序,它的默认排序规则按照key值进行排序的,如果key为封装int的IntWritable类型,那么MapReduce按照数字大小对key排序,如果key为封装为String的Text类型,那么MapReduce按照字典顺序对字符串排序。
使用封装int的IntWritable型数据结构了。也就是在map中将读入的数据转化成IntWritable型,然后作为key值输出(value任意)。reduce拿到
正序:
package com.mk.mapreduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.net.URI;
public class Sort {
public static class SortMapper extends Mapper {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
IntWritable v = new IntWritable(Integer.parseInt(value.toString().trim()));
context.write(v, new IntWritable(1));
}
}
public static class SortReducer extends Reducer {
int count = 1;
@Override
protected void reduce(IntWritable key, Iterable values, Context context) throws IOException, InterruptedException {
for (IntWritable v: values) {
context.write(new IntWritable(count ++), key);
}
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String uri = "hdfs://192.168.150.128:9000";
String input = "/sort/input";
String output = "/sort/output";
Configuration conf = new Configuration();
if(System.getProperty("os.name").toLowerCase().contains("win"))
conf.set("mapreduce.app-submission.cross-platform","true");
FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);
Path path = new Path(output);
fileSystem.delete(path,true);
Job job = new Job(conf,"Sort");
job.setJar("./out/artifacts/hadoop_test_jar/hadoop-test.jar");
job.setJarByClass(Sort.class);
job.setMapperClass(SortMapper.class);
job.setReducerClass(SortReducer.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPaths(job, uri + input);
FileOutputFormat.setOutputPath(job, new Path(uri + output));
boolean ret = job.waitForCompletion(true);
System.out.println(job.getJobName() + "-----" +ret);
}
}
逆序:
package com.mk.mapreduce;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.*;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import java.io.IOException;
import java.net.URI;
public class Sort {
public static class SortMapper extends Mapper {
@Override
protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
IntWritable v = new IntWritable(Integer.parseInt(value.toString().trim()));
context.write(v, new IntWritable(1));
}
}
public static class SortReducer extends Reducer {
int count = 1;
@Override
protected void reduce(IntWritable key, Iterable values, Context context) throws IOException, InterruptedException {
for (IntWritable v: values) {
context.write(new IntWritable(count ++), key);
}
}
}
public static class SortComparator implements RawComparator {
@Override
public int compare(byte[] b1, int s1, int l1, byte[] b2, int s2, int l2) {
return IntWritable.Comparator.compareBytes(b2, s2, l2, b1, s1, l1);
}
@Override
public int compare(IntWritable o1, IntWritable o2) {
return o2.get() - o1.get();
}
}
public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
String uri = "hdfs://192.168.150.128:9000";
String input = "/sort/input";
String output = "/sort/output";
Configuration conf = new Configuration();
if(System.getProperty("os.name").toLowerCase().contains("win"))
conf.set("mapreduce.app-submission.cross-platform","true");
FileSystem fileSystem = FileSystem.get(URI.create(uri), conf);
Path path = new Path(output);
fileSystem.delete(path,true);
Job job = new Job(conf,"Sort");
job.setJar("./out/artifacts/hadoop_test_jar/hadoop-test.jar");
job.setJarByClass(Sort.class);
job.setMapperClass(SortMapper.class);
job.setReducerClass(SortReducer.class);
job.setMapOutputKeyClass(IntWritable.class);
job.setMapOutputValueClass(IntWritable.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPaths(job, uri + input);
FileOutputFormat.setOutputPath(job, new Path(uri + output));
job.setSortComparatorClass(SortComparator.class);
boolean ret = job.waitForCompletion(true);
System.out.println(job.getJobName() + "-----" +ret);
}
}