前面的实例都是在数据上进行一些简单的处理,为进一步的操作打基础。单表关联这个实例要求从给出的数据中寻找到所关心的数据,它是对原始数据所包含信息的挖掘。下面进入这个实例。

1.实例描述

  实例中给出child-parent表,要求输出grandchild-grandparent表。

  样例输入:

  file:

  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

  样例输出:

  

2.设计思路

  分析这个实例,显然需要进行单表连接,连接的是左表的parent列和右表的child列,且左表和右表是同一个表。连接结果中除去连接的两列就是所需要的结果----grandchild,grandparent表。要用MapReduce实现这个实例,首先要考虑如何实现表的自连接,其次就是连接列的设置,最后是结果的整理。考虑到MapReduce的shuffle过程会将相同的key值放在一起,所以可以将Map结果的key值设置成待连接的列,然后列中相同的值自然就会连接在一起了。再与最开始的分析联系起来:要连接的是左表的parent列和右表的child列,且左表和右表是同一个表,所以在Map阶段将读入数据分割成child和parent之后,会将parent设置成key,child设置成value进行输出,作为左表;再将同一对child和parent中的child设置成key,parent设置成value进行输出,作为右表。为了区分输出中的左右表,需要在输出的value中再加上左右表信息,比如在value的String最开始处加上字符1表示左表,字符2表示右表。这样在Map的结果中就形成了左表和右表,然后在shuffle过程中完成连接。在Reduce接收到的连接结果中,每个key的value-list就包含了grandchild和grandparent关系。取出每个key的value-list进行解析,将左表中的child放入一个数组,右表中的parent放入一个数组,然后对两个数组求笛卡尔积就是最后的结果了。

3.程序代码:

import java.io.IOException;
import java.util.Iterator; import org.apache.hadoop.conf.Configuration;
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 STjoin { public static int time = 0;
// Map 将输入分割成child和parent,然后正序输出一次作为右表,反序输出一次作为左表,需要
// 注意的是在输出的value中必须加上左右表区别标志
public static class Map extends Mapper<Object, Text, Text, Text>{
@Override
protected void map(Object key, Text value,Mapper<Object, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
// super.map(key, value, context);
String childname = new String();
String parentname = new String();
String relationtype = new String();
String line = value.toString();
int i=0;
while(line.charAt(i)!=' '){
i++;
}
String [] values = {line.substring(0,i),line.substring(i+1)};
if(values[0].compareTo("child") !=0 ){
childname = values[0];
parentname = values[1];
relationtype = "1"; // 左右表区分标志
context.write(new Text(values[1]), new Text(relationtype+"+"+childname+"+"+parentname)); // 左表
relationtype = "2";
context.write(new Text(values[0]), new Text(relationtype+"+"+childname+"+"+parentname)); // 右表
}
}
} public static class Reduce extends Reducer<Text, Text, Text, Text>{
@Override
protected void reduce(Text key, Iterable<Text> values,Reducer<Text, Text, Text, Text>.Context context)
throws IOException, InterruptedException {
// super.reduce(arg0, arg1, arg2);
if(time==0){ // 输出表头
context.write(new Text("grandchild"), new Text("grandparent"));
time++;
}
int grandchildnum = 0;
String grandchild[] = new String[10];
int grandparentnum = 0;
String grandparent[] = new String[10];
Iterator ite = values.iterator();
while(ite.hasNext()){
String record = ite.next().toString();
int len = record.length();
int i = 2;
if(len == 0) continue;
char relationtype = record.charAt(0);
String childname = new String();
String parentname = new String();
// 获取value-list中value的child
while(record.charAt(i)!='+'){
childname = childname + record.charAt(i);
i++;
}
i = i+1;
// 获取value-list中value的parent
while(i<len){
parentname = parentname+record.charAt(i);
i++;
}
// 左表,取出child放入grandchild
if(relationtype == '1'){
grandchild[grandchildnum] = childname;
grandchildnum++;
}else { // 右表,取出parent放入grandparent
grandparent[grandparentnum] = parentname;
grandparentnum++;
}
}
// grandchild 和 grandparent 数组求笛卡尔积
if(grandparentnum !=0 && grandchildnum !=0){
for(int m=0;m<grandchildnum;m++){
for(int n=0; n<grandparentnum;n++){
context.write(new Text(grandchild[m]), new Text(grandparent[n])); // 输出结果
}
}
}
}
} public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
if(otherArgs.length!=2){
System.out.println("Usage:wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf,"sing table join");
job.setJarByClass(STjoin.class);
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);
FileInputFormat.addInputPath(job,new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true)?0:1);
} }

  

