【Mapreduce】利用单表关联在父子关系中求解爷孙关系

2023-05-16

首先是有如下数据,设定左边是右边的儿子,右边是左边的父母

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	Jesse
Tom	Alice
Jone	Jesse
Jone	Alice
Jone	Ben
Jone	Mary
Tom	Ben
Tom	Mary
Philip	Alice
Philip	Jesse
Mark	Alice
Mark	Jesse

要利用Mapreduce解决这个问题,主要思想如下:

1、在Map阶段,将父子关系与相反的子父关系,同时在各个value前补上前缀-与+标识此key-value中的value是正序还是逆序产生的,之后进入context。

下图通过其中的Jone-Lucy Lucy-Mary,求解出Jone-Mary来举例


2、MapReduce会自动将同一个key的不同的value值,组合在一起,推到Reduce阶段。在value数组中,跟住前缀,我们可以轻松得知,哪个是爷,哪个是孙。

因此对各个values数组中各个项的前缀进行输出。


可以看得出,整个过程Key一直被作为连接的桥梁来用。形成一个单表关联的运算。


因此代码如下,根据上面的思想很容易得出如下的代码了,需要注意的是,输出流输入流都要设置成Text,因为全程都是在对Text而不是IntWritable进行操作:

import java.io.IOException;
import java.util.ArrayList;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
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 org.apache.hadoop.util.GenericOptionsParser;

public class MyMapReduce {

	public static class MyMapper extends Mapper<Object, Text, Text, Text> {

		public void map(Object key, Text value, Context context)
				throws IOException, InterruptedException {
			String child = value.toString().split(" ")[0];
			String parent = value.toString().split(" ")[1];
			//产生正序与逆序的key-value同时压入context
			context.write(new Text(child), new Text("-" + parent));
			context.write(new Text(parent), new Text("+" + child));
		}
	}

	public static class MyReducer extends Reducer<Text, Text, Text, Text> {

		public void reduce(Text key, Iterable<Text> values, Context context)
				throws IOException, InterruptedException {
			ArrayList<Text> grandparent = new ArrayList<Text>();
			ArrayList<Text> grandchild = new ArrayList<Text>();
			for (Text t : values) {//对各个values中的值进行处理
				String s = t.toString();
				if (s.startsWith("-")) {
					grandparent.add(new Text(s.substring(1)));
				} else {
					grandchild.add(new Text(s.substring(1)));
				}
			}
			//再将grandparent与grandchild中的东西,一一对应输出。
			for (int i = 0; i < grandchild.size(); i++) {
				for (int j = 0; j < grandparent.size(); j++) {
					context.write(grandchild.get(i), grandparent.get(j));
				}
			}
		}
	}

	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();

		String[] otherArgs = new GenericOptionsParser(conf, args)
				.getRemainingArgs();
		if (otherArgs.length != 2) {
			System.err.println("Usage: wordcount <in> <out>");
			System.exit(2);
		}
		Job job = new Job(conf);
		job.setMapperClass(MyMapper.class);
		job.setReducerClass(MyReducer.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);

		// 判断output文件夹是否存在,如果存在则删除
		Path path = new Path(otherArgs[1]);// 取第1个表示输出目录参数(第0个参数是输入目录)
		FileSystem fileSystem = path.getFileSystem(conf);// 根据path找到这个文件
		if (fileSystem.exists(path)) {
			fileSystem.delete(path, true);// true的意思是,就算output有东西,也一带删除
		}

		FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
		FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
		System.exit(job.waitForCompletion(true) ? 0 : 1);
	}

}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

【Mapreduce】利用单表关联在父子关系中求解爷孙关系 的相关文章

