大数据去重

实验 4 大数据去重

1.实验目的

通过Hadoop数据去重实验,学生可以掌握准备数据、伪分布式文件系统配置方法,以及在集成开发环境Eclipse中实现Hadoop数据去重方法。

2.实验要求

了解基于Hadoop处理平台的大数据去重过程,理解其主要功能,并能够在Hadoop环境下独立完成。

(1)制订实验计划。

(2)准备数据。

(3)伪分布式文件系统配置。

(4)在集成开发环境Eclipse中实现Hadoop数据去重。

3.实验内容

(1)制订实验计划。

(2)进入"/usr/local/hadoop"目录。

(3)准备数据。

(4)修改"/usr/local/hadoop/etc/hadoop/"目录下的Hadoop配置文件。

(5)NameNode格式化。

(6)在集成开发环境Eclipse中实现Hadoop数据去重。

4.实验总结

通过本实验,使学生了解Hadoop数据去重的特点和过程、理解MapReduce程序的执行过程,掌握NameNode的格式化方法、Hadoop的配置文件的修改和Eclipse开发环境下实现Hadoop数据去重的方法。

5.思考拓展

(1)为什么需要NameNode格式化?说明NameNode格式化方法。

1.清空dfs.name.dir和dfs.name.edits.dir两个目录下的所有文件
2.在目录dfs.name.dir下创建文件:

