这篇文章主要介绍了hadoop如何通过cachefile来避免数据倾斜,具有一定借鉴价值,感兴趣的朋友可以参考下,希望大家阅读完这篇文章之后大有收获,下面让小编带着大家一起了解一下。
10年积累的成都网站设计、成都做网站、外贸网站建设经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先网站制作后付款的网站建设流程,更有沙湾免费网站建设让你可以放心的选择与我们合作。
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{ private final Log LOG = LogFactory.getLog(GetFileNameMapper.class); @Override protected void setup(Mapper .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 .Context context) throws IOException, InterruptedException { } }
感谢你能够认真阅读完这篇文章,希望小编分享的“hadoop如何通过cachefile来避免数据倾斜”这篇文章对大家有帮助,同时也希望大家多多支持创新互联,关注创新互联行业资讯频道,更多相关知识等着你来学习!