云计算与大数据入门实行四 —— MapReduce 低级编程实践 ...

打印 上一主题 下一主题

主题 595|帖子 595|积分 1785

云计算与大数据入门实行四 —— MapReduce 低级编程实践

  实行目标

  

  • 通过实行掌握基本的 MapReduce 编程方法
  • 掌握用 MapReduce 解决一些常见的数据处理问题,包罗数据去重、数据排序和数据挖掘等
  实行内容

  (一)编程实现文件合并和去重操作
对于两个输入文件,即文件A和文件B,请编写MapReduce步伐,对两个文件举行合并,并剔除其中重复的内容,得到一个新的输出文件C。下面是输入文件和输出文件的一个样例供参考。
输入文件A的样比方下:
  1. 20170101     x
  2. 20170102     y
  3. 20170103     x
  4. 20170104     y
  5. 20170105     z
  6. 20170106     x
复制代码
输入文件B的样比方下:
  1. 20170101      y
  2. 20170102      y
  3. 20170103      x
  4. 20170104      z
  5. 20170105      y
复制代码
根据输入文件A和B合并得到的输出文件C的样比方下:
  1. 20170101      x
  2. 20170101      y
  3. 20170102      y
  4. 20170103      x
  5. 20170104      y
  6. 20170104      z
  7. 20170105      y
  8. 20170105      z
  9. 20170106      x
复制代码
(二)编写步伐实现对输入文件的排序
如今有多个输入文件,每个文件中的每行内容均为一个整数。要求读取所有文件中的整数,举行升序排序后,输出到一个新的文件中,输出的数据格式为每行两个整数,第一个数字为第二个整数的排序位次,第二个整数为原待排列的整数。下面是输入文件和输出文件的一个样例供参考。
输入文件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
复制代码
(三)对给定的表格举行信息挖掘
下面给出一个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
复制代码
实行步骤

  

  • 编程实现文件合并和去重操作
  对于两个输入文件,即文件 A 和文件 B,请编写 MapReduce 步伐,对两个文件举行合并,并剔除其中重复的内容,得到一个新的输出文件 C
  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 Main {
  13.     /**
  14. //     * @param args 对 A,B 两个文件进行合并,并剔除其中重复的内容,得到一个新的输出文件 C
  15.      */
  16. //重载 map 函数,直接将输入中的 value 复制到输出数据的 key 上
  17.     public static class Map extends Mapper<Object, Text, Text, Text> {
  18.         private static Text text = new Text();
  19.         public void map(Object key, Text value, Context context) throws
  20.                 IOException, InterruptedException {
  21.             text = value;
  22.             context.write(text, new Text(""));
  23.         }
  24.     }
  25.     //重载 reduce 函数,直接将输入中的 key 复制到输出数据的 key 上
  26.     public static class Reduce extends Reducer<Text, Text, Text, Text> {
  27.         public void reduce(Text key, Iterable<Text> values, Context context)
  28.                 throws IOException, InterruptedException {
  29.             context.write(key, new Text(""));
  30.         }
  31.     }
  32.     public static void main(String[] args) throws Exception {
  33. // TODO Auto-generated method stub
  34.         Configuration conf = new Configuration();
  35.         conf.set("fs.default.name", "hdfs://localhost:8088");
  36.         String[] otherArgs = new String[]{"input", "output"}; /* 直接设置输入参数
  37.          */
  38.         if (otherArgs.length != 2) {
  39.             System.err.println("Usage: wordcount <in><out>");
  40.             System.exit(2);
  41.         }
  42.         Job job = Job.getInstance(conf, "Merge and duplicate removal");
  43.         job.setJarByClass(Main.class);
  44.         job.setMapperClass(Map.class);
  45.         job.setCombinerClass(Reduce.class);
  46.         job.setReducerClass(Reduce.class);
  47.         job.setOutputKeyClass(Text.class);
  48.         job.setOutputValueClass(Text.class);
  49.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  50.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  51.         System.exit(job.waitForCompletion(true) ? 0 : 1);
  52.     }
  53. }
