12.31日报
完成大型数据库实验五,以下为实验内容:
实验5
MapReduce初级编程实践
1.实验目的
(1)通过实验掌握基本的MapReduce编程方法;
(2)掌握用MapReduce解决一些常见的数据处理问题,包括数据去重、数据排序和数据挖掘等。
2.实验平台
(1)操作系统:Linux(建议Ubuntu16.04或Ubuntu18.04)
(2)Hadoop版本:3.1.3
3.实验步骤
(一)编程实现文件合并和去重操作
对于两个输入文件,即文件A和文件B,请编写MapReduce程序,对两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件C。下面是输入文件和输出文件的一个样例供参考。
输入文件A的样例如下:
20170101 x 20170102 y 20170103 x 20170104 y 20170105 z 20170106 x |
输入文件B的样例如下:
20170101 y 20170102 y 20170103 x 20170104 z 20170105 y |
根据输入文件A和B合并得到的输出文件C的样例如下:
20170101 x 20170101 y 20170102 y 20170103 x 20170104 y 20170104 z 20170105 y 20170105 z 20170106 x |
(二)编写程序实现对输入文件的排序
现在有多个输入文件,每个文件中的每行内容均为一个整数。要求读取所有文件中的整数,进行升序排序后,输出到一个新的文件中,输出的数据格式为每行两个整数,第一个数字为第二个整数的排序位次,第二个整数为原待排列的整数。下面是输入文件和输出文件的一个样例供参考。
输入文件1的样例如下:
33 37 12 40 |
输入文件2的样例如下:
4 16 39 5 |
输入文件3的样例如下:
1 45 25 |
根据输入文件1、2和3得到的输出文件如下:
1 1 2 4 3 5 4 12 5 16 6 25 7 33 8 37 9 39 10 40 11 45 |
(三)对给定的表格进行信息挖掘
下面给出一个child-parent的表格,要求挖掘其中的父子辈关系,给出祖孙辈关系的表格。
输入文件内容如下:
child parent Steven Lucy Steven Jack Jone Lucy Jone Jack Lucy Mary Lucy Frank Jack Alice Jack Jesse David Alice David Jesse Philip David Philip Alma Mark David Mark Alma |
输出文件内容如下:
grandchild grandparent Steven Alice Steven Jesse Jone Alice Jone Jesse Steven Mary Steven Frank Jone Mary Jone Frank Philip Alice Philip Jesse Mark Alice Mark Jesse |
4.实验报告
题目: |
MapReduce初级编程实践 |
姓名 李健龙 |
|
日期 2024/12/15 |
实验环境:Ubuntu 18.04.6 LTS Hadoop 3.1.3 |
||||
实验内容与完成情况: Java: 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.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser;
public class Merge {
/** * @param args * 对A,B两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件C */ //重载map函数,直接将输入中的value复制到输出数据的key上 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(""));//括号内容作为中间结果扔出去交给shuffle处理 } }
//重载reduce函数,直接将输入中的key复制到输出数据的key上 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{
// TODO Auto-generated method stub Configuration conf = new Configuration();//程序运行时的参数 conf.set("fs.default.name","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(Merge.class);//设置整个程序的类名 job.setMapperClass(Map.class);//添加Mapper类 job.setCombinerClass(Reduce.class);//设置Combiner类 job.setReducerClass(Reduce.class);//添加Reducer类 job.setOutputKeyClass(Text.class);//设置输出类型 job.setOutputValueClass(Text.class);//设置输出类型 FileInputFormat.addInputPath(job, new Path(otherArgs[0]));//设置输入原始文件文件路径 FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));//设置输出文件路径 //Job运行是通过job.waitForCompletion(true),true表示将运行进度等信息及时输出给用户,false的话只是等待作业结束 boolean result = job.waitForCompletion(true); System.exit(result ? 0 : 1); }
} java: 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.Reducer; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.GenericOptionsParser;
public class Merge {
/** * @param args * 对A,B两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件C */
// 重载map函数,直接将输入中的value复制到输出数据的key上 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("")); // 括号内容作为中间结果扔出去交给shuffle处理 } }
// 重载reduce函数,直接将输入中的key复制到输出数据的key上 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.default.name", "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(Merge.class); // 设置整个程序的类名 job.setMapperClass(Map.class); // 添加Mapper类 job.setCombinerClass(Reduce.class); // 设置Combiner类 job.setReducerClass(Reduce.class); // 添加Reducer类 job.setOutputKeyClass(Text.class); // 设置输出类型 job.setOutputValueClass(Text.class); // 设置输出类型
// 设置输入文件路径和输出文件路径 FileInputFormat.addInputPath(job, new Path(otherArgs[0])); // 设置输入原始文件文件路径 FileOutputFormat.setOutputPath(job, new Path(otherArgs[1])); // 设置输出文件路径
// 运行Job,true表示实时输出作业的进度等信息,false只等作业结束 boolean result = job.waitForCompletion(true); System.exit(result ? 0 : 1); } }
|
||||
出现的问题:MapReduce程序无法正确读取输入文件 父子辈关系解析错误 |
||||
解决方案(列出遇到的问题和解决办法,列出没有解决的问题):确保输入文件路径正确,并且Hadoop有权限访问这些文件。 确保MapReduce程序正确解析输入文件中的父子关系。 |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现