1.编程实现文件合并和去重操作
对于两个输入文件,即文件 A 和文件 B,请编写 MapReduce 程序,对两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件 C。下面是输入文件和输出文件的一个样例供参考。(输入文件使用 Tab 制表符分割)。 源码文件命名为 TextMerge.java
输入文件 A 的样例如下:
20150101 x
20150102 y
20150103 x
20150104 y
20150105 z
20150106 x
输入文件 B 的样例如下:
20150101 y
20150102 y
20150103 x
20150104 z
20150105 y
根据输入文件 A 和 B 合并得到的输出文件 C 的样例如下。
20150101 x
20150101 y
20150102 y
20150103 x
20150104 y
20150104 z
20150105 y
20150105 z
20150106 x
(1)建立 input 文件夹存放所要处理的文件,如下图所示:
(2)将 TextMerge.java文件所在 dashuju项目打包成 jar,并将其复制到相关文件夹中,如下图所示:
(3)运行 dashuju.jar,将结果存放在新文件夹 output中,如下图所示:
(4)查看运行结果,如下图所示:
程序代码:
import java.io.IOException;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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;
public class TextMerge {
public static class Map extends Mapper<Object,Text,Text,Text>{
private static Text text=new Text();
public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
text=value;
context.write(text,new Text(""));
}
}
public static class Reduce extends Reducer<Text,Text,Text,Text>{
public void reduce(Text key, Iterable<Text> values,Context context)throws IOException,InterruptedException {
context.write(key, new Text(""));
}
}
public static void main(String[] args) throws Exception{
Configuration conf=new Configuration();
conf.set("fs.defaultFS","hdfs://localhost:9000");
String[] otherArgs=new String[]{"input","output"};
if(otherArgs.length != 2){
System.err.println("Usage:wordcount<in><out>");
System.exit(2);
}
Job job=Job.getInstance(conf,"Merge and duplicate removal");
job.setJarByClass(TextMerge.class);
job.setMapperClass(Map.class);
job.setCombinerClass(Reduce.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job,new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true)?0:1);
}
}
2.编程实现对输入文件的排序
现在有多个输入文件,每个文件中的每行内容均为一个整数。要求读取所有文件中的整数,进行升序排序后,输出到一个新的文件中,输出的数据格式为每行两个整数,第一个整数为第二个整数的排序位次,第二个整数为原待排列的整数。下面是输入文件和输出文件的一个样例,供参考。源码文件命名为 NumberSort.java
输入文件 1的样例如下:
33
37
12
40
输入文件 2的样例如下:
4
16
39
5
输入文件 3的样例如下:
1
45
25
根据输入文件 1、2和 3得到的输出文件如下 (输出文件使用 Tab制表符分割):
1 1
2 4
3 5
4 12
5 16
6 25
7 33
8 37
9 39
10 40
11 45
基本步骤同上例1
程序代码:
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;
import java.io.IOException;
public class NumberSort {
public static class Map extends Mapper<Object, Text, IntWritable,IntWritable> {
private static IntWritable data=new IntWritable();
public void map(Object key,Text value,Context context) throws IOException,InterruptedException{
String line=value.toString();
data.set(Integer.parseInt(line));
context.write(data,new IntWritable(1));
}
}
public static class Reduce extends Reducer<IntWritable,IntWritable,IntWritable,IntWritable> {
private static IntWritable linenum=new IntWritable(1);
public void reduce(IntWritable key, Iterable<IntWritable> values,Context context)throws IOException,InterruptedException {
for (IntWritable val:values){
context.write(linenum,key);
linenum=new IntWritable(linenum.get()+1);
}
}
}
public static class Partition extends Partitioner<IntWritable,IntWritable>{
public int getPartition(IntWritable key,IntWritable value,int num_Partition){
int Maxnum=65223;
int bound=Maxnum/num_Partition+1;
int keynum= key.get();
for (int i = 0; i < num_Partition; i++) {
if(keynum<bound*(i+1) && keynum>=bound*i){
return i;
}
}
return -1;
}
}
public static void main(String[] args) throws Exception{
Configuration conf=new Configuration();
conf.set("fs.defaultFS","hdfs://localhost:9000");
String[] otherArgs=new String[]{"input","output"};
if(otherArgs.length != 2){
System.err.println("Usage:wordcount<in><out>");
System.exit(2);
}
Job job=Job.getInstance(conf,"ContentSort");
job.setJarByClass(NumberSort.class);
job.setMapperClass(Map.class);
job.setPartitionerClass(Partition.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(IntWritable.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job,new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job,new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true)?0:1);
}
}