实验十 Hadoop实验——MapReduce编程
- 实验目的
- 通过实验掌握基本的MapReduce编程方法。
- 通过操作MapReduce的实验,模仿实验内容,深入理解MapReduce的过程,熟悉MapReduce程序的编程方式。
- 实验环境
与实验五一致https://mp.csdn.net/editor/html/116616391,其他命令可参考实验六:https://mp.csdn.net/editor/html/116618050实验七https://mp.csdn.net/editor/html/116618050
- 试验内容
- 对于两个输入文件,即文件A和文件B,请编写MapReduce程序,对两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件C。下面是输入文件和输出文件的一个样例供参考。
输入文件f1.txt的样例如下:
20150101 x
20150102 y
20150103 x
20150104 y
20150105 z
20150106 x
输入文件f2.txt的样例如下:
20150101 y
20150102 y
20150103 x
20150104 z
20150105 y
根据输入文件f1和f2合并得到的输出文件的样例如下:
20150101 x
20150101 y
20150102 y
20150103 x
20150104 y
20150104 z
20150105 y
20150105 z
20150106 x
- 实验步骤
- 创建文件f1.txt和f2.txt
vi f1.txt
vi f2.txt
- 在HDFS建立testinput文件夹(执行这步之前要开启hadoop相关进程)
./hadoop fs -mkdir testinput
- 将文件f1.txt和f2.txt上传到HDFS中的testinput文件夹
./hadoop fs -put ~/f*.txt testinput
- 接着打开eclipse,实验环境沿用实验七https://mp.csdn.net/editor/html/116618050
- 新建Merge类,编写如下代码:
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 Merge {
public static class Map extends Mapper<Object, Text, Text, Text> {
private static Text text = new Text();
@Override
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> {
@Override
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[]{"testinput", "testoutput"};
if (otherArgs.length != 2) {
System.err.println("Usage: Merge and duplicate removal <in> <out>");
System.exit(2);
}
Job job = Job.getInstance(conf, "Merge");
job.setJarByClass(Merge.class);
job.setMapperClass(Map.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);
}
}
五、实验结果与分析
1)请给出实验代码中红色标记的代码每一行注释此行代码功能。
//实例化Merge类
Job job = Job.getInstance(conf, "Merge");
//设置主类名
job.setJarByClass(Merge.class);
//指定使用上述代码自定义的Map类
job.setMapperClass(Map.class);
//指定使用上述代码自定义的Reduce类
job.setReducerClass(Reduce.class);
//设定Reduce类输出的<K,V>,K类型
job.setOutputKeyClass(Text.class);
//设定Reduce类输出的<K,V>,V类型
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)运行并分析实验内容程序的运行结果


文件f1.txt与文件f2.txt通过Merge类合并后,实现了预期的效果,通过mapreduce简单实现了两个文件的合并操作
本实验介绍了如何使用Hadoop MapReduce进行文件合并与去重操作。通过编写MapReduce程序,实现了将两个输入文件合并并去除重复内容,生成新的输出文件。实验代码包括Map和Reduce类的实现,详细解释了每个关键代码段的功能,并展示了运行结果,成功完成了文件合并任务。

8999

被折叠的 条评论
为什么被折叠?



