hadoop 多表关联

5、多表关联

多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息。下面进入这个实例。

5.1 实例描述

输入是两个文件,一个代表工厂表,包含工厂名列和地址编号列;另一个代表地址表,包含地址名列和地址编号列。要求从输入数据中找出工厂名地址名对应关系,输出"工厂名——地址名"表。

样例输入如下所示。

    a.txt (工厂表)

    f1      3
  f2      2
  f3      1
  b.txt(地址表)

  1       Beijing
  2       Shanghai
  3       Tianjin
  样例输出:

  f3      Beijing
  f2      Shanghai
  f1      Tianjin

5.2 设计思路

多表关联和单表关联相似,都类似于数据库中的自然连接。相比单表关联,多表关联的左右表和连接列更加清楚。所以可以采用和单表关联的相同处理方式,map识别出输入的行属于哪个表之后,对其进行分割,将连接的列值保存在key中,另一列和左右表标识保存在value中,然后输出。reduce拿到连接结果之后,解析value内容,根据标志将左右表内容分开存放,然后求笛卡尔积,最后直接输出。

这个实例的具体分析参考单表关联实例。下面给出代码。

5.3 程序代码

程序代码如下所示:

  

package test;

import java.util.ArrayList;
import java.util.List;

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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class MTjoin {

	public static class Map extends Mapper<LongWritable, Text, Text, Text>{
		private static Text k = new Text();
		private static Text v = new Text();

		protected void map(LongWritable key, Text value, Context context)
				throws java.io.IOException ,InterruptedException {
			String[] splits = value.toString().split("\t");
			if(splits.length != 2){
				return ;
			}

			//取得文件名 a.txt(工厂名字,序号) b.txt(序号,地址)
			String fileName = ((FileSplit)context.getInputSplit()).getPath().getName();
			if("a.txt".equals(fileName)){
				k.set(splits[1]);
				v.set("1"+splits[0]);
			}else if("b.txt".equals(fileName)){
				k.set(splits[0]);
				v.set("2"+splits[1]);
			}else{
				return ;
			}
			context.write(k, v);
		};
	}
	public static class Reduce extends Reducer<Text, Text, Text, Text>{
		private static List<String> names = new ArrayList<String>();
		private static List<String> addrs = new ArrayList<String>();
		private static Text name = new Text();
		private static Text addr = new Text();

		protected void reduce(Text key, Iterable<Text> values, Context context)
				throws java.io.IOException ,InterruptedException {
			for (Text value : values) {
				String temp = value.toString();
				if(temp.startsWith("1")){
					names.add(temp.substring(1));
				}else{
					addrs.add(temp.substring(1));
				}
			}
			for (String n : names) {
				for (String a : addrs) {
					name.set(n);
					addr.set(a);
					context.write(name, addr);
				}
			}
			names.clear();
			addrs.clear();
		};
	}
	public static void main(String[] args) throws Exception {
		Configuration conf = new Configuration();
		String[] otherArgs = new GenericOptionsParser(conf,args).getRemainingArgs();
		if(otherArgs.length != 2){
			System.err.println("Usage:MTjoin");
			System.exit(2);
		}
		Job job = new Job(conf, "MTjoin");
		job.setJarByClass(MTjoin.class);

		job.setMapperClass(Map.class);
		job.setReducerClass(Reduce.class);

		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setOutputKeyClass(Text.class);
		job.setOutputValueClass(Text.class);

		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));

		System.exit(job.waitForCompletion(true) ? 0 : 1);

	}
}

输出结果:

[[email protected] ~]# hadoop dfs -cat /output/*
f3      Beijing
f2      Shanghai
f1      Tianjin

  

hadoop 多表关联

时间: 2024-08-25 07:10:30

hadoop 多表关联的相关文章

Hadoop on Mac with IntelliJ IDEA - 8 单表关联NullPointerException

