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

文件内容:

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. 最新为Phpstorm配置xdebug 进行断点调试

    额  ,曾经写过一个,现在发现不咋好使了 ,你说咋整,下载xdebug的时候 还得注意系统是32位还是64位,而且一堆下载文件不知道是哪个. 额,所以我现在发现有个更好的方法,啥也不下了 直接配把  ...

  2. easyui-textbox 和 easyui-validatebox 设置值和获取值

    表单作如下定义:该input使用easyui的"easyui-textbox" <input id="addSnumber" style="wi ...

  3. Octopus系列之代码备份

    代码 $.extend($.validator.messages, { required: "This field is required.", remote: "Ple ...

  4. JAVA https证书相关

    生成证书: keytool -genkey -alias cas -keyalg RSA -keystore  cas.key 导出证书: keytool -export -alias cas  -f ...

  5. tomcat 一个项目在本机和办公室以外电脑服务器上搭建出现乱码问题

    插入数据库都是???乱码 页面浏览之前在本机插入的数据都是正常的 修改conf目录下的server.xml文件 转义字符集 为 GBK  或 UTF-8 utf-8 即 添加 URIEncoding= ...

  6. NOIP 2015 BZOJ 4326 运输计划 (树链剖分+二分)

    Description 公元 年,人类进入了宇宙纪元. L 国有 n 个星球,还有 n− 条双向航道,每条航道建立在两个星球之间,这 n− 条航道连通了 L 国的所有星球. 小 P 掌管一家物流公司, ...

  7. 【opencv学习笔记】SetImageROI函数设置ROI区域的作用及用法

    虽然先前知道ROI区域是感兴趣区域,但是真正看到调用了OpenCV的cvSetImageROI函数时,并不知道它的作用,所以还是单独写了一段代码对这个函数进行探究.   OpenCVchm文档中对cv ...

  8. vs git extensions简单使用方法

    一.准备工具 0.下载Git windows版本下载 http://git-scm.com/download 1.下载Git Extensions.地址http://sourceforge.net/p ...

  9. HDU 3622 Bomb Game(二分+2SAT)

    题意:有一个游戏,有n个回合,每回合可以在指定的2个区域之一放炸弹,炸弹范围是一个圈,要求每回合的炸弹范围没有重合.得分是炸弹半径最小的值.求可以得到的最大分数. 思路:二分+2SAT. 二分炸弹范围 ...

  10. Bitcode设置 编译问题

    今天在一个iOS培训网站上看到一篇关于第三方库不包含bitcode就会报错的文章,感觉剖析得很详细,分享出来,希望可以对iOS初入门者有所帮助.下面我们就一起来看看吧. 用Xcode 7 beta 3 ...