"单表关联"这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘。

需求:实例中给出 child-parent(孩子—父母)表,要求输出 grandchild-grandparent(孙子—爷奶)表。

package test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List; import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
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; /**
* 输入:
* child parent
* 张三 张三的爸爸
* 张三的爸爸 张三的爷爷
*
* 输出:
* grandChiled grandFather
* 张三 张三的爷爷
*/
public class MySingle { public static void main(String[] args) throws Exception { //配置环境变量
System.setProperty("hadoop.home.dir", "F:\\JAVA\\hadoop-2.2.0");
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(MySingle.class); job.setMapperClass(STMapper.class);
job.setReducerClass(STReducer.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.addInputPath(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : -1);
} public static class STMapper extends Mapper<LongWritable, Text, Text, Text>{
@Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException { String[] splited = value.toString().split(" ");
if(splited.length >= 2){ //正向输出,value即 父亲前加符号"-"
context.write(new Text(splited[0]), new Text("-"+splited[1])); //反向输出
context.write(new Text(splited[1]), new Text(splited[0]));
}
}
} public static class STReducer extends Reducer<Text, Text, Text, Text>{
@Override
protected void reduce(Text key, Iterable<Text> v2s,Context context)
throws IOException, InterruptedException { List<String> grandChild=new ArrayList<String>();
List<String> grandParent=new ArrayList<String>(); for(Text text : v2s){ //以"-"开始则是key的父亲
if(text.toString().startsWith("-")){ //将可能成为爷爷的变量存储到grandParent集合中去
grandParent.add(text.toString().substring(1));
}else { grandChild.add(text.toString());
}
}
/**
* 【关键的判断】
* 当前输入的key既有儿子又有父亲
*/
if(grandChild.size()!=0 && grandParent.size()!=0){ for(int i=0;i<grandChild.size();i++){
for(int j=0;j<grandParent.size();j++){ //key:孙子 value:爷爷
context.write(new Text(grandChild.get(i)), new Text(grandParent.get(j)));
}
}
}
}
}
}
  • 在reduce阶段,将两种Value分别存储到grandchild和grandparent集合中
  • 对于reduce阶段的key,只有当他既有儿子又有父亲时,他才可以使得grandchild和grandparent两集合都不为空

MR案例:单表关联查询的更多相关文章

  1. MapReduce应用案例--单表关联

    1. 实例描述 单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘. 实例中给出child-parent 表, 求出grandchild-grandparent表. ...

  2. 序列化表单为json对象,datagrid带额外参提交一次查询 后台用Spring data JPA 实现带条件的分页查询 多表关联查询

    查询窗口中可以设置很多查询条件 表单中输入的内容转为datagrid的load方法所需的查询条件向原请求地址再次提出新的查询,将结果显示在datagrid中 转换方法看代码注释 <td cols ...

  3. MyBatis 多表关联查询

    多表关联查询 一对多 单条SQL实现. //根据部门编号查询出部门和部门成员姓名public dept selectAll() thorws Excatipon; //接口的抽象方法 下面是对应接口的 ...

  4. SpringBoot12 QueryDSL02之利用QueryDSL实现多表关联查询

    1 业务需求 有的系统业务逻辑比较复杂,存在着多表关联查询的的情况,查询的内容不仅仅是单张表的的内容而是多张表的字段组合而成的,直接使用SplringDataJPA实现是比较复杂的,但是如果使用Que ...

  5. RDIFramework.NET 中多表关联查询分页实例

    RDIFramework.NET 中多表关联查询分页实例 RDIFramework.NET,基于.NET的快速信息化系统开发.整合框架,给用户和开发者最佳的.Net框架部署方案.该框架以SOA范式作为 ...

  6. MyBatis学习总结(三)——多表关联查询与动态SQL

    在上一章中我们学习了<MyBatis学习总结(二)——MyBatis核心配置文件与输入输出映射>,这一章主要是介绍一对一关联查询.一对多关联查询与动态SQL等内容. 一.多表关联查询 表与 ...

  7. oracle02--多表关联查询

    1. 多表(关联)查询 多表查询也称之为关联查询.多表关联查询等,主要是指通过多个表的关联来获取数据的一种方式. 1.1. 多表映射关系 一对多:A表的一行数据,对应B表中的多条.如:一个部门可以对应 ...

  8. 【SQL】在SQL Server中多表关联查询问题

    好久没有写SQL语句的多表连接查询,总在用框架进行持久化操作.今天写了一个多表关联查询,想根据两个字段唯一确定一条数据 失败的案例如下: SELECT cyb.id,ad.name FROM [Gen ...

  9. 面试官:为什么mysql不建议执行超过3表以上的多表关联查询?

    概述 前段时间在跟其他公司DBA交流时谈到了mysql跟PG之间在多表关联查询上的一些区别,相比之下mysql只有一种表连接类型:嵌套循环连接(nested-loop),不支持排序-合并连接(sort ...

随机推荐

  1. git sourcetree忽略某些文件提交

    打开sourcetree 点击edit按钮,在文件中加入如下内容.*.iws*.iml*.iprtarget/.settings.project.classpath.externalToolBuild ...

  2. CentOS添加PHP至环境变量

    一,修改/etc/profile文件在尾部添加 PATH=$PATH:/usr/local/php/bin export PATH PATH后面跟着是的php的执行文档路径,可以追加多个以冒号分割 e ...

  3. 学习LINQ必备条件

    转自:http://www.cnblogs.com/VolcanoCloud/p/4451302.html 学习LINQ必备条件:隐式类型局部变量:对象集合初始化器:委托:匿名函数:lambda表达式 ...

  4. Cookies and Session Tracking Client Identification cookie与会话跟踪 客户端识别

    w HTTP The Definitive Guide Cookies can be used to track users as they make multiple transactions to ...

  5. Random/Stochastic

    ---恢复内容开始--- ===================================================== A random variable's possible valu ...

  6. 不得不知的Excel技巧

    1.超链接 选中一个格右击选择超链接. 2.求和 选择一个格点击开始中的求和按钮并拖动求和区域. 3.冻结 冻结一行,选择一行区域,选择开始菜单中的冻结窗格. 冻结上面的行和左边的行,选择夹角的格并点 ...

  7. 如何使用 libtorch 实现 LeNet 网络?

    如何使用 libtorch 实现 LeNet 网络? LeNet 网络论文地址: http://yann.lecun.com/exdb/publis/pdf/lecun-01a.pdf

  8. qemu网络虚拟化之数据流向分析一

    插曲:   今天下午欣喜的想写点关于qemu网络部分的功能,但是中途出现了点小插曲,电脑被某人搞得死机了,并且文章也没有保存.结果,,,就只能重新写了!!所以这里强烈建议开发团队提供自动保存的功能! ...

  9. 入参是小数的String,返回小数乘以100的String

    String money = request.getParameter("orderAmt"); BigDecimal moneyDecimal = new BigDecimal( ...

  10. 0605-Zuul构建API Gateway-使用Sidecar支持异构平台的微服务

    使用非jvm语言 参看地址:https://cloud.spring.io/spring-cloud-static/Edgware.SR3/single/spring-cloud.html#_poly ...