MapReduce使用DistributedCache关联大表和小表

package com.oracle.join;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.HashMap;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.LongWritable;
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 JoinMapMain {
	public static class Map extends Mapper<LongWritable, Text, Text, Text>{
		private HashMap<String,String> map = new HashMap<String, String>();
		@Override
		protected void setup(Context context)
				throws IOException, InterruptedException {
		 	//从缓存中读取
			Path[] distributePaths = DistributedCache.getLocalCacheFiles(context.getConfiguration());
			BufferedReader br = null;
			
			String line = null;
			for(Path path:distributePaths){
				map.put("1", path.toString());
				//上传路径必须具体到指定某个文件
				if(path.toString().endsWith("datas.txt")){
				br = new BufferedReader(new FileReader(path.toString()));
					while(null!=(line = br.readLine())){
						String[] lines = line.split(" ");
						map.put(lines[0], lines[1]);
					}
				}
			}
		}
		@Override
		protected void map(LongWritable key, Text value,Context context)
				throws IOException, InterruptedException {
			context.write(new Text("0"), new Text(map.get("1")));
			
		}
	}
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
		Configuration conf = new Configuration();
		//将路径放入缓存中
		DistributedCache.addCacheFile(new Path(args[2]).toUri(), conf);
		FileSystem file = FileSystem.get(conf);
		if(file.exists(new Path(args[1]))){
			file.delete(new Path(args[1]),true);
		}
		Job job = Job.getInstance(conf);
		
		job.setJobName("JoinMapMain");
		job.setJarByClass(JoinMapMain.class);
		
		job.setMapperClass(Map.class);
		
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		
		
		FileInputFormat.setInputPaths(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		
		job.waitForCompletion(true);
	}
}





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值