随机推荐

  • 7.1、mysql mha 主从自动切换 高可用

    是这个博主写的 xff0c 但是找不到地址了 写了他的另一个MHA地址 感谢原创的贡献 mysql mha 主从自动切换 高可用 mha xff08 Master High Availability xff09 目前在MySQL多服务器 x
  • 7.3、mysql主主循环备份数据库

    绿色部分是我根据需要自己写的 mysql 主主互备 双机热备的概念简单说一下 xff0c 就是要保持两个数据库的状态自动同步 对任何一个数据库的操作都自动应用到另外一个数据库 xff0c 始终保持两个数据库数据一致 这样做的好处多 1 可以
  • 7.4、Slave_SQL_Running: No mysql同步故障解决方法

    Slave SQL Running No mysql同步故障解决方法 2010 02 21 16 31 30 标签 xff1a mysql 数据库 同步 双机 休闲 原创作品 xff0c 允许转载 xff0c 转载时请务必以超链接形式标明文
  • 7.5、mysql破解密码

    找不到原创了 xff0c 百度了一下 xff0c 这个比较像 感谢原创的贡献 vi etc my cnf 在配置文件中加入 s kip grant tables mysqld safe skip grant tables amp 最佳答案
  • Scrum实践系列之三--敏捷教练的修炼之路

    敏捷教练与项目经理 在被奉为 项目管理圣经 的PMBOK中 xff0c 对项目经理在各阶段的职责有着清晰的界定 xff0c 比如项目经理制定规则 安排进度 监控执行中的各项风险并实时汇报状态 xff0c 等等 然而在敏捷的世界里 xff0c
  • 知识图谱_概述:课程PPT+个人理解

    2019 05 08 一 概念 xff08 是什么 xff09 1 知识 xff1a 有不同的解释 xff0c 可以是 不变的真理 经验 背景 解释 交工的信息 xff08 1 xff09 分类 陈述性知识 gt 描述客观事物的性状和关系等
  • chatgpt

    transformer GitHub Topics GitHub
  • Apollo:source cyber/setup.bash的作用

    source cyber setup bash 是在使用Apollo开发过程中 xff0c 用于加载Apollo软件的配置以及环境变量的脚本 Apollo是一款自动驾驶开发平台 xff0c cyber是其中的一个核心模块 xff0c 提供了
  • 什么样的人当不好程序员?

    什么样的人当不好程序员 xff1f 2016 01 21 程序员之家 来源 xff1a 36Kr 译文 xff1a http 36kr com p 5042433 html 原文 xff1a https goo gl jLfUFq 软件蚕食
  • java基础语法(顺便回顾cpp语法并比较与java的异同)

    变量 标识符 关键字与数据类型 1 标识符命名风格约定 xff1a 不能以数字开头 xff0c 也不能有 等符号 可以有 和 但不用作开头 方法名 变量名首单词小写 xff0c 其余单词首字母大写 如anyVariableName 类名 接
  • 刷leetcode,锻炼编程能力(c++)

    力扣20 xff0c 有效的括号 xff0c 栈 span class token macro property span class token directive keyword include span span class toke
  • 华为笔试题库之困难--难度

    题记 xff1a 若立志投身算法研究 xff0c 可精研理论算法 xff1a 动态规划 递归 深度搜索等 xff1b 若以解决问题为目的 xff0c 主要为了工作内容 xff0c 当尝试快而简单的方法 xff0c 这该是学习的本意 1 素数
  • C++ - opencv应用实例之矩形框检测

    C opencv应用实例之矩形框检测 现阶段下 目标检测在实际应用场景中的表现颇为重要 工业质检 移动机器人视觉伺服 作业 交通监控 安防领域等均需要通过目标检测来实现对目标的定位 测量或者统计 辅助控制等 目前目标检测主要分为两个方向的发
  • cpp-5 : Depends: gcc-5-base (= 5.3.1-14ubuntu2) but 5.4.0-6ubuntu1~16.04.11 is to be installed

    在5 4 0 6ubuntu1 16 04 11 cpp 5 amd64 Xenial 16 04 Ubuntu上下载对应的版本 xff0c 国 内源的版本都是5 4 0 6ubuntu1 16 04 12 xff0c 最后一位是12 xf
  • Questasim SystemC 指令sccom

    QuestaSim sccom 10 6c 1 compiler 2017 09 Sep 15 2017 Usage sccom options CPP compiler options lt CPP files gt General Op
  • MacOS 安装curl和wget

    目录 1 什么是curl和wget1 1 curl1 2 wget1 3 curl和wget的区别 2 安装curl2 1 利用wget安装curl2 2 直接下载curl源码编译安装 推荐 3 安装wget3 1 利用Homebrew安装
  • linux tty core 源码分析__tty_read,__tty_write,__tty_poll,

    转载 xff1a http blog csdn net sirzjp article details 6134489 前面分析了open操作 xff0c 现在分析读操作tty read tty read直接调用线路规程中的读操作从tty g
  • D触发器二分频电路

    D触发器二分频电路 有时真的要感慨一下自己电路学的够烂的 xff0c 啥都不会 xff0c 做示波器要学习分频电路 xff0c 学呗 将D触发器的Q非端接到数据输入端D即可实现二分频 xff0c 说白了就是CLK时钟信号的一个周期Q端电平反
  • 自动驾驶系列(十)编写电动车ROS节点(刹车)

    一 硬件控制协议 对于不同的设备 xff0c 底层的通讯方式都不一致 xff0c 因此需要根据具体硬件具体分析 本系统 采用了1个CAN盒子控制转向 xff0c 1路DA控制油门 xff0c 另外一路DA控制刹车 1 xff0c 转向协议
  • 【Mapreduce】利用单表关联在父子关系中求解爷孙关系

    首先是有如下数据 xff0c 设定左边是右边的儿子 xff0c 右边是左边的父母 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Mary Lucy Ben Jack Alice Jack Jesse