头歌大数据MapReduce基础编程

打印 上一主题 下一主题

主题 537|帖子 537|积分 1611

第1关:归并去重

任务描述

本关任务:编程实现文件归并和去重操纵。
相关知识

为了实现文件的归并去重,我们起首要知道文件中都有些什么,将文件内容举行“拆解”,映射(Map)到不同节点;然后在通过归约(Reduce)去除重复部分。

Map过程
用法如下:
重载map函数,直接将输入中的value复制到输出数据的key上。
  1. public static class Map extends Mapper<Object, Text, Text, Text>{}
复制代码
Reduce过程
重载reduce函数,直接将输入中的key复制到输出数据的key上。
  1. public static class Reduce extends Reducer<Text, Text, Text, Text>{}
复制代码
编程要求

对于两个输入文件,即文件A和文件B,请编写MapReduce程序,对两个文件举行归并,并剔除此中重复的内容,得到一个新的输出文件C。
为了完成文件归并去重的任务,你编写的程序要能将含有重复内容的不同文件归并到一个没有重复的整合文件,规则如下:

  • 第一列按学号排列;
  • 学号相同,按x,y,z排列。
测试说明

程序会对你编写的代码举行测试:
输入已经指定了测试文本数据:需要你的程序输出归并去重后的结果。
下面是输入文件和输出文件的一个样例供参考。
输入文件A的样比方下:
  1. 20150101     x
  2. 20150102     y
  3. 20150103     x
  4. 20150104     y
  5. 20150105     z
  6. 20150106     x
复制代码
输入文件B的样比方下:
  1. 20150101      y
  2. 20150102      y
  3. 20150103      x
  4. 20150104      z
  5. 20150105      y
复制代码
根据输入文件A和B归并得到的输出文件C的样比方下:
  1. 20150101      x
  2. 20150101      y
  3. 20150102      y
  4. 20150103      x
  5. 20150104      y
  6. 20150104      z
  7. 20150105      y
  8. 20150105      z
  9. 20150106      x
复制代码
示例代码

  1. import java.io.IOException;  
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.Path;  
  4. import org.apache.hadoop.io.IntWritable;  
  5. import org.apache.hadoop.io.Text;  
  6. import org.apache.hadoop.mapreduce.Job;  
  7. import org.apache.hadoop.mapreduce.Mapper;  
  8. import org.apache.hadoop.mapreduce.Reducer;  
  9. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  10. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  11. import org.apache.hadoop.util.GenericOptionsParser;
  12. public class Merge {
  13.     /**  
  14.      * @param args  
  15.      * 对A,B两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件C  
  16.      */  
  17.     //重载map函数,直接将输入中的value复制到输出数据的key上  
  18.     /********** Begin **********/  
  19.     public static class Map extends Mapper<Object, Text, Text, Text>{  
  20.         private static Text text = new Text();  
  21.         public void map(Object key, Text value, Context context) throws IOException,InterruptedException{  
  22.             text = value;  
  23.             context.write(text, new Text(""));  
  24.         }  
  25.     }  
  26.     /********** End **********/  
  27.     //重载reduce函数,直接将输入中的key复制到输出数据的key上  
  28.     /********** Begin **********/  
  29.     public static class Reduce extends Reducer<Text, Text, Text, Text>{  
  30.         public void reduce(Text key, Iterable<Text> values, Context context ) throws IOException,InterruptedException{  
  31.             context.write(key, new Text(""));  
  32.         }  
  33.     }  
  34.     /********** End **********/  
  35.     public static void main(String[] args) throws Exception{  
  36.         // TODO Auto-generated method stub  
  37.         Configuration conf = new Configuration();  
  38.         conf.set("fs.default.name","hdfs://localhost:9000");  
  39.         String[] otherArgs = new String[]{"input","output"}; /* 直接设置输入参数 */  
  40.         if (otherArgs.length != 2) {  
  41.             System.err.println("Usage: wordcount <in> <out>");  
  42.             System.exit(2);  
  43.             }  
  44.         Job job = Job.getInstance(conf,"Merge and duplicate removal");  
  45.         job.setJarByClass(Merge.class);  
  46.         job.setMapperClass(Map.class);  
  47.         job.setCombinerClass(Reduce.class);  
  48.         job.setReducerClass(Reduce.class);  
  49.         job.setOutputKeyClass(Text.class);  
  50.         job.setOutputValueClass(Text.class);  
  51.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  52.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  53.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  54.     }
  55. }
复制代码
第1关:第2关:整合排序

任务描述

本关任务:编写程序实现对输入文件的排序。
相关知识

