azure011328

导航

< 2025年3月 >
23 24 25 26 27 28 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31 1 2 3 4 5
统计
 

实验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. <dependency>  
  2.             <groupId>junit</groupId>  
  3.             <artifactId>junit</artifactId>  
  4.             <version>3.8.1</version>  
  5.             <scope>test</scope>  
  6.         </dependency>  
  7.   
  8.         <dependency>  
  9.             <groupId>org.apache.hadoop</groupId>  
  10.             <artifactId>hadoop-common</artifactId>  
  11.             <version>2.8.5</version>  
  12.         </dependency>  
  13.         <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->  
  14.         <dependency>  
  15.             <groupId>org.apache.hadoop</groupId>  
  16.             <artifactId>hadoop-hdfs</artifactId>  
  17.             <version>2.8.5</version>  
  18.         </dependency>  
  19.         <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-mapreduce-client-core -->  
  20.         <dependency>  
  21.             <groupId>org.apache.hadoop</groupId>  
  22.             <artifactId>hadoop-mapreduce-client-core</artifactId>  
  23.             <version>2.8.5</version>  
  24.         </dependency>  
  25.         <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-client -->  
  26.         <dependency>  
  27.             <groupId>org.apache.hadoop</groupId>  
  28.             <artifactId>hadoop-client</artifactId>  
  29.             <version>2.8.5</version>  
  30.         </dependency>  
  31.         <!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-yarn-api -->  
  32.         <dependency>  
  33.             <groupId>org.apache.hadoop</groupId>  
  34.             <artifactId>hadoop-yarn-api</artifactId>  
  35.             <version>2.8.5</version>  
  36.         </dependency>  
  37.         <!--ssh2架包-->  
  38.         <dependency>  
  39.             <groupId>ch.ethz.ganymed</groupId>  
  40.             <artifactId>ganymed-ssh2</artifactId>  
  41.             <version>build210</version>  
  42.         </dependency>  
  43.         <dependency>  
  44.             <groupId>log4j</groupId>  
  45.             <artifactId>log4j</artifactId>  
  46.             <version>1.2.17</version>  
  47.         </dependency>  
  48.         <dependency>  
  49.             <groupId>org.apache.hbase</groupId>  
  50.             <artifactId>hbase-client</artifactId>  
  51.             <version>2.1.0</version>  
  52.         </dependency>  

 

  1. import java.io.IOException;  
  2.   
  3. import org.apache.hadoop.conf.Configuration;  
  4. import org.apache.hadoop.fs.Path;  
  5. import org.apache.hadoop.io.IntWritable;  
  6. import org.apache.hadoop.io.Text;  
  7. import org.apache.hadoop.mapreduce.Job;  
  8. import org.apache.hadoop.mapreduce.Mapper;  
  9. import org.apache.hadoop.mapreduce.Reducer;  
  10. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  11. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  12. import org.apache.hadoop.util.GenericOptionsParser;  
  13.   
  14. public class Merge {  
  15.     //重载map函数,直接将输入中的value复制到输出数据的key上  
  16.     public static class Map extends Mapper<Object, Text, Text, Text>{  
  17.         private static Text text = new Text();  
  18.         public void map(Object key, Text value, Context context) throws IOException,InterruptedException{  
  19.             text = value;  
  20.             context.write(text, new Text(""));  
  21.         }  
  22.     }  
  23.   
  24.     //重载reduce函数,直接将输入中的key复制到输出数据的key上  
  25.     public static class Reduce extends Reducer<Text, Text, Text, Text>{  
  26.         public void reduce(Text key, Iterable<Text> values, Context context ) throws IOException,InterruptedException{  
  27.             context.write(key, new Text(""));  
  28.         }  
  29.     }  
  30.   
  31.     public static void main(String[] args) throws Exception{  
  32.         Configuration conf = new Configuration();  
  33.         conf.set("fs.defaultFS","hdfs://node1:8020");  
  34.         String[] otherArgs = new String[]{"/input","/output"}; /* 直接设置输入参数 */  
  35.         Job job = Job.getInstance(conf,"Merge and duplicate removal");  
  36.         job.setJarByClass(Merge.class);  
  37.         job.setMapperClass(Map.class);  
  38.         job.setCombinerClass(Reduce.class);  
  39.         job.setReducerClass(Reduce.class);  
  40.         job.setOutputKeyClass(Text.class);  
  41.         job.setOutputValueClass(Text.class);  
  42.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  43.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  44.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  45.     }  
  46. }  

 

 

posted on   淮竹i  阅读(3)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!
 
点击右上角即可分享
微信分享提示