小标
2019-01-09
来源 :
阅读 1905
评论 0
摘要:本文主要向大家介绍了JAVA语言之java map端 实现join代码实例,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
本文主要向大家介绍了JAVA语言之java map端 实现join代码实例,通过具体的内容向大家展示,希望对大家学习JAVA语言有所帮助。
package cn.smart.bigdata.mr.mapsidejoin;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;
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.NullWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class MapSideJoin {
static class MapsideJoinMapper extends Mapper<longwritable, nullwritable="">{
Map<string, string=""> pdInfoMap = new HashMap<>();
Text k = new Text();
@Override
protected void setup(Mapper<longwritable, nullwritable="">.Context context)
throws IOException, InterruptedException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("/D:/data/mapjoincache/pdts.txt")));
String line;
while(StringUtils.isNotBlank(line=br.readLine())){
String[] fields = line.split(",");
pdInfoMap.put(fields[0], fields[1]);
}
br.close();
}
@Override
protected void map(LongWritable key, Text value, Mapper<longwritable, nullwritable="">.Context context)
throws IOException, InterruptedException {
String orderLine = value.toString();
String[] fields = orderLine.split("\t");
String pdName = pdInfoMap.get(fields[1]);
k.set(orderLine + "\t" + pdName);
context.write(k, NullWritable.get());
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
Job job = Job.getInstance(conf);
job.setJarByClass(MapSideJoin.class);
job.setMapperClass(MapsideJoinMapper.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
FileInputFormat.setInputPaths(job, new Path("D:/data/mapjoininput"));
FileOutputFormat.setOutputPath(job, new Path("D:/data/mapjoinoutput"));
job.addCacheFile(new URI("file:/D:/data/mapjoincache/pdts.txt"));
job.setNumReduceTasks(0);
boolean res = job.waitForCompletion(true);
System.exit(res?1:0);
}
}
pdts.txt
pd001,apple
pd002,banana
pd003,orange
order.txt
1001 pd001 300
1001 pd002 20
1002 pd003 40
1003 pd002 50
本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注编程语言JAVA频道!
喜欢 | 1
不喜欢 | 0
您输入的评论内容中包含违禁敏感词
我知道了

请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式AI+学习就业服务平台 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号