Advertisement
FancyKing

第3关:信息挖掘 - 挖掘父子关系

Mar 19th, 2020
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.31 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.*;
  3.  
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.IntWritable;
  7. import org.apache.hadoop.io.Text;
  8. import org.apache.hadoop.mapreduce.Job;
  9. import org.apache.hadoop.mapreduce.Mapper;
  10. import org.apache.hadoop.mapreduce.Reducer;
  11. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13. import org.apache.hadoop.util.GenericOptionsParser;
  14.  
  15. /**
  16.  * @author FancyKing
  17.  */
  18. public class simple_data_mining {
  19.     public static int time = 0;
  20.  
  21.     /**
  22.      * @param args
  23.      * 输入一个child-parent的表格
  24.      * 输出一个体现grandchild-grandparent关系的表格
  25.      */
  26.     //Map将输入文件按照空格分割成child和parent,然后正序输出一次作为右表,反序输出一次作为左表,需要注意的是在输出的value中必须加上左右表区别标志
  27.     public static class Map extends Mapper<Object, Text, Text, Text>{
  28.         public void map(Object key, Text value, Context context) throws IOException,InterruptedException{
  29.             /********** Begin **********/
  30.             String line = value.toString();
  31.             String[] childAndParent = line.split(" ");
  32.             List<String> list = new ArrayList<>(2);
  33.             for (String childOrParent : childAndParent) {
  34.                 if (!"".equals(childOrParent)) {
  35.                     list.add(childOrParent);
  36.                 }
  37.             }
  38.             if (!"child".equals(list.get(0))) {
  39.                 String childName = list.get(0);
  40.                 String parentName = list.get(1);
  41.                 String relationType = "1";
  42.                 context.write(new Text(parentName), new Text(relationType + "+"
  43.                         + childName + "+" + parentName));
  44.                 relationType = "2";
  45.                 context.write(new Text(childName), new Text(relationType + "+"
  46.                         + childName + "+" + parentName));
  47.             }
  48.  
  49.             /********** End **********/
  50.         }
  51.     }
  52.  
  53.     public static class Reduce extends Reducer<Text, Text, Text, Text>{
  54.         public void reduce(Text key, Iterable<Text> values,Context context) throws IOException,InterruptedException{
  55.                 /********** Begin **********/
  56.  
  57.                 //输出表头
  58.             if (time == 0) {
  59.                 context.write(new Text("grand_child"), new Text("grand_parent"));
  60.                 time++;
  61.             }
  62.  
  63.  
  64.            
  65.                 //获取value-list中value的child
  66.             List<String> grandChild = new ArrayList<>();
  67.  
  68.  
  69.  
  70.                
  71.                 //获取value-list中value的parent
  72.  
  73.             List<String> grandParent = new ArrayList<>();
  74.  
  75.                
  76.                 //左表,取出child放入grand_child
  77.  
  78.             for (Text text : values) {
  79.                 String s = text.toString();
  80.                 String[] relation = s.split("\\+");
  81.                 String relationType = relation[0];
  82.                 String childName = relation[1];
  83.                 String parentName = relation[2];
  84.                 if ("1".equals(relationType)) {
  85.                     grandChild.add(childName);
  86.                 } else {
  87.                     grandParent.add(parentName);
  88.                 }
  89.             }
  90.                 //右表,取出parent放入grand_parent
  91.  
  92.             int grandParentNum = grandParent.size();
  93.                int grandChildNum = grandChild.size();
  94.                if (grandParentNum != 0 && grandChildNum != 0) {
  95.                 for (int m = 0; m < grandChildNum; m++) {
  96.                     for (int n = 0; n < grandParentNum; n++) {
  97.                         //输出结果
  98.                         context.write(new Text(grandChild.get(m)), new Text(
  99.                                     grandParent.get(n)));
  100.                     }
  101.                 }
  102.             }
  103.                 /********** End **********/
  104.                
  105.         }
  106.     }
  107.     public static void main(String[] args) throws Exception{
  108.         // TODO Auto-generated method stub
  109.         Configuration conf = new Configuration();
  110.         Job job = Job.getInstance(conf,"Single table join");
  111.         job.setJarByClass(simple_data_mining.class);
  112.         job.setMapperClass(Map.class);
  113.         job.setReducerClass(Reduce.class);
  114.         job.setOutputKeyClass(Text.class);
  115.         job.setOutputValueClass(Text.class);
  116.         String inputPath = "/user/reduce/input";   //设置输入路径
  117.         String outputPath = "/user/reduce/output";   //设置输出路径
  118.         FileInputFormat.addInputPath(job, new Path(inputPath));
  119.         FileOutputFormat.setOutputPath(job, new Path(outputPath));
  120.         System.exit(job.waitForCompletion(true) ? 0 : 1);
  121.  
  122.     }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement