Hadoop-Map/Reduce之单表连接的实现
package cn.genekang.hadoop.test; import java.io.IOException;
import java.util.ArrayList; 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; public class STjoin {
/*
* child parentTom LucyTom JackLucy MarryLucy BenJack AliceJack Jesse* *
*/
// 单表连接
public static class StjoinMap extends
Mapper<LongWritable, Text, Text, Text> { private Text kText = new Text();
private Text vText = new Text(); @Override
protected void map(LongWritable key, Text value, Context context)
throws IOException, InterruptedException {
String[] lineSplit = value.toString().split("\t");
// c#代表的是左表 p#代表的是右表
// 右表
kText.set(lineSplit[1]);
vText.set("p#" + lineSplit[0]);
context.write(kText, vText); // 左表
kText.set(lineSplit[0]);
vText.set("c#" + lineSplit[1]);
context.write(kText, vText); } } public static class StjoinReduce extends Reducer<Text, Text, Text, Text> {
private Text kText = new Text();
private Text vText = new Text(); @Override
protected void reduce(Text key, Iterable<Text> values, Context context)
throws IOException, InterruptedException {
ArrayList<String> cList = new ArrayList<String>();
ArrayList<String> pList = new ArrayList<String>();
for (Text v : values) {
if (v.toString().contains("c#")) {
cList.add(v.toString().substring(2));
} else if (v.toString().contains("p#")) {
pList.add(v.toString().substring(2)); }
} if (!cList.isEmpty() && !pList.isEmpty()) {
for (String c : cList) {
for (String p : pList) {
kText.set(c);
vText.set(p);
context.write(kText, vText);
}
}
} // 清空list
cList.clear();
pList.clear();
} } public static void main(String[] args) throws IOException,
ClassNotFoundException, InterruptedException {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf); job.setJarByClass(STjoin.class); job.setMapperClass(StjoinMap.class);
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(Text.class); job.setReducerClass(StjoinReduce.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); System.exit(job.waitForCompletion(true) ? 0 : 1);
} }
Hadoop-Map/Reduce之单表连接的实现的更多相关文章
- Hadoop阅读笔记(三)——深入MapReduce排序和单表连接
继上篇了解了使用MapReduce计算平均数以及去重后,我们再来一探MapReduce在排序以及单表关联上的处理方法.在MapReduce系列的第一篇就有说过,MapReduce不仅是一种分布式的计算 ...
- Hadoop Map/Reduce教程
原文地址:http://hadoop.apache.org/docs/r1.0.4/cn/mapred_tutorial.html 目的 先决条件 概述 输入与输出 例子:WordCount v1.0 ...
- 一步一步跟我学习hadoop(5)----hadoop Map/Reduce教程(2)
Map/Reduce用户界面 本节为用户採用框架要面对的各个环节提供了具体的描写叙述,旨在与帮助用户对实现.配置和调优进行具体的设置.然而,开发时候还是要相应着API进行相关操作. 首先我们须要了解M ...
- Hadoop Map/Reduce
Hadoop Map/Reduce是一个使用简易的软件框架,基于它写出来的应用程序能够运行在由上千个商用机器组成的大型集群上,并以一种可靠容错的方式并行处理上T级别的数据集.一个Map/Reduce ...
- Hadoop Map/Reduce的工作流
问题描述 我们的数据分析平台是单一的Map/Reduce过程,由于半年来不断地增加需求,导致了问题已经不是那么地简单,特别是在Reduce阶段,一些大对象会常驻内存.因此越来越顶不住压力了,当前内存问 ...
- Hadoop Map/Reduce 示例程序WordCount
#进入hadoop安装目录 cd /usr/local/hadoop #创建示例文件:input #在里面输入以下内容: #Hello world, Bye world! vim input #在hd ...
- (转载)Hadoop map reduce 过程获取环境变量
来源:http://www.linuxidc.com/Linux/2012-07/66337.htm 作者: lmc_wy Hadoop任务执行过程中,在每一个map节点或者reduce节点能获取 ...
- Hadoop map reduce 任务数量优化
mapred.tasktracker.map.tasks.maximum 官方解释:The maximum number of map tasks that will be run simultan ...
- hadoop2.2编程:自定义hadoop map/reduce输入文件切割InputFormat
hadoop会对原始输入文件进行文件切割,然后把每个split传入mapper程序中进行处理,FileInputFormat是所有以文件作为数据源的InputFormat实现的基类,FileInput ...
随机推荐
- PHP问题
/usr/bin/ld: cannot find -lltdlcollect2: ld returned 1 exit statusmake: *** [libphp5.la] 错误 1 缺少libt ...
- Record:逻辑分区下新建简单卷后其他卷被删除
上图是恢复后的磁盘情况,恢复前的情况没有截图. 事情是这样:扩展分区中原本有4个逻辑分区.想将其中一个分区(MySpace,第一个分区)压缩出部分空间新建一个分区.经过 压缩卷->新建简单卷 后 ...
- H5小内容(三)
Canvas(画布) 基本内容 简单来说,HTML5提供的新元素<canvas> Canvas在HTML页面提供画布的功能 在画布中绘制各种图形 C ...
- IE6下解决select层级高的问题
div在IE6下无法遮盖select,原因是在IE6下,浏览器将select元素视为窗口级元素,这时div或者其它的普通元素无论z-index设置的多高都是无法遮住select元素的. 解决方法有三种 ...
- MVVM Light 一个窗口承载两个视图
MVVM Light 一个窗口承载两个视图 原文地址:http://www.codeproject.com/Articles/323187/MVVMLight-Using-Two-Views 本文 ...
- 丢沙包游戏(或杀人游戏)的C语言实现
丢沙包游戏(或杀人游戏)用C语言实现: 游戏简述: 杀人游戏(或者丢沙包游戏),设定一些人(人数为:num)一起玩游戏,从某个指定的人(设定为:start)开始轮流扔沙包,扔沙包人的下一个人为1,每隔 ...
- S3C2440触摸屏控制总结
触摸屏控制原理,其实与ADC读取一个滑动变阻器中间触点电压的原理一样.只不过,读取触摸屏的X.Y方向上的电压需要两次,而且需要设置其工作模式以实现一个ADC读取两个通道的电压. S3C2440的ADC ...
- 定位 - CoreLocation - INTULocationManager
https://github.com/intuit/LocationManager #import "ViewController.h" #import "INTULoc ...
- aircrack-ng on OSX 从零开始之探测
继续上一篇内容,在安装好aircrack-ng之后,就要学习如何对目标进行探测了.找了篇教程跟着学习一下吧.其实网上关于使用aircrack-ng的教程还是很多的,我也参考了很多,不过最后还是以官方的 ...
- js 正则 非负整数
Javascript 正则表达式 非负整数 /** * 正则判断非负整数 */ function testnum(ob){ var reg=/^[0-9]+?$/; //如果正则需要判断非负整数并带2 ...