hadoop 通过cachefile来避免数据倾斜


package hello_hadoop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.filecache.DistributedCache;
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.input.FileSplit;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
public class GetFileName {
	//得到处理的文件名,以及将需要的文件缓存到相应的节点
	private final static  Log LOG = LogFactory.getLog(GetFileName.class);
	public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException, URISyntaxException {
		LOG.info("Go into the main method ......");
		Configuration conf = new Configuration();
		Job job = Job.getInstance(conf);
		
		job.setJarByClass(GetFileName.class);
		job.setMapOutputKeyClass(Text.class);
		job.setMapOutputValueClass(Text.class);
		job.setMapperClass(GetFileNameMapper.class);
		job.setNumReduceTasks(0);
		//集群上添加DistributedCache data134:9000namenode的描述   #thelinkofthefile改文件的链接,下面读取的时候需要使用
		job.addCacheFile(new URI("hdfs://data134:9000/home/tmp.txt#thelinkofthefile"));
		FileInputFormat.addInputPath(job, new Path(args[0]));
		FileOutputFormat.setOutputPath(job, new Path(args[1]));
		boolean test = job.waitForCompletion(true);
		LOG.info("End  the main method ......");
		System.exit(test?0:1);		
	}
}
class GetFileNameMapper extends Mapper<LongWritable, Text, Text, Text>{
	private final Log LOG = LogFactory.getLog(GetFileNameMapper.class);
	
	@Override
	protected void setup(Mapper<LongWritable, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
		if(context.getCacheFiles().length>0);
		URI u = context.getCacheFiles()[0];
		//这里使用链接来访问文件
		BufferedReader br = new BufferedReader( new FileReader(new File("./thelinkofthefile")));
		String line = br.readLine();
		context.write(new Text(line), new Text());
		System.out.println("Here I read Line :"+line);
	}
	@Override
	protected void map(LongWritable key, Text value, Mapper<LongWritable, Text, Text, Text>.Context context)
			throws IOException, InterruptedException {
	}
}


来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/31506529/viewspace-2213417/,如需转载,请注明出处,否则将追究法律责任。

转载于:http://blog.itpub.net/31506529/viewspace-2213417/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值