Hadoop 单表关联的更多相关文章

  1. Hadoop 多表关联

    一.实例描述 多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息.下面进入这个实例. 输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列:另一个代表地址列,包含地 ...

  2. Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException

    简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Ha ...

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

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

  4. MapRedece(单表关联)

    源数据:Child--Parent表 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse T ...

  5. MR案例:单表关联查询

    "单表关联"这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘. 需求:实例中给出 child-parent(孩子—父母)表,要求输出 grandchild ...

  6. MapReduce编程系列 — 5:单表关联

    1.项目名称: 2.项目数据: chile    parentTom    LucyTom    JackJone    LucyJone    JackLucy    MaryLucy    Ben ...

  7. 利用hadoop来解决“单表关联”的问题

    已知 child parent a b a c d b d c b e b f c g c h x g x h m x m n o x o n 则 c 2+c+g 2+c+h 1+a+c 1+d+c ...

  8. MapReduce单表关联学习~

    首先考虑表的自连接,其次是列的设置,最后是结果的整理. 文件内容: import org.apache.hadoop.conf.Configuration; import org.apache.had ...

  9. mapreduce-实现单表关联

    //map类 package hadoop3; import java.io.IOException; import org.apache.hadoop.io.LongWritable;import ...

随机推荐

  1. Xcode9模拟器隐藏边框

    选中模拟器,在Mac顶部菜单栏找到Window-->Show Device Bezeles 取消勾选代表去除黑边,勾选代表展示黑边,根据个人喜好设置吧

  2. 基于服务器的AAA配置实验(Cisco PT)

    一.实验拓扑 二.网络地址分配 Device Interface IP Address Subnet Mask R1 Fa0/0 192.168.1.1 255.255.255.0 S0/0/0 10 ...

  3. 《剑指Offer》第20题(Java实现):定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1))。

    一.题目描述 定义栈的数据结构,请在该类型中实现一个能够得到栈中所含最小元素的min函数(时间复杂度应为O(1)). 二.思路解析 首先定义一个Integer类型的栈,记为stack,此栈用来完成数据 ...

  4. centos7+mariadb+防火墙,允许远程

    centos7 已安装mariadb,想要允许数据库远程==数据库权限允许+系统允许 mariadb:允许数据库用户在所有ip使用某个用户远程 GRANT ALL PRIVILEGES ON *(数据 ...

  5. 自学elastic search

    工作也有一段时间了,虽然来这个公司之后学会了几门不同的语言,但想拨尖还是任重道远. 想往高级程序员甚至是架构师方向发展.他仍然是我的学习对象.我现在做着的,无非是他玩剩下的罢了. luncene之前有 ...

  6. 4,postman和newman的联合使用

    1:下载安装node.js http://nodejs.cn/ 出现版本号证明安装node.js成功 2:安装newman npm install -g newman --registry=https ...

  7. Numpy Ndarray对象

    Numpy 最重要的一个特点是 N 维数组对象 ndarrary ,它是一系列同类型数据的集合,以 0 下标为开始进行集合中的索引. ndarray 对象是用于存放同类型元素的多维数组. ndarra ...

  8. 从 Basic Paxos 到 Multi Paxos 到 Raft

    在朴素Paxos算法中, 各个节点经过 Prepare 和 Accept 阶段, 会达成一个值, 这个值一旦达成, 就不能被修改, 如下例子: 图示1 上面的操作几乎没有任何实用价值, 于是演变成下面 ...

  9. Asp.net Zero 应用实战-官方示例PhoneBook学习1

    适用Zero版本:ASP.NET Core & Angular 2+ (aspnet-zero-core-3.1.0). 该版本官方有两个solution文件夹:Angular(前端) 和 a ...

  10. 如何创建并初始化程序集里List类型的反射

    参考网址:http://stackoverflow.com/questions/315231/using-reflection-to-set-a-property-with-a-type-of-lis ...