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); }
阅读全文
0 0
- MapReduce 练习一 找爷孙关系
- MapReduce练习(一)
- MapReduce练习一:多MapReduce链接
- MapReduce编程练习(一)
- MapReduce实战练习一:手机流量统计
- mapreduce编程练习(一)简单的练习 WordCount
- 关系代数和SQL练习(一)
- MapReduce练习一(计数,去重,排序,平均成绩)
- MapReduce练习:DataJoin联结练习
- MapReduce(一)
- MapReduce练习(二)
- MapReduce练习(三)
- MapReduce代码实例练习
- Hadoop与Mapreduce关系
- GFS MapReduce BigTable关系
- MapReduce求爷孙关系
- flume+mapreduce实战小练习
- MapReduce 练习二 找朋友
- Linux新手入门:Unable to locate package错误解决办法
- XRecyclerview mvpgson网络解析
- 错题
- Java设计模式之状态模式
- js 定时器的设置及删除
- MapReduce 练习一 找爷孙关系
- 国内IT风投事件
- 关于我理解的闭包(一)
- 745. Prefix and Suffix Search
- 一级购物car
- ssh框架的基本运用
- 浅谈SVN服务器迁移的一些注意事项
- 【Java】【教程】Java 教程
- day22网络编程 UDP、TCP协议