第四个MapReduce程序----两表join
来源:互联网 发布:tiny core linux 编辑:程序博客网 时间:2024/05/16 14:29
需求:两个表,一个有id和name,一个有id和phoneNum,得到name和与之对应的phoneNum
package club.drguo.mapreduce.joinquery;import java.io.IOException;import java.util.ArrayList;import org.apache.commons.lang.StringUtils;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.mapred.FileSplit;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 JoinQuery {public static class JoinQueryMapper extends Mapper<LongWritable, Text, Text, Text> {@Overrideprotected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)throws IOException, InterruptedException {String line = value.toString();String[] strings = StringUtils.split(line, " ");String id = strings[0];String phoneNum = strings[1];FileSplit fileSplit = (FileSplit) context.getInputSplit();String filename = fileSplit.getPath().getName();context.write(new Text(id), new Text(phoneNum + "---" + filename));// K:001// V:123---a.txt}}public static class JoinQueryReducer extends Reducer<Text, Text, Text, Text> {// K:001 V:{123---a.txt,124---b.txt}@Overrideprotected void reduce(Text key, Iterable<Text> values, Reducer<Text, Text, Text, Text>.Context context)throws IOException, InterruptedException {// 第一次循环拿出a表中的那个字段String leftKey = "";ArrayList<String> rightFields = new ArrayList<>();for (Text value : values) {if (value.toString().contains("a.txt")) {leftKey = StringUtils.split(value.toString(), "---")[0];} else {rightFields.add(value.toString());}}// 再用leftkey去遍历拼接b表中的字段,并输出结果for (String field : rightFields) {String result = "";result += leftKey + "\t" + StringUtils.split(field.toString(), "---")[0];context.write(new Text(leftKey), new Text(result));}}}public static void main(String[] args) throws Exception {Configuration conf = new Configuration();Job joinjob = Job.getInstance(conf);joinjob.setJarByClass(JoinQuery.class);joinjob.setMapperClass(JoinQueryMapper.class);joinjob.setReducerClass(JoinQueryReducer.class);joinjob.setOutputKeyClass(Text.class);joinjob.setOutputValueClass(Text.class);FileInputFormat.setInputPaths(joinjob, new Path(args[0]));FileOutputFormat.setOutputPath(joinjob, new Path(args[1]));joinjob.waitForCompletion(true);}}
0 0
- 第四个MapReduce程序----两表join
- MapReduce 中的两表 join
- MapReduce数据处理两表join连接
- MapReduce数据处理两表join连接
- MapReduce的两表join一般操作
- MapReduce的两表join操作优化
- MapReduce 中的两表 join 几种方案简介
- MapReduce 中的两表 join 几种方案简介
- MapReduce 中的两表 join 几种方案简介
- MapReduce 中的两表 join 几种方案简介
- MapReduce 中的两表 join 几种方案简介
- MapReduce 中的两表 join 几种方案简介
- MapReduce数据处理两表join连接 (Ruduce端连接)
- MapReduce 中的两表 join 几种方案简介
- MapReduce中的两表join几种方案简介
- MapReduce中的两表join几种方案简介
- MapReduce中的两表join几种方案简介
- MapReduce 中的两表 join 几种方案简介
- GOOGLE VR SDK开发VR游戏
- 理解长短期记忆模型LSTM
- 设置文字段落属性
- 大华摄像头视频接入(一)
- CSS 元素垂直居中的 6种方法(转)
- 第四个MapReduce程序----两表join
- VR场景优化,知易行难
- zjnu1735BOB (单调队列,单调栈)
- 毕业设计之甘特图制作
- tomcat单机多实例部署
- 天声人語 20160324 春を切り裂くテロ
- hbuilder git 新建共享项目
- HDOJ 2206 IP的计算(正则表达式的应用)
- didFinishLaunchingWithOptions参数launchOptions内容