MapReduce单表关联学习~
首先考虑表的自连接,其次是列的设置,最后是结果的整理.
文件内容:
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单表关联学习~的更多相关文章
- MapReduce应用案例--单表关联
1. 实例描述 单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘. 实例中给出child-parent 表, 求出grandchild-grandparent表. ...
- Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException
简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Ha ...
- Hadoop 单表关联
前面的实例都是在数据上进行一些简单的处理,为进一步的操作打基础.单表关联这个实例要求从给出的数据中寻找到所关心的数据,它是对原始数据所包含信息的挖掘.下面进入这个实例. 1.实例描述 实例中给出chi ...
- MapRedece(单表关联)
源数据:Child--Parent表 Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse T ...
- MR案例:单表关联查询
"单表关联"这个实例要求从给出的数据中寻找所关心的数据,它是对原始数据所包含信息的挖掘. 需求:实例中给出 child-parent(孩子—父母)表,要求输出 grandchild ...
- MapReduce编程系列 — 5:单表关联
1.项目名称: 2.项目数据: chile parentTom LucyTom JackJone LucyJone JackLucy MaryLucy Ben ...
- mapreduce-实现单表关联
//map类 package hadoop3; import java.io.IOException; import org.apache.hadoop.io.LongWritable;import ...
- 利用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 ...
- Hibernate单表映射学习笔记之一——hibernalnate开发环境配置
1.什么是ORM? Object/Relationship Mapping:对象/关系映射 2.写SQL语句不好之处: (1)不同数据库使用的SQL语法不同(PL/SQL.T/SQL) (2)同样的功 ...
随机推荐
- java文件上传下载
文件上传首先要引入两个核心包 commons-fileupload-1.2.1.jar commons-io-1.4.jar 下面是对文件上传和下载的一些代码做的一个简单封装,可以方便以后直接使用[使 ...
- easyui-textbox 和 easyui-validatebox 设置值和获取值
表单作如下定义:该input使用easyui的"easyui-textbox" <input id="addSnumber" style="wi ...
- Redis-收藏文章
http://www.cnblogs.com/capqueen/p/HowToUseRedis.html Redis到底该如何利用? http://www.cnblogs.com/yangecnu/ ...
- apache启动时80端口占用的解决方法
问题: (98)Address already in use: make_sock: could not bind to address [::]:80 (98)Address already in ...
- css定位之浮动定位
浮动定位可以是原本垂直排列的块级元素,变成水平排列 1浮动元素 float:left 或者float:right 这些浮动会直接碰到父容器的边界为止. 2设置了浮动的元素,元素会脱离标准文档流中,但 ...
- Invoke-WebRequest Invoke-RestMethod 乱码研究
powershell Invoke-WebRequest Invoke-RestMethod 乱码 encoding sharset CharacterSet Invoke-WebRequest和In ...
- AsyncTask的基本使用和各个参数的说明
AsyncTask 的执行分为四个步骤,每一步都对应一个回调方法,需要注意的是这些方法不应该由应用程序调用,开发者需要做的就是实现这些方法.在任务的执行过程中,这些方法被自动调用. * onPreEx ...
- Objective-C学习笔记-第一天(2)
Objective-C中的协议,相当于Java中的接口 参考:http://www.cnblogs.com/zzy0471/p/3894307.html 一个简单的协议遵循: PersonProtoc ...
- 用VS2010+Qt4.6.4编译QtAV
http://blog.csdn.net/trustguan/article/details/45623891 如果在链接的过程中,出现以上错误: 1>MSVCRTD.lib(MSVCR100D ...
- Fragment开发计划
Fragment是什么 Fragment正如字面意思所言是碎片,所以这是一个管理碎片时间的应用程序.目前考虑的是先在Android上实现,如果IOS的合作伙伴靠谱可以交给他做,如果不靠谱就等Andro ...