简化陆喜恒. Hadoop实战(第2版)5.4单表关联的代码时遇到空指向异常,经分析是逻辑问题,在此做个记录. 环境:Mac OS X 10.9.5, IntelliJ IDEA 13.1.5, Hadoop 1.2.1 改好的代码如下,在reduce阶段遇到了NullPointerException. 1 public class STjoinEx { 2 private static final String TIMES = "TIMES"; 3 4 public static v

Hadoop 单表多表关联

单表: package org.bigdata.util; import java.io.IOException; import java.util.ArrayList; import java.util.List; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.ha

20亿与20亿表关联优化方法(超级大表与超级大表join优化方法)

记得5年前遇到一个SQL.就是一个简单的两表关联.SQL跑了几乎相同一天一夜,这两个表都非常巨大.每一个表都有几十个G.数据量每一个表有20多亿,表的字段也特别多. 相信大家也知道SQL慢在哪里了,单个进程的PGA 是绝对放不下几十个G的数据,这就会导致消耗大量temp tablespace,SQL慢就是慢在temp来回来回来回...的读写数据. 遇到这样的超级大表与超级大表怎么优化呢?这篇文章将告诉你答案. 首先创建2个測试表 t1,t2 数据来自dba_objects create tabl

MapReduce编程之实现多表关联

多表关联和单表关联类似.它也是通过对原始数据进行一定的处理.从当中挖掘出关心的信息.例如以下 输入的是两个文件,一个代表工厂表,包括工厂名列和地址编号列:还有一个代表地址表,包括地址名列和地址编号列. 要求从输入数据中找出工厂名和地址名的相应关系.输出工厂名-地址名表 样本例如以下: factory: <span style="font-size:14px;">factoryname addressed Beijing Red Star 1 Shenzhen Thunder

MapReduce应用案例--单表关联

1. 实例描述 单表关联这个实例要求从给出的数据中寻找出所关心的数据,它是对原始数据所包含信息的挖掘. 实例中给出child-parent 表, 求出grandchild-grandparent表. 输入数据 file01: child parent Tom Lucy Tom Jack Jone Lucy Jone Jack Lucy Marry Lucy Ben Jack Alice Jack Jesse Terry Alice Terry Jesse Philip Terry Philip

yii2 ActiveRecord多表关联以及多表关联搜索的实现

一个老生常谈的问题.最近通过群里的反馈,觉得很多人还是没有去理解这个问题.今天把这个问题讲明白了,看看yii2 ActiveRecord是怎么个多表关联以及如何去优化这个关联. 场景需求: 假设我们有一张用户表user和一张用户渠道表auth,两张数据表通过user.id和auth.uid进行一对一关联.现需要在user列表展示auth表的来源渠道source,且该渠道可搜索. 首先我们先通过gii生成user和auth系列相关的model和操作.此处不做详细说明,有关gii的操作可参考gii详

Oracle Update 语句语法与性能分析 - 多表关联

Oracle Update 语句语法与性能分析 - 多表关联 为了方便起见,建立了以下简单模型,和构造了部分测试数据: 在某个业务受理子系统BSS中, SQL 代码 --客户资料表 create table customers ( customer_id number(8) not null, -- 客户标示 city_name varchar2(10) not null, -- 所在城市 customer_type char(2) not null, -- 客户类型 ... ) create

Yii2中多表关联查询(with、join、joinwith)

表结构 现在有客户表.订单表.图书表.作者表, 客户表Customer   (id  customer_name) 订单表Order         (id  order_name   customer_id   book_id) 图书表Book          (id  book_name    author_id) 作者表Author        (id  author_name) 模型定义 下面是这4个个模型的定义,只写出其中的关联 Customer class Customer ex

Hibernate-多表关联查询结果的处理方法

Hibernate多表查询结果处理 (2014-07-06 20:45:40) 转载▼ 标签: hibernate 多表查询 结果集处理 分类: Java 如果我们在Hibernate中需要查询多个表的不同字段,那么如何来获取Hibernate多表查询的结果呢?有两种方式: 1. 对各个字段分别转化成对应类型,如下: Java代码: Query q = session.createQuery(" select members, classInfo.className " + "