当前位置:  开发笔记 > 大数据 > 正文

hadoop mapreduce多表关联

hadoopmapreduce多表关联假设有如下两个文件,一个是表是公司和地址的序号的对应,一个表是地址的序号和地址的名称的对应。表1:[plain]A:BeijingRedStar1A:ShenzhenThunder3A:GuangzhouHonda2A:BeijingRising1A:GuangzhouDevelopmentBa

hadoop mapreduce多表关联 假设有如下两个文件,一个是表是公司和地址的序号的对应,一个表是地址的序号和地址的名称的对应。 表1: [plain] A:Beijing Red Star 1 A:Shenzhen Thunder 3 A:Guangzhou Honda 2 A:Beijing Rising 1 A:Guangzhou Development Ba

hadoop mapreduce多表关联

假设有如下两个文件,一个是表是公司和地址的序号的对应,一个表是地址的序号和地址的名称的对应。

表1:

[plain]

A:Beijing Red Star 1

A:Shenzhen Thunder 3

A:Guangzhou Honda 2

A:Beijing Rising 1

A:Guangzhou Development Bank 2

A:Tencent 3

A:Back of Beijing 1

表2:

[plain]

B:1 Beijing

B:2 Guangzhou

B:3 Shenzhen

B:4 Xian

mapreduce如下:

[plain]

private static final Text typeA = new Text("A:");

private static final Text typeB = new Text("B:");

private static Log log = LogFactory.getLog(MTJoin.class);

public static class Map extends Mapper {

public void map(Object key, Text value, Context context)

throws IOException, InterruptedException {

String valueStr = value.toString();

String type = valueStr.substring(0, 2);

String content = valueStr.substring(2);

log.info(content);

if(type.equals("A:"))

{

String[] contentArray = content.split("\t");

String city = contentArray[0];

String address = contentArray[1];

MapWritable map = new MapWritable();

map.put(typeA, new Text(city));

context.write(new Text(address), map);

}

else if(type.equals("B:"))

{

String[] contentArray = content.split("\t");

String adrNum = contentArray[0];

String adrName = contentArray[1];

MapWritable map = new MapWritable();

map.put(typeB, new Text(adrName));

context.write(new Text(adrNum), map);

}

}

}

public static class Reduce extends Reducer {

public void reduce(Text key, Iterable values, Context context)

throws IOException, InterruptedException {

Iterator it = values.iterator();

List cityList = new ArrayList();

List adrList = new ArrayList();

while(it.hasNext())

{

MapWritable map = it.next();

if(map.containsKey(typeA))

{

cityList.add((Text)map.get(typeA));

}

else if(map.containsKey(typeB))

{

adrList.add((Text)map.get(typeB));

}

}

for(int i = 0; i < cityList.size(); i++)

{

for(int j = 0; j < adrList.size(); j++)

{

context.write(cityList.get(i), adrList.get(j));

}

}

}

}

原理很简单,map的出口,以地址的序号作为key,然后出来的时候,公司名称放一个list,地址的名称放一个list,两个list的内容作笛卡儿积,就得到了结果。

输出如下:

[plain]

Beijing Red Star Beijing

Beijing Rising Beijing

Back of Beijing Beijing

Guangzhou Honda Guangzhou

Guangzhou Development Bank Guangzhou

Shenzhen Thunder Shenzhen

Tencent Shenzhen

推荐阅读
牛尾巴2010
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有