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();
        @Override
        protected 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();
        @Override
        protected 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));//儿子
                }else
                    father.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 中心思想就是 每一次执行的都是儿子和父亲

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
MapReduce是一种分布式计算框架,广泛应用于大数据处理。在MapReduce框架中,Map任务将输入数据分解为许多小的部分,每个小部分都会被分配给不同的Reducer。Reducer任务会处理各自不同的部分数据,并生成最终的结果。 在Python语言中,我们可以通过使用Hadoop Streaming来使用MapReduce框架。Hadoop Streaming是一个允许用户在MapReduce框架中使用任何可执行文件的工具,因此我们可以使用Python编写Map和Reduce函数,并将它们打包成可执行文件,从而在Hadoop Streaming中使用。 在MapReduce中,爷孙关系是一个非常常见的问题。举个例子,我们想要找到一个人的所有祖先或后代,就需要将人们按照他们的关系进行分类,父母属于祖先,子女属于后代。在Map阶段,我们可以使用键值对的形式将所有人按照他们的关系进行映射,键是父母(或子女),而值则是他们的孩子(或父母)。在Reduce阶段,我们可以遍历这些映射,并递归地查找他们的祖先或后代。 在Python中,我们可以使用字典作为数据结构来实现爷孙关系的查找。在Map任务中,我们将字典的键设置为父母(或子女),并将对应值设置为他们的孩子(或父母)。在Reduce任务中,我们可以使用递归函数来查找所有相关的祖先或后代,并将结果存储在一个列表中。 总之,使用MapReduce和Python可以很方便地实现爷孙关系的查找,这在大数据处理和家谱研究等领域中非常有用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值