Hadoop--Map/Reduce实现多表链接
MR实现多表连接的原理和单表连接时一样的,甚至比单表连接还要简单。
在map阶段只需要根据文件的名称区分左表还是右表。使用关联的字段作为key2。
在reduce中对values中的值分别存储到一个左表list和右表list中。对左表list和右表list进行一个笛卡尔积完事。
import java.io.*;
import java.util.*; import org.apache.hadoop.io.*;
import org.apache.hadoop.util.*;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.mapreduce.*;
import org.apache.hadoop.mapreduce.lib.input.*;
import org.apache.hadoop.mapreduce.lib.output.*;
import org.apache.hadoop.conf.*;
import org.apache.hadoop.util.Tool;
public class MTjoin extends Configured implements Tool {
/*
* 多表链接,与单表链接思路类似。将关联列作为map的key值,用数字区分左表和右表。在Reduce阶段对两个表进行笛卡尔积
* */
public static class Map extends Mapper<LongWritable,Text,Text,Text>{
public void map(LongWritable key,Text value,Context context)throws IOException,InterruptedException{
String line=value.toString();
int linelen=line.length();
//去除文件首行
if(line.indexOf("factoryname")==-1&&line.indexOf("addressID")==-1)
{
//处理factory数据
if(line.charAt(linelen-2)==' ')
{
String facstr="1"+line.substring(0, linelen-2);
String addrestr=String.valueOf(line.charAt(linelen-1));
context.write(new Text(addrestr), new Text(facstr));
}else{
String addreidstr=String.valueOf(line.charAt(0));
String addrenastr="2"+line.substring(1);
context.write(new Text(addreidstr), new Text(addrenastr));
} }
} } public static class Reduce extends Reducer<Text,Text,Text,Text>{
public void reduce(Text key,Iterable<Text> values,Context context)throws IOException, InterruptedException{
ArrayList<String> facarr=new ArrayList<String>();
ArrayList<String> addarr=new ArrayList<String>();
for(Text var:values){
if(var.toString().charAt(0)=='1')
{
facarr.add(var.toString().substring(1));
}else if(var.toString().charAt(0)=='2')
{
addarr.add(var.toString().substring(1));
} }
if(facarr.size()!=0&&addarr.size()!=0)
{
for(int i=0;i<facarr.size();i++)
{
context.write(new Text(facarr.get(i)), new Text(addarr.get(0)));
} }
}
}
@Override
public int run(String[] args) throws Exception {
// TODO Auto-generated method stub
Configuration conf=new Configuration();
Job job=new Job(conf,"MTjoin");
job.setJarByClass(MTjoin.class); job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class); job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class); job.setInputFormatClass(TextInputFormat.class);
job.setOutputFormatClass(TextOutputFormat.class); FileInputFormat.setInputPaths(job, new Path(args[0]));
FileOutputFormat.setOutputPath(job, new Path(args[1])); boolean success=job.waitForCompletion(true);
return success?0:1;
}
public static void main(String[] args)throws Exception{
int ret=ToolRunner.run(new MTjoin(), args);
System.exit(ret);
} }
Hadoop--Map/Reduce实现多表链接的更多相关文章
- 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 ...
- hadoop map reduce 实例wordcount的使用
hadoop jar hadoop-mapreduce-examples-2.7.3.jar wordcount /wordcount.txt /wc/output3
随机推荐
- 《JavaScript高级程序设计》 阅读计划
第一周 第1章 JavaScript简介 1 第2章 在Html中使用JavaScript 1 第3章 基本概念 3 第二周 第4章 变量.作用域和内存 ...
- cmake,gtest单元测试程序
参考:http://blog.csdn.net/stdcoutzyx/article/details/8284183 PROJECT (HELLO) SET(SRC_LIST main.c) MESS ...
- Linux 内核学习的经典书籍及途径
from:http://www.zhihu.com/question/19606660 知乎 Linux 内核学习的经典书籍及途径?修改 修改 写补充说明 举报 添加评论 分享 • 邀请回答 ...
- session 保存在指定的数据表,cookie设置
首先建立数据表,可在ThinkPHP/Extend/Driver/Session/SessionDb.class.php中copy代码 在配置文件中配置: 'SESSION_TYPE' => ' ...
- php获取文件mime类型Fileinfo等方法
前几天写到使用wordpress xmlrpc api远程发布文章,如果本地服务器的文章库里某一篇待发表的wordpress文章包含图片文件时,就会使用到WordPress上传文件的API metaW ...
- PHP源码阅读笔记一(explode和implode函数分析)
PHP源码阅读笔记一一.explode和implode函数array explode ( string separator, string string [, int limit] )此函数返回由字符 ...
- mvc4 membership, [Win32Exception (0x80004005): The system cannot find the file specified]
public class UsersContext : DbContext { public UsersContext() : base("conn1") //change the ...
- java+eclipse+tomcat+mysql+jdbc——完美配置攻略
说明: 软件均采用最新版本,请大家详细阅读,注意每个细节,无需分门别类的百度各种教程,配置java环境这一篇就够了. 所需软件及版本(参考): java8; - jdk1.8.0_60; - jre1 ...
- 集合工具类 - CollectionUtil.java
集合工具类,提供数组转LIST.数组转SET.合并集合.计算笛卡儿积等方法. 源码如下:(点击下载 - CollectionUtil.java.ArrayUtil.java.commons-lang ...
- 根据WSDL生成代理类方式
方式一: 1.使用VS2010提供的工具wsdl.exe由WSDL文件生成cs文件 使用wsdl.exe的/serverInterface选项(或缩写的 /si)指定输入的wsdl文件(注意,如果要转 ...