Hadoop使用DistributedCache进行join

本文介绍如何利用DistributedCache实现数据关联操作,通过在Map任务中读取缓存文件,实现数据过滤与合并,确保操作高效且内存消耗合理。重点展示了文件加载与数据处理流程,以及如何在Mapper初始化阶段获取并利用DistributedCache中的文件。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

使用DistributedCache有一个前提,就是进行join的数据有一个足够小,可以装入内存中。注意我们可以从代码中看出它是如何被装入内存中的,因此,我们也可以在装入的过程中进行过滤。但是值得指出的是,如果文件很大,那么装入内存中也是很费时的。

DistributedCache的原理是将小的那个文件复制到所有节点上。

我们使用DistributedCache.addCacheFile()来设定要传播的文件,然后在mapper的初始化方法setup中取用DistributedCache.getLocalCacheFiles()方法获取该文件并装入内存中。

  1. import java.io.BufferedReader;  
  2. import java.io.FileReader;  
  3. import java.io.IOException;  
  4. import java.util.HashMap;  
  5.   
  6. import org.apache.Hadoop.conf.Configuration;  
  7. import org.apache.hadoop.conf.Configured;  
  8. import org.apache.hadoop.filecache.DistributedCache;  
  9. import org.apache.hadoop.fs.Path;  
  10. import org.apache.hadoop.io.LongWritable;  
  11. import org.apache.hadoop.io.Text;  
  12. import org.apache.hadoop.mapred.JobClient;  
  13. import org.apache.hadoop.mapreduce.Job;  
  14. import org.apache.hadoop.mapreduce.Mapper;  
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;  
  16. import org.apache.hadoop.mapreduce.lib.input.KeyValueTextInputFormat;  
  17. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;  
  18. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;  
  19. import org.apache.hadoop.util.Tool;  
  20. import org.apache.hadoop.util.ToolRunner;  
  21.   
  22.   
  23. public class DistributedCacheJoin extends Configured implements Tool{  
  24.   
  25.       
  26.     public static class MyMapper extends Mapper<Text,Text,Text,Text>{  
  27.         private HashMap<String,String> joinData = new HashMap<String,String>();  
  28.         public void map(Text key, Text value, Context context)  
  29.             throws IOException,InterruptedException{  
  30.             String joinValue = joinData.get(key.toString());//注意要toString(),hashcode()你懂的   
  31.             if(null != joinValue){  
  32.                 context.write(key, new Text(joinValue + ","+ value.toString()));  
  33.             }  
  34.         }  
  35.           
  36.         public void setup(Context context){  
  37.             try {  
  38.                 Path [] cacheFiles = DistributedCache.getLocalCacheFiles(context.getConfiguration());  
  39.                 if(null != cacheFiles  && cacheFiles.length > 0){  
  40.                     String line;  
  41.                     String []tokens;  
  42.                     BufferedReader br = new BufferedReader(new FileReader(cacheFiles[0].toString()));  
  43.                     try{  
  44.                         while((line = br.readLine()) != null){  
  45.                             tokens = line.split(","2);  
  46.                             joinData.put(tokens[0], tokens[1]);  
  47.                               
  48.                         }  
  49.                     }finally{  
  50.                         br.close();  
  51.                     }  
  52.                 }  
  53.             } catch (IOException e) {  
  54.                 e.printStackTrace();  
  55.             }  
  56.         }  
  57.     }  
  58.     public int run(String[] args) throws Exception {  
  59.         Configuration conf = getConf();  
  60.         Job job = new Job(conf,"DistributedCacheJoin");  
  61.         job.setJarByClass(DistributedCacheJoin.class);  
  62.           
  63.         DistributedCache.addCacheFile(new Path(args[0]).toUri(), job.getConfiguration());  
  64.         Path in = new Path(args[1]);  
  65.         Path out = new Path(args[2]);  
  66.           
  67.           
  68.         FileInputFormat.setInputPaths(job, in);  
  69.         FileOutputFormat.setOutputPath(job, out);  
  70.           
  71.         job.setMapperClass(MyMapper.class);  
  72.         job.setNumReduceTasks(0);  
  73.         job.setInputFormatClass(KeyValueTextInputFormat.class);  
  74.         job.setOutputFormatClass(TextOutputFormat.class);  
  75.         job.getConfiguration()  
  76.             .set("mapreduce.input.keyvaluelinerecordreader.key.value.separator"",");  
  77.         //在新API 中不再是key.value.separator.in.input.line,你可以在源码KeyValueLineRecordReader.java中看见。   
  78.         System.exit(job.waitForCompletion(true)?0:1);  
  79.           
  80.         return 0;  
  81.     }  
  82.       
  83.     public static void main(String args[]) throws Exception{  
  84.         int res = ToolRunner.run(new Configuration(), new DistributedCacheJoin(), args);  
  85.         System.exit(res);  
  86.     }  
  87.       
  88. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值