1.需求
1001 pd001 300
1001 pd002 20
1002 pd003 40
1003 pd002 50
products
pd001,apple
pd002,banana
pd003,orange
2.思路
3.代碼
public class MJoin {
static class MJoinMapper extends Mapper<LongWritable, Text, Text, NullWritable>{
Map<String,String> pdInfoMap = new HashMap<String,String>();
@Override
protected void setup(Context context)throws IOException, InterruptedException {
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("pdts.txt")));
String line;
while(StringUtils.isNotEmpty(line = br.readLine())){
String fields[] = line.split(",");
pdInfoMap.put(fields[0], fields[1]);
}
}
@Override
protected void map(LongWritable key, Text value,Context context)
throws IOException, InterruptedException {
String orderLine = value.toString();
String fields[] = orderLine.split("\t");
String pdName = pdInfoMap.get(fields[1]);
Text k = new Text(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(MJoin.class);
job.setMapperClass(MJoinMapper.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(NullWritable.class);
FileInputFormat.setInputPaths(job, new Path("H:/大數據/mapreduce/mapjoin/input"));
FileOutputFormat.setOutputPath(job, new Path("H:/大數據/mapreduce/mapjoin/output"));
job.addCacheFile(new URI("file:/H:/大數據/mapreduce/mapjoin/pdts.txt"));
job.setNumReduceTasks(0);
boolean res = job.waitForCompletion(true);
System.exit(res ? 0 : 1);
}
}
4.輸出
1001 pd001 300 apple
1001 pd002 20 banana
1002 pd003 40 orange
1003 pd002 50 banana