mapreduce的爷孙关系问题

编程入门 行业动态 更新时间:2024-10-06 08:22:13

mapreduce的爷孙<a href=https://www.elefans.com/category/jswz/34/1770737.html style=关系问题"/>

mapreduce的爷孙关系问题

这个题目,相信大家在网上已经看到很多解决方案了,我在拿到这个题的时候,先想的是,爷爷找孙子,还是孙子找爷爷.

先上数据:

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

题目:左边数据是child 右边数据是parent 找到当中的孙子和爷爷奶奶

上代码!

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;import java.io.IOException;
import java.util.ArrayList;/*** @create: 2021-09-09 20:34* @author: 溜达.逮幸福* @program: Find_Parent* @Description:**/
public class Find_Parent {static class MMapper extends Mapper<LongWritable,Text,Text,Text>{Text k = new Text();Text v = new Text();@Overrideprotected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {String line = value.toString();String[] words = line.split("-");String child = words[0] ;String parent = words[1] ;context.write(new Text(parent),new Text("-"+child));//儿子context.write(new Text(child),new Text("+"+parent));//父亲}}static class RReducer extends Reducer<Text,Text,Text,Text>{Text v = new Text();@Overrideprotected void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {ArrayList<String>father = new ArrayList<>();ArrayList<String>son = new ArrayList<>();for (Text value : values) {String str = value.toString();if (str.startsWith("-")){son.add(str.substring(1));//儿子}elsefather.add(str.substring(1));//父亲}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 Exception{Configuration conf = new Configuration();Job job = Job.getInstance(conf, "wordcount");job.setMapperClass(MMapper.class);job.setReducerClass(RReducer.class);job.setMapOutputKeyClass(Text.class);job.setMapOutputValueClass(Text.class);job.setOutputKeyClass(Text.class);job.setOutputValueClass(Text.class);FileInputFormat.setInputPaths(job,new Path("d:\\child_parent.txt"));FileOutputFormat.setOutputPath(job,new Path("d:\\parent"));job.waitForCompletion(true);}
}

这个是我在网上看到的,我觉得最妙的解决方案:

1 获得child 的所有parent 就是父亲

2 获得parent的所有child 就是儿子

map里面的每一行数据中,获得它的所有的儿子的父亲  和所有父亲的儿子

3 利用集合,将父亲和儿子分别存放起来

4 遍历集合就行了.

5 中心思想就是 每一次执行的都是儿子和父亲

更多推荐

mapreduce的爷孙关系问题

本文发布于:2024-02-28 09:36:06,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1769092.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:关系   mapreduce

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!