复制代码

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

  对给定的表格举行信息挖掘
  下面给出一个 child-parent 的表格,要求挖掘其中的父子辈关系,给出祖孙辈关系的表格
  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 Main {
  14.     public static int time = 0;
  15. /**
  16. // * @param args
  17. * 输入一个 child-parent 的表格
  18. * 输出一个体现 grandchild-grandparent 关系的表格
  19. */
  20. //Map 将输入文件按照空格分割成 child 和 parent,然后正序输出一次作为右表,反序
  21. //    输出一次作为左表,
  22. //    需要注意的是在输出的 value
  23. //    中必须加上左右表区别标志
  24.     public static class Map extends Mapper<Object, Text, Text, Text> {
  25.         public void map(Object key, Text value, Context context) throws
  26.                 IOException, InterruptedException {
  27.             String child_name = new String();
  28.             String parent_name = new String();
  29.             String relation_type = new String();
  30.             String line = value.toString();
  31.             int i = 0;
  32.             while (line.charAt(i) != ' ') {
  33.                 i++;
  34.             }
  35.             String[] values = {line.substring(0, i), line.substring(i + 1)};
  36.             if (values[0].compareTo("child") != 0) {
  37.                 child_name = values[0];
  38.                 parent_name = values[1];
  39.                 relation_type = "1";//左右表区分标志
  40.                 context.write(new Text(values[1]), new
  41.                         Text(relation_type + "+" + child_name + "+" + parent_name));
  42. //左表
  43.                 relation_type = "2";
  44.                 context.write(new Text(values[0]), new
  45.                         Text(relation_type + "+" + child_name + "+" + parent_name));
  46. //右表
  47.             }
  48.         }
  49.     }
  50.     public static class Reduce extends Reducer<Text, Text, Text, Text> {
  51.         public void reduce(Text key, Iterable<Text> values, Context context) throws
  52.                 IOException, InterruptedException {
  53.             if (time == 0) { //输出表头
  54.                 context.write(new Text("grand_child"), new
  55.                         Text("grand_parent"));
  56.                 time++;
  57.             }
  58.             int grand_child_num = 0;
  59.             String grand_child[] = new String[10];
  60.             int grand_parent_num = 0;
  61.             String grand_parent[] = new String[10];
  62.             Iterator ite = values.iterator();
  63.             while (ite.hasNext()) {
  64.                 String record = ite.next().toString();
  65.                 int len = record.length();
  66.                 int i = 2;
  67.                 if (len == 0) continue;
  68.                 char relation_type = record.charAt(0);
  69.                 String child_name = new String();
  70.                 String parent_name = new String();
  71. //获取 value-list 中 value 的 child
  72.                 while (record.charAt(i) != '+') {
  73.                     child_name = child_name + record.charAt(i);
  74.                     i++;
  75.                 }
  76.                 i = i + 1;
  77. //获取 value-list 中 value 的 parent
  78.                 while (i < len) {
  79.                     parent_name = parent_name + record.charAt(i);
  80.                     i++;
  81.                 }
  82. //左表,取出 child 放入 grand_child
  83.                 if (relation_type == '1') {
  84.                     grand_child[grand_child_num] = child_name;
  85.                     grand_child_num++;
  86.                 } else {//右表,取出 parent 放入 grand_parent
  87.                     grand_parent[grand_parent_num] = parent_name;
  88.                     grand_parent_num++;
  89.                 }
  90.             }
  91.             if (grand_parent_num != 0 && grand_child_num != 0) {
  92.                 for (int m = 0; m < grand_child_num; m++) {
  93.                     for (int n = 0; n < grand_parent_num; n++) {
  94.                         context.write(new Text(grand_child[m]), new
  95.                                 Text(grand_parent[n]));
  96. //输出结果
  97.                     }
  98.                 }
  99.             }
  100.         }
  101.     }
  102.     public static void main(String[] args) throws Exception {
  103. // TODO Auto-generated method stub
  104.         Configuration conf = new Configuration();
  105.         conf.set("fs.default.name", "hdfs://localhost:8088");
  106.         String[] otherArgs = new String[]{"input", "output"}; /* 直接设置输入参数
  107.          */
  108.         if (otherArgs.length != 2) {
  109.             System.err.println("Usage: wordcount <in><out>");
  110.             System.exit(2);
  111.         }
  112.         Job job = Job.getInstance(conf, "Single table join");
  113.         job.setJarByClass(Main.class);
  114.         job.setMapperClass(Map.class);
  115.         job.setReducerClass(Reduce.class);
  116.         job.setOutputKeyClass(Text.class);
  117.         job.setOutputValueClass(Text.class);
  118.         FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
  119.         FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
  120.         System.exit(job.waitForCompletion(true) ? 0 : 1);
  121.     }
  122. }
复制代码


免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

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

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

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

本版积分规则

自由的羽毛

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

标签云

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