MapReduce 练习一 找爷孙关系

来源:互联网 发布:网络电视播放器哪个好 编辑:程序博客网 时间:2024/06/18 09:33

要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘。下面进入这个实例。
实例中给出child-parent(孩子——父母)表,要求输出grandchild-grandparent(孙子——爷奶)
child parent
Tom Lucy

Tom Jack

Jone Lucy

Jone Jack

Lucy Mary

Lucy Ben

Jack Alice

Jack Jesse

Terry Alice

Terry Jesse

Philip Terry

Philip Alma

Mark Terry

Mark Alma

关系家族谱
这里写图片描述

代码详解

  /***     * Tom   Lucy     Tom   Jack     Jone   Lucy     Jone   Jack     Lucy   Mary     Lucy   Ben     Jack   Alice     Jack   Jesse     Terry   Alice     Terry   Jesse     Philip   Terry     Philip   Alma     Mark   Terry     Mark   Alma     反转:有孩子和父母为主角     */    //mapper    public static class YearMapper extends Mapper<LongWritable,Text,Text,Text>{        @Override        protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {            String son = value.toString().split("\t")[0];            String father = value.toString().split("\t")[1];            //设为有父母涉及到父母,孩子。 相同key +/-不同            //father为键 son为值            context.write(new Text(father),new Text("-"+son));            //son为键 father为值            context.write(new Text(son),new Text("+"+father));        }    }    /***     * --父母 -- 孩子     * lucy tom     * lucy jone     *     * ++孩子 -- 父母     * lucy mary     * lucy ben     *     * lucy:主角     */    //reducer    public static class YearReducer extends Reducer<Text,Text,Text,Text>{        @Override        protected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {            ArrayList<Text> father=new ArrayList<Text>();            ArrayList<Text> son=new ArrayList<Text>();            for (Text t:values){                String str = t.toString();                if(str.startsWith("-")){                    //正常 孩子                    son.add(new Text(str.substring(1)));                }else{                    //反转 父母                    father.add(new Text(str.substring(1)));                }            }            //lucy的tom是mary的孙子            for (int i=0;i<son.size();i++){                for (int j=0;j<father.size();j++){                    context.write(new Text(son.get(i)),new Text(father.get(j)));                }            }        }    }    public static void main(String [] args) throws IOException, ClassNotFoundException, InterruptedException {        Configuration conf=new Configuration();        Job job= Job.getInstance(conf);        job.setJarByClass(Year.class);        job.setMapperClass(YearMapper.class);        job.setReducerClass(YearReducer.class);        job.setMapOutputValueClass(Text.class);        job.setMapOutputKeyClass(Text.class);        job.setOutputKeyClass(Text.class);        job.setOutputValueClass(Text.class);        //路径        FileInputFormat.setInputPaths(job,new Path(args[0]));        FileOutputFormat.setOutputPath(job,new Path(args[1]));        boolean res = job.waitForCompletion(true);        System.exit(res?0:1);    }
原创粉丝点击