首先考虑表的自连接,其次是列的设置,最后是结果的整理.

文件内容:

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.input.TextInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.Tool;
import org.apache.hadoop.util.ToolRunner; import java.io.IOException;
import java.util.Iterator;
import java.util.Objects; public class STjoin extends Configured implements Tool {
public static int time = 0;
  //map将输入分割成child和parent,然后正序输出一次作为右表,反序输出一次作为左表
  //需要注意的是在输出的value中必须加上左右表区别标志
public static class Map extends Mapper<Object,Text,Text,Text>{ public void map(Object key,Text value,Context context) throws IOException,
InterruptedException{
String childname = new String();
String parentname = new String();
String relationtype = new String();
String line = value.toString();
int i = 0;
       //文件以空格分隔
while(line.charAt(i) != ' '){
i++;
}
       //拆分child 和 parent
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>{ public void reduce(Text key,Iterable<Text> values,Context context) throws IOException,InterruptedException{
        //输出表头
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(); while(record.charAt(i) != '+'){
childname = childname + record.charAt(i);
i++;
}
i = i+1; while(i<len){
parentname = parentname + record.charAt(i);
i++;
} if(relationtype == '1') {
grandchild[grandchildnum] = childname;
;
grandchildnum++;
}else{
grandparent[grandparentnum] = parentname;
grandparentnum++;
} } if(grandparentnum != 0 && grandchildnum != 0){
for(int m = 0;m<grandchildnum;m++){
for(int n = 0;n<grandparentnum;n++){
System.out.println(grandchild[m] + " " + grandparent[n]);
context.write(new Text(grandchild[m]),new Text(grandparent[n]));
}
}
} }
} public int run(String[] args) throws Exception{
Configuration aaa = new Configuration();
Job job = Job.getInstance(aaa);
String InputPaths = "/usr/local/idea-IC-139.1117.1/Hadoop/out/datainput/child-parent.txt";
String OutputPath = "/usr/local/idea-IC-139.1117.1/Hadoop/out/dataout/"; job.setJarByClass(Sort.class);
job.setJobName("Sort"); job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);
FileInputFormat.setInputPaths(job, new Path(InputPaths));
FileOutputFormat.setOutputPath(job, new Path(OutputPath)); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(org.apache.hadoop.mapreduce.lib.output.TextOutputFormat.class);
boolean success = job.waitForCompletion(true);
return success ? 0 : 1; } public static void main(String[] args) throws Exception{
int ret = ToolRunner.run(new STjoin(), args);
System.exit(ret);
}
}

输出结果:

参考:《Hadoop实战》

MapReduce单表关联学习~的更多相关文章

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

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

  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. Hadoop 单表关联

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

  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. mapreduce-实现单表关联

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

  8. 利用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 ...

  9. Hibernate单表映射学习笔记之一——hibernalnate开发环境配置

    1.什么是ORM? Object/Relationship Mapping:对象/关系映射 2.写SQL语句不好之处: (1)不同数据库使用的SQL语法不同(PL/SQL.T/SQL) (2)同样的功 ...

随机推荐

  1. 读javascript高级程序设计01-基本概念、数据类型、函数

    一. javascript构成 1.javascript实现由三部分组成: ECMAScript:核心语言功能 DOM:文档对象模型,提供访问和操作网页内容的方法和接口 BOM:浏览器对象模型,提供与 ...

  2. [ie兼容]ie7 margin-bottom失效

    有时候子元素设置了margin,父元素也设置了margin,父元素的margin在ie7下会失效(包括padding) 解决方法:父容器加属性overflow:hidden;zoom:100%;

  3. android单选框和复选框(练习)

    <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android=&quo ...

  4. new对象时,类名后加括号与不加括号的区别

    [1]默认构造函数 关于默认构造函数,请参见随笔<类中函数> 请看测试代码: 1 #include <iostream> 2 using namespace std; 3 4 ...

  5. SQLite常用命令

    1.点命令 [退出SQLite提示符] .quit .exit [帮助] .help [显示设置] .show 2.语法 [结束符] : --一行语句的结束以分号(:)结尾 [CREATE TABLE ...

  6. C语言基础--数组及相关

    概念: 一堆相同类型的数据的有序集合 格式: 元素类型  数组名称[ 元素个数 ] 定义数组: // 定义了一个名称叫做scores的数组, 数组中可以存放3个int类型的数据 ]; // 只要定义一 ...

  7. Linux学习 : 裸板调试 之 使用MMU

    MMU(Memory Management Unit,内存管理单元),操作系统通过使用处理器的MMU功能实现以下:1)虚拟内存.有了虚拟内存,可以在处理器上运行比实际物理内存大的应用程序.为了使用虚拟 ...

  8. c# windows编程控件学习-1

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  9. AngularJs的UI组件ui-Bootstrap分享(三)——Accordion

    Accordion手风琴控件使用uib-accordion和uib-accordion-group指令. <script> angular.module('myApp', ['ui.boo ...

  10. 再议C++的性能

    最近在公司里的项目做的是性能优化,相关性能调优的经验总结也在前一篇文章里说了.这里再说一说和性能相关的东西.主要针对的是C++类库中常用的一些数据结构,比方说std::string.顺序容器(vect ...