plain\] view plaincopy {dfs.name.dir}/current/fsimage {dfs.name.dir}/current/fstime {dfs.name.dir}/current/VERSION {dfs.name.dir}/image/fsimage * 1 * 2 * 3 * 4 * 5 3.在目录dfs.name.edits.dir下创建文件: \[plain\] view plaincopy {dfs.name.edits.dir}/current/edits {dfs.name.edits.dir}/current/fstime {dfs.name.edits.dir}/current/VERSION {dfs.name.edits.dir}/image/fsimage (2)为什么需要数据去重?说明Hadoop数据去重的主要优势。 与传统的数据仓库相比,Hadoop 的分布式架构,实现了既能够处理关系型数据库当中的结构化数据,也能够处理例如视频、音频图片等非结构化数据,并且还能根据数据任务的规模和复杂程度,实现轻松的扩展。 所以 Hadoop能处理哪些类型数据?概括点来说,就是传统的结构化数据,文字图片等,以及非结构化的数据,视频、音频等,都能基于Hadoop框架技术得到合理的处理 Hadoop处理大数据,主要通过分布式技术来解决各种类型的数据问题一- 并行化问题:处理数据的应用程序要改造成适合并行的方式;资源分配管理问题:如何有效的管理提交任务的资源,内存、网络、磁盘等; 容错问题:随着机器数量的增加,可靠性如何保证,例如部分机器硬件出错导致不可用,最终结果的完整性和正确性如何保证。 (3)结合MapReduce程序执行过程,说明Hadoop数据去重是离线处理还是在线处理。 ****1. MapReduce 定义**** [Hadoop](https://so.csdn.net/so/search?q=Hadoop&spm=1001.2101.3001.7020 "Hadoop")中的 MapReduce是一个使用简单的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错式并行处理TB级别的数据集 ****2. MapReduce 特点**** [MapReduce](https://so.csdn.net/so/search?q=MapReduce&spm=1001.2101.3001.7020 "MapReduce") 之所以如此受欢迎,它主要有以下几个特点。: ****- MapReduce 易于编程。**** 它简单的实现一些接口,就可以完成一个[分布式](https://so.csdn.net/so/search?q=%E5%88%86%E5%B8%83%E5%BC%8F&spm=1001.2101.3001.7020 "分布式")程序,这个分布式程序可以分布到大量廉价的 PC 机器运行。也就是说你写一个分布式程序,跟写一个简单的串行程序是一模一样的。 就是因为这个特点使得 MapReduce 编程变得非常流行。 ****- 良好的扩展性**** 。当你的计算资源不能得到满足的时候,你可以通过简单的增加机器来扩展它的计算能力。 \*\*- 高容错性。\*\*MapReduce 设计的初衷就是使程序能够部署在廉价的 PC 机器上,这就要求它具有很高的容错性。比如其中一台机器挂了,它可以把上面的计算任务转移到另外一个节点上面上运行,不至于这个任务运行失败,而且这个过程不需要人工参与,而完全是由 hadoop 内部完成的。 - 适合 PB 级以上海量数据的****离线处理**** 。这里加红字体离线处理,说明它适合离线处理而不适合在线处理。比如像毫秒级别的返回一个结果,MapReduce 很难做到。 MapReduce 虽然具有很多的优势,但是它也有不擅长的地方。这里的不擅长不代表它不能做,而是在有些场景下实现的效果差,并不适合 MapReduce 来处理,主要表现在以下几个方面。 ****- 实时计算**** 。MapReduce 无法像 MySQL 一样,在毫秒或者秒级内返回结果。 ****- 流式计算**** 。流式计算的输入数据时动态的,而 MapReduce 的输入[数据集](https://so.csdn.net/so/search?q=%E6%95%B0%E6%8D%AE%E9%9B%86&spm=1001.2101.3001.7020 "数据集")是静态的,不能动态变化。这是因为 MapReduce 自身的设计特点决定了数据源必须是静态的。 ****- DAG(有向图)计算**** 。多个应用程序存在依赖关系,后一个应用程序的输入为前一个的输出。在这种情况下,MapReduce 并不是不能做,而是使用后,每个MapReduce 作业的输出结果都会写入到磁盘,会造成大量的****磁盘IO**** ,导致性能非常的低下。 ****3. MapReduce的架构**** 目前存在两种 MapReduce 实现,分别是 ****• 可独立运行的 MapReduce**** 它由两类服务组成,分别是 JobTracker 和 TaskTraker,其中 JobTracker 存在单点故障问题,本文提到的单点故障实际上是第一种实现中JobTracker的单点故障。 ****• MapReduce On YARN**** 在这种实现中,每个作业独立使用一个作业跟踪器(ApplicationMaster),彼此之间不再相互影响,不存在单点故障问题。 (4)说明在集成开发环境Eclipse中实现Hadoop数据去重的主要过程。 ### ******一、MapReduce 模型简介****** MapReduce 将复杂的、运行于大规模集群上的并行计算过程高度地抽象到了两个函数:Map 和 Reduce 。它采用 " 分而治之 " 策略,一个存储在分布式文件系统中的大规模数据集,会被切分成许多独立的分片(split ),这些分片可以被多个 Map 任务并行处理。 #### ******1.Map 和 Reduce 函数****** ![](https://img-home.csdnimg.cn/images/20230724024159.png) #### ******2.MapReduce 体系结构****** MapReduce 体系结构主要由四个部分组成,分别是: Client 、 JobTracker、 TaskTracker 以及 Task ![](https://img-home.csdnimg.cn/images/20230724024159.png) ****1)Client****   用户编写的[MapReduce](https://so.csdn.net/so/search?q=MapReduce&spm=1001.2101.3001.7020 "MapReduce")程序通过Client提交到JobTracker端 用户可通过Client提供的一些接口查看作业运行状态 ****2)JobTracker**** JobTracker负责资源监控和作业调度 JobTracker 监控所有TaskTracker与Job的健康状况,一旦发现失败,就将相应的任务转移到其他节点 JobTracker 会跟踪任务的执行进度、资源使用量等信息,并将这些信息告诉任务调度器(TaskScheduler),而调度器会在资源出现空闲时,选择合适的任务去使用这些资源 ****3)TaskTracker**** TaskTracker 会周期性地通过"心跳"将本节点上资源的使用情况和任务的运行进度汇报给JobTracker,同时接收JobTracker 发送过来的命令并执行相应的操作(如启动新任务、杀死任务等) TaskTracker 使用"slot"等量划分本节点上的资源量(CPU、内存等)。一个Task 获取到一个slot 后才有机会运行,而Hadoop调度器的作用就是将各个TaskTracker上的空闲slot分配给Task使用。slot 分为Map slot 和[Reduce](https://so.csdn.net/so/search?q=Reduce&spm=1001.2101.3001.7020 "Reduce") slot 两种,分别供MapTask 和Reduce Task 使用 ****4)Task**** Task 分为Map Task 和Reduce Task 两种,均由TaskTracker 启动 #### ******3.MapReduce 工作流程****** ****1) 工作流程概述**** ![](https://img-home.csdnimg.cn/images/20230724024159.png) * 不同的Map任务之间不会进行通信 * 不同的Reduce任务之间也不会发生任何信息交换 * 用户不能显式地从一台机器向另一台机器发送消息 * 所有的数据交换都是通过MapReduce框架自身去实现的 ****2) MapReduce各个执行阶段**** ![](https://img-home.csdnimg.cn/images/20230724024159.png) #### ******4.MapReduce 应用程序执行过程****** ![](https://img-home.csdnimg.cn/images/20230724024159.png) *** ** * ** *** ### ******二、MapReduce 实战****** #### ******1.数据去重****** "数据去重"主要是为了掌握和利用并行化思想来对数据进行有意义的筛选。统计大数据集上的数据种类个数、从网站日志中计算访问地等这些看似庞杂的任务都会涉及数据去重。 ****1.1实例描述**** 对数据文件中的数据进行去重。数据文件中的每行都是一个数据。样例输入如下所示: 1)file1: 2012-3-1 a 2012-3-2 b 2012-3-3 c 2012-3-4 d 2012-3-5 a 2012-3-6 b 2012-3-7 c 2012-3-3 c 2)file2: 2012-3-1 b 2012-3-2 a 2012-3-3 b 2012-3-4 d 2012-3-5 a 2012-3-6 c 2012-3-7 d 2012-3-3 c 样例输出如下所示: 2012-3-1 a 2012-3-1 b 2012-3-2 a 2012-3-2 b 2012-3-3 b 2012-3-3 c 2012-3-4 d 2012-3-5 a 2012-3-6 b 2012-3-6 c 2012-3-7 c ****1.2 解题思路**** ****map阶段**** :将每一行的文本作为键值对的key ![](https://img-home.csdnimg.cn/images/20230724024159.png) ****reduce阶段**** :将每一个公用的键组输出 ![](https://img-home.csdnimg.cn/images/20230724024159.png) ****1.3 代码展示**** ![](https://img-home.csdnimg.cn/images/20230724024159.png) 1. package datadeduplicate.pers.xls.datadeduplicate; 1. 2. 1. 2. import java.io.IOException; 1. 2. import org.apache.hadoop.conf.Configuration; 1. 2. import org.apache.hadoop.fs.Path; 1. 2. import org.apache.hadoop.io.Text; 1. 2. import org.apache.hadoop.mapreduce.Job; 1. 2. import org.apache.hadoop.mapreduce.Mapper; 1. 2. import org.apache.hadoop.mapreduce.Reducer; 1. 2. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 1. 2. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat; 1. 2. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 1. 2. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat; 1. 2. import org.apache.log4j.BasicConfigurator; 1. 2. 1. 2. public class Deduplication { 1. 2. public static void main(String\[\] args) throws Exception { 1. 2. BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境 1. 2. //必须要传递的是自定的mapper和reducer的类,输入输出的路径必须指定,输出的类型\必须指定 1. 2. //1首先寫job,知道需要conf和jobname在去創建即可 1. 2. Configuration conf=new Configuration(); 1. 2. String jobName=Deduplication.class.getSimpleName(); 1. 2. Job job = Job.getInstance(conf, jobName); 1. 2. //2将自定义的MyMapper和MyReducer组装在一起 1. 2. //3读取HDFS內容:FileInputFormat在mapreduce.lib包下 1. 2. FileInputFormat.setInputPaths(job, new Path(args\[0\])); 1. 2. //4指定解析\的类(谁来解析键值对) 1. 2. //\*指定解析的类可以省略不写,因为设置解析类默认的就是TextInputFormat.class 1. 2. job.setInputFormatClass(TextInputFormat.class); 1. 2. //5指定自定义mapper类 1. 2. job.setMapperClass(MyMapper.class); 1. 2. //6指定map输出的key2的类型和value2的类型 \ 1. 2. //\*下面两步可以省略,当\和\类型一致的时候,\类型可以不指定 1. 2. job.setMapOutputKeyClass(Text.class); 1. 2. job.setMapOutputValueClass(Text.class); 1. 2. //7分区(默认1个),排序,分组,规约 采用 默认 1. 2. job.setCombinerClass(MyReducer.class); 1. 2. //接下来采用reduce步骤 1. 2. //8指定自定义的reduce类 1. 2. job.setReducerClass(MyReducer.class); 1. 2. //9指定输出的\类型 1. 2. job.setOutputKeyClass(Text.class); 1. 2. job.setOutputValueClass(Text.class); 1. 2. //10指定输出\的类 1. 2. //\*下面这一步可以省 1. 2. job.setOutputFormatClass(TextOutputFormat.class); 1. 2. //11指定输出路径 1. 2. FileOutputFormat.setOutputPath(job, new Path(args\[1\])); 1. 2. //12写的mapreduce程序要交给resource manager运行 1. 2. job.waitForCompletion(true); 1. 2. //\*13最后,如果要打包运行改程序,则需要调用如下行 1. 2. job.setJarByClass(Deduplication.class); 1. 2. } 1. 2. private static class MyMapper extends Mapper\{ 1. 2. private static Text line=new Text(); 1. 2. @Override 1. 2. protected void map(Object k1, Text v1,Mapper\.Context context) throws IOException, InterruptedException { 1. 2. line=v1;//v1为每行数据,赋值给line 1. 2. context.write(line, new Text("")); 1. 2. } 1. 2. } 1. 2. private static class MyReducer extends Reducer\ 1. 2. { 1. 2. @Override 1. 2. protected void reduce(Text k2, Iterable\ v2s,Reducer\.Context context) throws IOException, InterruptedException { 1. 2. context.write(k2, new Text("")); 1. 2. } 1. 2. } 1. 2. } 1. ****1.4 运行结果展示**** 打包项目成可运行的jar包,上传的hdfs文件系统: ![](https://img-home.csdnimg.cn/images/20230724024159.png) 在linux系统下终端输入hadoop命令,在建立的hadoop节点上运行jar包: ![](https://img-home.csdnimg.cn/images/20230724024159.png) 查看eclipse中hdfs文件系统下out文件夹,发现生成了先前指定的deduplication文件夹,其中part-r-00000为运行的输出。 ![](https://img-home.csdnimg.cn/images/20230724024159.png) #### **2.数据排序** ![](https://img-home.csdnimg.cn/images/20230724024159.png) ![](https://img-home.csdnimg.cn/images/20230724024159.png) 1. package dararank.pers.xls.datarank; 1. 2. 1. 2. import org.apache.hadoop.conf.Configuration; 1. 2. import org.apache.hadoop.fs.Path; 1. 2. import org.apache.hadoop.io.IntWritable; 1. 2. import org.apache.hadoop.io.Text; 1. 2. import org.apache.hadoop.mapreduce.Job; 1. 2. import org.apache.hadoop.mapreduce.Mapper; 1. 2. import org.apache.hadoop.mapreduce.Reducer; 1. 2. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 1. 2. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 1. 2. import org.apache.hadoop.util.GenericOptionsParser; 1. 2. import org.apache.log4j.BasicConfigurator; 1. 2. import java.io.IOException; 1. 2. 1. 2. public class DataRank { 1. 2. /\*\* 1. 2. \* 使用Mapper将数据文件中的数据本身作为Mapper输出的key直接输出 1. 2. \*/ 1. 2. public static class forSortedMapper extends Mapper\ { 1. 2. private IntWritable mapperValue = new IntWritable(); //存放key的值 1. 2. public void map(Object key, Text value, Context context) 1. 2. throws IOException, InterruptedException { 1. 2. String line = value.toString(); //获取读取的值,转化为String 1. 2. mapperValue.set(Integer.parseInt(line)); //将String转化为Int类型 1. 2. context.write(mapperValue,new IntWritable(1)); //将每一条记录标记为(key,value) key--数字 value--出现的次数 1. 2. //每出现一次就标记为(number,1) 1. 2. } 1. 2. } 1. 2. 1. 2. /\*\* 1. 2. \* 使用Reducer将输入的key本身作为key直接输出 1. 2. \*/ 1. 2. public static class forSortedReducer extends Reducer\{ 1. 2. private IntWritable postion = new IntWritable(1); //存放名次 1. 2. @Override 1. 2. protected void reduce(IntWritable key, Iterable\ values, Context context) throws IOException, InterruptedException { 1. 2. for (IntWritable item :values){ //同一个数字可能出多次,就要多次并列排序 1. 2. context.write(postion,key); //写入名次和具体数字 1. 2. System.out.println(postion + "\\t"+ key); 1. 2. postion = new IntWritable(postion.get()+1); //名次加1 1. 2. } 1. 2. } 1. 2. } 1. 2. 1. 2. 1. 2. public static void main(String\[\] args) throws Exception { 1. 2. 1. 2. BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境 1. 2. 1. 2. Configuration conf = new Configuration(); //设置MapReduce的配置 1. 2. String\[\] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs(); 1. 2. if(otherArgs.length \< 2){ 1. 2. System.out.println("Usage: datarank \ \[\...\] \"); 1. 2. System.exit(2); 1. 2. } 1. 2. //设置作业 1. 2. //Job job = new Job(conf); 1. 2. Job job = Job.getInstance(conf); 1. 2. job.setJarByClass(DataRank.class); 1. 2. job.setJobName("DataRank"); 1. 2. //设置处理map,reduce的类 1. 2. job.setMapperClass(forSortedMapper.class); 1. 2. job.setReducerClass(forSortedReducer.class); 1. 2. //设置输入输出格式的处理 1. 2. job.setOutputKeyClass(IntWritable.class); 1. 2. job.setOutputValueClass(IntWritable.class); 1. 2. //设定输入输出路径 1. 2. for (int i = 0; i \< otherArgs.length-1;++i){ 1. 2. FileInputFormat.addInputPath(job,new Path(otherArgs\[i\])); 1. 2. } 1. 2. FileOutputFormat.setOutputPath(job, new Path(otherArgs\[otherArgs.length-1\])); 1. 2. System.exit(job.waitForCompletion(true)?0:1); 1. 2. } 1. 2. 1. 2. } 1. ![](https://img-home.csdnimg.cn/images/20230724024159.png) #### **3.平均成绩** ![](https://img-home.csdnimg.cn/images/20230724024159.png) 1. package averagescoreapp.pers.xls.averagescoreapp; 1. 2. 1. 2. import java.io.IOException; 1. 2. import java.util.StringTokenizer; 1. 2. import org.apache.hadoop.conf.Configuration; 1. 2. import org.apache.hadoop.fs.Path; 1. 2. import org.apache.hadoop.io.DoubleWritable; 1. 2. import org.apache.hadoop.io.IntWritable; 1. 2. import org.apache.hadoop.io.Text; 1. 2. import org.apache.hadoop.mapreduce.Job; 1. 2. import org.apache.hadoop.mapreduce.Mapper; 1. 2. import org.apache.hadoop.mapreduce.Reducer; 1. 2. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 1. 2. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 1. 2. import org.apache.hadoop.util.GenericOptionsParser; 1. 2. import org.apache.log4j.BasicConfigurator; 1. 2. 1. 2. /\*\* 1. 2. \* 求平均成绩 1. 2. \* 1. 2. \*/ 1. 2. public class AverageScoreApp { 1. 2. 1. 2. public static class Map extends Mapper\{ 1. 2. @Override 1. 2. protected void map(Object key, Text value, Mapper\.Context context) throws IOException, InterruptedException { 1. 2. //成绩的结构是: 1. 2. // 张三 80 1. 2. // 李四 82 1. 2. // 王五 86 1. 2. StringTokenizer tokenizer = new StringTokenizer(value.toString(), "\\n"); 1. 2. while(tokenizer.hasMoreElements()) { 1. 2. StringTokenizer lineTokenizer = new StringTokenizer(tokenizer.nextToken()); 1. 2. String name = lineTokenizer.nextToken(); //姓名 1. 2. String score = lineTokenizer.nextToken();//成绩 1. 2. context.write(new Text(name), new IntWritable(Integer.parseInt(score))); 1. 2. } 1. 2. } 1. 2. } 1. 2. 1. 2. public static class Reduce extends Reducer\{ 1. 2. @Override 1. 2. protected void reduce(Text key, Iterable\ values,Reducer\.Context context) 1. 2. throws IOException, InterruptedException { 1. 2. //reduce这里输入的数据结构是: 1. 2. // 张三 \<80,85,90\> 1. 2. // 李四 \<82,88,94\> 1. 2. // 王五 \<86,80,92\> 1. 2. int sum = 0;//所有课程成绩总分 1. 2. double average = 0;//平均成绩 1. 2. int courseNum = 0; //课程数目 1. 2. for(IntWritable score:values) { 1. 2. sum += score.get(); 1. 2. courseNum++; 1. 2. } 1. 2. average = sum/courseNum; 1. 2. context.write(new Text(key), new DoubleWritable(average)); 1. 2. } 1. 2. } 1. 2. 1. 2. public static void main(String\[\] args) throws Exception{ 1. 2. BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境 1. 2. Configuration conf = new Configuration(); 1. 2. String\[\] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs(); 1. 2. if(otherArgs.length \< 2){ 1. 2. System.out.println("Usage: AverageScoreRank \ \[\...\] \"); 1. 2. System.exit(2); 1. 2. } 1. 2. Job job = Job.getInstance(conf); 1. 2. job.setJarByClass(AverageScoreApp.class); 1. 2. job.setMapperClass(Map.class); 1. 2. job.setReducerClass(Reduce.class); 1. 2. 1. 2. job.setMapOutputKeyClass(Text.class); 1. 2. job.setMapOutputValueClass(IntWritable.class); 1. 2. 1. 2. job.setOutputKeyClass(Text.class); 1. 2. job.setOutputValueClass(DoubleWritable.class); 1. 2. 1. 2. //设定输入输出路径 1. 2. for (int i = 0; i \< otherArgs.length-1;++i){ 1. 2. FileInputFormat.addInputPath(job,new Path(otherArgs\[i\])); 1. 2. } 1. 2. FileOutputFormat.setOutputPath(job, new Path(otherArgs\[otherArgs.length-1\])); 1. 2. 1. 2. System.exit(job.waitForCompletion(true)?0:1); 1. 2. } 1. 2. 1. 2. } 1. ![](https://img-home.csdnimg.cn/images/20230724024159.png) ![](https://img-home.csdnimg.cn/images/20230724024159.png) #### **4.单表关联** ![](https://img-home.csdnimg.cn/images/20230724024159.png) 1. package singletabblerelation.pers.xls.singletablerelation; 1. 2. 1. 2. import java.io.IOException; 1. 2. import java.util.ArrayList; 1. 2. import java.util.List; 1. 2. import java.util.StringTokenizer; 1. 2. import org.apache.hadoop.conf.Configuration; 1. 2. import org.apache.hadoop.fs.Path; 1. 2. import org.apache.hadoop.io.LongWritable; 1. 2. import org.apache.hadoop.io.Text; 1. 2. import org.apache.hadoop.mapreduce.Job; 1. 2. import org.apache.hadoop.mapreduce.Mapper; 1. 2. import org.apache.hadoop.mapreduce.Reducer; 1. 2. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; 1. 2. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; 1. 2. import org.apache.hadoop.util.GenericOptionsParser; 1. 2. import org.apache.log4j.BasicConfigurator; 1. 2. 1. 2. public class SingleTableRelation { 1. 2. public static int time = 0; 1. 2. public static class Map extends Mapper\ { 1. 2. protected void map(LongWritable key, Text value, Context context)throws java.io.IOException, InterruptedException { 1. 2. // 左右表的标识 1. 2. int relation; 1. 2. StringTokenizer tokenizer = new StringTokenizer(value.toString()); 1. 2. String child = tokenizer.nextToken(); 1. 2. String parent = tokenizer.nextToken(); 1. 2. if (child.compareTo("child") != 0) { 1. 2. // 左表 1. 2. relation = 1; 1. 2. context.write(new Text(parent), new Text(relation + "+" + child)); 1. 2. // 右表 1. 2. relation = 2; 1. 2. context.write(new Text(child), new Text(relation + "+" + parent)); 1. 2. } 1. 2. }; 1. 2. 1. 2. } 1. 2. 1. 2. public static class Reduce extends Reducer\ { 1. 2. protected void reduce(Text key, Iterable\ values, 1. 2. Reducer\.Context output) 1. 2. throws java.io.IOException, InterruptedException { 1. 2. int grandchildnum = 0; 1. 2. int grandparentnum = 0; 1. 2. List\ grandchilds = new ArrayList\<\>(); 1. 2. List\ grandparents = new ArrayList\<\>(); 1. 2. /\*\* 输出表头 \*/ 1. 2. if (time == 0) { 1. 2. output.write(new Text("grandchild"), new Text("grandparent")); 1. 2. time++; 1. 2. } 1. 2. for (Text val : values) { 1. 2. String record = val.toString(); 1. 2. char relation = record.charAt(0); 1. 2. // 取出此时key所对应的child 1. 2. if (relation == '1') { 1. 2. String child = record.substring(2); 1. 2. grandchilds.add(child); 1. 2. grandchildnum++; 1. 2. } 1. 2. // 取出此时key所对应的parent 1. 2. else { 1. 2. String parent = record.substring(2); 1. 2. grandparents.add(parent); 1. 2. grandparentnum++; 1. 2. } 1. 2. } 1. 2. if (grandchildnum != 0 \&\& grandparentnum != 0) { 1. 2. for (int i = 0; i \< grandchildnum; i++) 1. 2. for (int j = 0; j \< grandparentnum; j++) 1. 2. output.write(new Text(grandchilds.get(i)), new Text( 1. 2. grandparents.get(j))); 1. 2. } 1. 2. 1. 2. } 1. 2. } 1. 2. 1. 2. public static void main(String\[\] args) throws IOException, ClassNotFoundException, InterruptedException { 1. 2. BasicConfigurator.configure(); //自动快速地使用缺省Log4j环境 1. 2. //必须要传递的是自定的mapper和reducer的类,输入输出的路径必须指定,输出的类型\必须指定 1. 2. //2将自定义的MyMapper和MyReducer组装在一起 1. 2. Configuration conf=new Configuration(); 1. 2. String\[\] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs(); 1. 2. if(otherArgs.length \< 2){ 1. 2. System.out.println("Usage: SingleTableRelation \ \[\...\] \"); 1. 2. System.exit(2); 1. 2. } 1. 2. String jobName=SingleTableRelation.class.getSimpleName(); 1. 2. //1首先寫job,知道需要conf和jobname在去創建即可 1. 2. Job job = Job.getInstance(conf, jobName); 1. 2. job.setJarByClass(SingleTableRelation.class); 1. 2. job.setMapperClass(Map.class); 1. 2. job.setReducerClass(Reduce.class); 1. 2. job.setOutputKeyClass(Text.class); 1. 2. job.setOutputValueClass(Text.class); 1. 2. //设定输入输出路径 1. 2. for (int i = 0; i \< otherArgs.length-1;++i){ 1. 2. FileInputFormat.addInputPath(job,new Path(otherArgs\[i\])); 1. 2. } 1. 2. FileOutputFormat.setOutputPath(job, new Path(otherArgs\[otherArgs.length-1\])); 1. 2. System.exit((job.waitForCompletion(true) ? 0 : 1)); 1. 2. } 1. 2. } 1. ![](https://img-home.csdnimg.cn/images/20230724024159.png) ![](https://img-home.csdnimg.cn/images/20230724024159.png) *** ** * ** *** ### ******三、总结****** hadoop 是一个分布式的基础架构,利用分布式实现高效的计算与储存,最核心的设计在于 HDFS 与 MapReduce 。 HDFS 在集群上实现了分布式文件系统, MapReduce 则在集群上实现了分布式计算和任务处理。HDFS 在 MapReduce 任务处理过程中提供了对文件操作和存储等的支持。而MapReduce在 HDFS 的基础上实现任务的分发、跟踪和执行等工作,并收集结果,两种相互作用,完成了 Hadoop 分布式集群的主要任务。 通过这四个实战的题目我进一步掌握了 Hadoop 架构在现实生活中的应用。

相关推荐
王景程6 分钟前
如何测试短信接口
java·服务器·前端
安冬的码畜日常29 分钟前
【AI 加持下的 Python 编程实战 2_10】DIY 拓展:从扫雷小游戏开发再探问题分解与 AI 代码调试能力(中)
开发语言·前端·人工智能·ai·扫雷游戏·ai辅助编程·辅助编程
小杨升级打怪中34 分钟前
前端面经-JS篇(三)--事件、性能优化、防抖与节流
前端·javascript·xss
清风细雨_林木木38 分钟前
Vue开发网站会有“#”原因是前端路由使用了 Hash 模式
前端·vue.js·哈希算法
鸿蒙布道师1 小时前
OpenAI为何觊觎Chrome?AI时代浏览器争夺战背后的深层逻辑
前端·人工智能·chrome·深度学习·opencv·自然语言处理·chatgpt
袈裟和尚1 小时前
如何在安卓平板上下载安装Google Chrome【轻松安装】
前端·chrome·电脑
曹牧1 小时前
HTML字符实体和转义字符串
前端·html
小希爸爸1 小时前
2、中医基础入门和养生
前端·后端
局外人LZ1 小时前
前端项目搭建集锦:vite、vue、react、antd、vant、ts、sass、eslint、prettier、浏览器扩展,开箱即用,附带项目搭建教程
前端·vue.js·react.js
G_GreenHand2 小时前
Dhtmlx Gantt教程
前端