设为首页 收藏本站
查看: 1134|回复: 0

[经验分享] hadoop实例---多表关联

[复制链接]

尚未签到

发表于 2015-7-12 11:59:04 | 显示全部楼层 |阅读模式
  多表关联和单表关联类似,它也是通过对原始数据进行一定的处理,从其中挖掘出关心的信息。如下
  输入的是两个文件,一个代表工厂表,包含工厂名列和地址编号列;另一个代表地址表,包含地址名列和地址编号列。要求从输入数据中找出工厂名和地址名的对应关系,输出工厂名-地址名表
  样本如下:
  factory:

factoryname addressed
Beijing Red Star 1
Shenzhen Thunder 3
Guangzhou Honda 2
Beijing Rising 1
Guangzhou Development Bank 2
Tencent 3
Back of Beijing 1

  address:

addressID addressname
1 Beijing
2 Guangzhou
3 Shenzhen
4 Xian

  
结果:

factoryname     addressname
Beijing Red Star        Beijing
Beijing Rising  Beijing
Bank of Beijing         Beijing
Guangzhou Honda         Guangzhou
Guangzhou Development Bank      Guangzhou
Shenzhen Thunder        Shenzhen
Tencent         Shenzhen

  
代码如下:

import java.io.IOException;
import java.util.*;

import org.apache.hadoop.conf.Configuration;
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.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;

public class MTjoin {

public static int time = 0;

/*
* 在map中先区分输入行属于左表还是右表,然后对两列值进行分割,
* 保存连接列在key值,剩余列和左右表标志在value中,最后输出
*/
public static class Map extends Mapper {

// 实现map函数
public void map(Object key, Text value, Context context)
throws IOException, InterruptedException {
String line = value.toString();// 每行文件
String relationtype = new String();// 左右表标识

// 输入文件首行,不处理
if (line.contains("factoryname") == true
|| line.contains("addressed") == true) {
return;
}

// 输入的一行预处理文本
StringTokenizer itr = new StringTokenizer(line);
String mapkey = new String();
String mapvalue = new String();
int i = 0;
while (itr.hasMoreTokens()) {
// 先读取一个单词
String token = itr.nextToken();
// 判断该地址ID就把存到"values[0]"
if (token.charAt(0) >= '0' && token.charAt(0)  0) {
relationtype = "1";
} else {
relationtype = "2";
}
continue;
}

// 存工厂名
mapvalue += token + " ";
i++;
}

// 输出左右表
context.write(new Text(mapkey), new Text(relationtype + "+"+ mapvalue));
}
}

/*
* reduce解析map输出,将value中数据按照左右表分别保存,
  * 然后求出笛卡尔积,并输出。
*/
public static class Reduce extends Reducer {

// 实现reduce函数
public void reduce(Text key, Iterable values, Context context)
throws IOException, InterruptedException {

// 输出表头
if (0 == time) {
context.write(new Text("factoryname"), new Text("addressname"));
time++;
}

int factorynum = 0;
String[] factory = new String[10];
int addressnum = 0;
String[] address = new String[10];

Iterator ite = values.iterator();
while (ite.hasNext()) {
String record = ite.next().toString();
int len = record.length();
int i = 2;
if (0 == len) {
continue;
}

// 取得左右表标识
char relationtype = record.charAt(0);

// 左表
if ('1' == relationtype) {
factory[factorynum] = record.substring(i);
factorynum++;
}

// 右表
if ('2' == relationtype) {
address[addressnum] = record.substring(i);
addressnum++;
}
}

// 求笛卡尔积
if (0 != factorynum && 0 != addressnum) {
for (int m = 0; m < factorynum; m++) {
for (int n = 0; n < addressnum; n++) {
// 输出结果
context.write(new Text(factory[m]),
new Text(address[n]));
}
}
}

}
}

public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
// 这句话很关键
//      conf.set("mapred.job.tracker", "192.168.1.2:9001");

//可使用args
//      String[] ioArgs = new String[] { "MTjoin_in", "MTjoin_out" };
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: Multiple Table Join  ");
System.exit(2);
}

Job job = new Job(conf, "Multiple Table Join");
job.setJarByClass(MTjoin.class);

// 设置Map和Reduce处理类
job.setMapperClass(Map.class);
job.setReducerClass(Reduce.class);

// 设置输出类型
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(Text.class);

// 设置输入和输出目录
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}

javac -classpath hadoop-core-1.1.2.jar:/opt/hadoop-1.1.2/lib/commons-cli-1.2.jar -d firstProject firstProject/MTJoin.java

jar -cvf MTJoin.jar -C firstProject/ .     
  删除已经存在的output

hadoop fs -rmr output

hadoop fs -mkdir input

hadoop fs -put factory input

hadoop fs -put address input

  运行

hadoop jar  MTJoin.jar MTJoin input output

  
查看结果

hadoop fs -cat output/part-r-00000

  


  
  

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-85799-1-1.html 上篇帖子: Hadoop使用常见问题以及解决方法(转载) 下篇帖子: hadoop源代码--DatanodeProtocol
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表