为了实现文件的整合排序,我们起首要知道文件中都有些什么,将文件内容举行“拆解”,映射(Map)到不同节点;然后在通过归约(Reduce)的过程中举行排序输出。

Map过程

  • 输入多个文件,每个文件中的每行内容均为一个整数;
  • map函数读取输入中的value,将其转化成IntWritable类型,末了作为输出key。
  1. public static class Map extends Mapper<Object, Text, IntWritable, IntWritable>{
  2. private static IntWritable data = new IntWritable();
  3. }
复制代码
Reduce过程

  • reduce函数将map输入的key复制到输出的value上;
  • 根据输入的value-list中元素的个数决定key的输出次数,定义一个全局变量line_num来代表key的位次。
  1. public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{
  2. private static IntWritable line_num = new IntWritable(1);
  3. }
复制代码
返回Partiton ID

  • 自定义Partition函数,此函数根据输入数据的最大值和MapReduce框架中Partition的数量获取将输入数据按照巨细分块的边界;
  • 根据输入数值和边界的关系返回对应的Partiton ID。
  1. public static class Partition extends Partitioner<IntWritable, IntWritable>{
  2. public int getPartition(IntWritable key, IntWritable value, int num_Partition){
  3. int Maxnumber = 65223;//int型的最大数值
  4. }
复制代码
编程要求

为了完成归并文件并排序的功能,你编写的程序要能将整合排序后的结果输出,规则如下:

  • 现在有多个输入文件,每个文件中的每行内容均为一个整数。要求读取所有文件中的整数,举行升序排序后,输出到一个新的文件中;
  • 输出的数据格式为每行两个整数,第一个数字为第二个整数的排序位次,第二个整数为原待排列的整数。
测试说明

程序会对你编写的代码举行测试:
下面是输入文件和输出文件的一个样例供参考。
输入文件1的样比方下:
  1. 33
  2. 37
  3. 12
  4. 40
复制代码
输入文件2的样比方下:
  1. 4
  2. 16
  3. 39
  4. 5
复制代码
输入文件3的样比方下:
  1. 1
  2. 45
  3. 25
复制代码
根据输入文件1、2和3得到的输出文件如下:
  1. 1 1
  2. 2 4
  3. 3 5
  4. 4 12
  5. 5 16
  6. 6 25
  7. 7 33
  8. 8 37
  9. 9 39
  10. 10 40
  11. 11 45
复制代码
示例代码

  1. import java.io.IOException;
  2. import org.apache.hadoop.conf.Configuration;  
  3. import org.apache.hadoop.fs.Path;  
  4. import org.apache.hadoop.io.IntWritable;  
  5. import org.apache.hadoop.io.Text;  
  6. import org.apache.hadoop.mapreduce.Job;  
  7. import org.apache.hadoop.mapreduce.Mapper;  
  8. import org.apache.hadoop.mapreduce.Partitioner;  
  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. public class MergeSort {
  14.     /**  
  15.      * @param args  
  16.      * 输入多个文件,每个文件中的每行内容均为一个整数  
  17.      * 输出到一个新的文件中,输出的数据格式为每行两个整数,第一个数字为第二个整数的排序位次,第二个整数为原待排列的整数  
  18.      */  
  19.     //map函数读取输入中的value,将其转化成IntWritable类型,最后作为输出key  
  20.     public static class Map extends Mapper<Object, Text, IntWritable, IntWritable>{
  21.         private static IntWritable data = new IntWritable();  
  22.         public void map(Object key, Text value, Context context) throws IOException,InterruptedException{  
  23.     /********** Begin **********/  
  24.             String text = value.toString();  
  25.             data.set(Integer.parseInt(text));  
  26.             context.write(data, new IntWritable(1));  
  27.     /********** End **********/  
  28.         }  
  29.     }  
  30.     //reduce函数将map输入的key复制到输出的value上,然后根据输入的value-list中元素的个数决定key的输出次数,定义一个全局变量line_num来代表key的位次  
  31.     public static class Reduce extends Reducer<IntWritable, IntWritable, IntWritable, IntWritable>{
  32.         private static IntWritable line_num = new IntWritable(1);  
  33.         public void reduce(IntWritable key, Iterable<IntWritable> values, Context context) throws IOException,InterruptedException{  
  34.     /********** Begin **********/  
  35.             for(IntWritable val : values){  
  36.                 context.write(line_num, key);  
  37.                 line_num = new IntWritable(line_num.get() + 1);  
  38.             }  
  39.     /********** End **********/  
  40.         }  
  41.     }  
  42.     //自定义Partition函数,此函数根据输入数据的最大值和MapReduce框架中Partition的数量获取将输入数据按照大小分块的边界,然后根据输入数值和边界的关系返回对应的Partiton ID  
  43.     public static class Partition extends Partitioner<IntWritable, IntWritable>{  
  44.         public int getPartition(IntWritable key, IntWritable value, int num_Partition){  
  45.     /********** Begin **********/  
  46.             int Maxnumber = 65223;//int型的最大数值  
  47.             int bound = Maxnumber/num_Partition+1;  
  48.             int keynumber = key.get();  
  49.             for (int i = 0; i<num_Partition; i++){  
  50.                 if(keynumber<bound * (i+1) && keynumber>=bound * i){  
  51.                     return i;  
  52.                 }  
  53.             }  
  54.             return -1;  
  55.     /********** End **********/  
  56.         }  
  57.     }  
  58.     public static void main(String[] args) throws Exception{  
  59.         // TODO Auto-generated method stub  
  60.         Configuration conf = new Configuration();  
  61.         conf.set("fs.default.name","hdfs://localhost:9000");  
  62.         String[] otherArgs = new String[]{"input","output"}; /* 直接设置输入参数 */  
  63.         if (otherArgs.length != 2) {  
  64.             System.err.println("Usage: wordcount <in> <out>");  
  65.             System.exit(2);  
  66.             }  
  67.         Job job = Job.getInstance(conf,"Merge and sort");  
  68.         job.setJarByClass(MergeSort.class);  
  69.         job.setMapperClass(Map.class);  
  70.         job.setReducerClass(Reduce.class);  
  71.         job.setPartitionerClass(Partition.class);  
  72.         job.setOutputKeyClass(IntWritable.class);  
  73.         job.setOutputValueClass(IntWritable.class);  
  74.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  75.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  76.         System.exit(job.waitForCompletion(true) ? 0 : 1);
  77.     }
  78. }
复制代码
第3关:信息发掘

任务描述

本关任务:对给定的表格举行信息发掘。
相关知识

Map过程

  • Map将输入文件按照空格分割成child和parent,然后正序输出一次作为右表,反序输出一次作为左表,需要注意的是在输出的value中必须加上左右表区别标志;
  • 在Map阶段,将父子关系与相反的子父关系,同时在各个value前补上前缀-与+标识此key-value中的value是正序还是逆序产生的,之后进入context。
  1. public static class Map extends Mapper<Object, Text, Text, Text>{
  2.         public void map(Object key, Text value, Context context) throws IOException,InterruptedException{
  3.             String child_name = new String();
  4.             String parent_name = new String();
  5. }
复制代码
Reduce过程
  1. public static class Reduce extends Reducer<Text, Text, Text, Text>{
  2.         public void reduce(Text key, Iterable<Text> values,Context context) throws IOException,InterruptedException{
  3.             if(time == 0){   //输出表头
  4.                 context.write(new Text("grand_child"), new Text("grand_parent"));
  5.                 time++;
  6.             }
  7. }
复制代码
编程要求

为了完成信息发掘的任务,你编写的程序要能发掘父子辈关系,给出祖孙辈关系的表格。规则如下:

  • 孙子在前,祖父在后;
  • 孙子相同,祖父的名字按照A-Z排列。
测试说明

程序会对你编写的代码举行测试:
下面给出一个child-parent的表格,要求发掘此中的父子辈关系,给出祖孙辈关系的表格。
输入文件内容如下:
  1. child          parent
  2. Steven        Lucy
  3. Steven        Jack
  4. Jone         Lucy
  5. Jone         Jack
  6. Lucy         Mary
  7. Lucy         Frank
  8. Jack         Alice
  9. Jack         Jesse
  10. David       Alice
  11. David       Jesse
  12. Philip       David
  13. Philip       Alma
  14. Mark       David
  15. Mark       Alma
复制代码
输出文件内容如下:
  1. grandchild       grandparent
  2. Steven          Alice
  3. Steven          Jesse
  4. Jone            Alice
  5. Jone            Jesse
  6. Steven          Mary
  7. Steven          Frank
  8. Jone            Mary
  9. Jone            Frank
  10. Philip           Alice
  11. Philip           Jesse
  12. Mark           Alice
  13. Mark           Jesse
复制代码
示例代码

  1. import java.io.IOException;  
  2. import java.util.*;
  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. public class simple_data_mining {  
  14.     public static int time = 0;
  15.     /**  
  16.      * @param args  
  17.      * 输入一个child-parent的表格  
  18.      * 输出一个体现grandchild-grandparent关系的表格  
  19.      */  
  20.     //Map将输入文件按照空格分割成child和parent,然后正序输出一次作为右表,反序输出一次作为左表,需要注意的是在输出的value中必须加上左右表区别标志  
  21.     public static class Map extends Mapper<Object, Text, Text, Text>{  
  22.         public void map(Object key, Text value, Context context) throws IOException,InterruptedException{  
  23.     /********** Begin **********/  
  24.             String child_name = new String();  
  25.             String parent_name = new String();  
  26.             String relation_type = new String();  
  27.             String line = value.toString();  
  28.             int i = 0;  
  29.             while(line.charAt(i) != ' '){  
  30.                 i++;  
  31.             }  
  32.             String[] values = {line.substring(0,i),line.substring(i+1)};  
  33.             if(values[0].compareTo("child") != 0){  
  34.                 child_name = values[0];  
  35.                 parent_name = values[1];  
  36.                 relation_type = "1";//左右表区分标志  
  37.                 context.write(new Text(values[1]), new Text(relation_type+"+"+child_name+"+"+parent_name));  
  38.                 //左表  
  39.                 relation_type = "2";  
  40.                 context.write(new Text(values[0]), new Text(relation_type+"+"+child_name+"+"+parent_name));  
  41.                 //右表  
  42.             }  
  43.     /********** End **********/  
  44.         }  
  45.     }  
  46.     public static class Reduce extends Reducer<Text, Text, Text, Text>{  
  47.         public void reduce(Text key, Iterable<Text> values,Context context) throws IOException,InterruptedException{  
  48.     /********** Begin **********/  
  49.             if(time == 0){   //输出表头  
  50.                 context.write(new Text("grand_child"), new Text("grand_parent"));  
  51.                 time++;  
  52.             }  
  53.             int grand_child_num = 0;  
  54.             String grand_child[] = new String[10];  
  55.             int grand_parent_num = 0;  
  56.             String grand_parent[]= new String[10];  
  57.             Iterator ite = values.iterator();  
  58.             while(ite.hasNext()){  
  59.                 String record = ite.next().toString();  
  60.                 int len = record.length();  
  61.                 int i = 2;  
  62.                 if(len == 0) continue;  
  63.                 char relation_type = record.charAt(0);  
  64.                 String child_name = new String();  
  65.                 String parent_name = new String();  
  66.                 //获取value-list中value的child  
  67.                 while(record.charAt(i) != '+'){  
  68.                     child_name = child_name + record.charAt(i);  
  69.                     i++;  
  70.                 }  
  71.                 i=i+1;  
  72.                 //获取value-list中value的parent  
  73.                 while(i<len){  
  74.                     parent_name = parent_name+record.charAt(i);  
  75.                     i++;  
  76.                 }  
  77.                 //左表,取出child放入grand_child  
  78.                 if(relation_type == '1'){  
  79.                     grand_child[grand_child_num] = child_name;  
  80.                     grand_child_num++;  
  81.                 }  
  82.                 else{//右表,取出parent放入grand_parent  
  83.                     grand_parent[grand_parent_num] = parent_name;  
  84.                     grand_parent_num++;  
  85.                 }  
  86.             }
  87.             if(grand_parent_num != 0 && grand_child_num != 0 ){  
  88.                 for(int m = 0;m<grand_child_num;m++){  
  89.                     for(int n=0;n<grand_parent_num;n++){  
  90.                         context.write(new Text(grand_child[m]), new Text(grand_parent[n]));  
  91.                         //输出结果  
  92.                     }  
  93.                 }  
  94.             }  
  95.     /********** End **********/  
  96.         }  
  97.     }  
  98.     public static void main(String[] args) throws Exception{  
  99.         // TODO Auto-generated method stub  
  100.         Configuration conf = new Configuration();  
  101.         conf.set("fs.default.name","hdfs://localhost:9000");  
  102.         String[] otherArgs = new String[]{"input","output"}; /* 直接设置输入参数 */  
  103.         if (otherArgs.length != 2) {  
  104.             System.err.println("Usage: wordcount <in> <out>");  
  105.             System.exit(2);  
  106.             }  
  107.         Job job = Job.getInstance(conf,"Single table join ");  
  108.         job.setJarByClass(simple_data_mining.class);  
  109.         job.setMapperClass(Map.class);  
  110.         job.setReducerClass(Reduce.class);  
  111.         job.setOutputKeyClass(Text.class);  
  112.         job.setOutputValueClass(Text.class);  
  113.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));  
  114.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));  
  115.         System.exit(job.waitForCompletion(true) ? 0 : 1);  
  116.     }
  117. }
复制代码
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

正序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

天空闲话

金牌会员
这个人很懒什么都没写!

标签云

快速回复 返回顶部 返回列表