欢迎投稿

今日深度:

hadoop中的分布式缓存——DistributedCache,

hadoop中的分布式缓存——DistributedCache,


分布式缓存一个最重要的应用就是在进行join操作的时候,如果一个表很大,另一个表很小很小,我们就可以将这个小表进行广播处理,即每个计算节点上都存一份,然后进行map端的连接操作,经过我的实验验证,这种情况下处理效率大大高于一般的reduce端join,广播处理就运用到了分布式缓存的技术。

DistributedCache将拷贝缓存的文件到Slave节点在任何Job在节点上执行之前,文件在每个Job中只会被拷贝一次,缓存的归档文件会被在Slave节点中解压缩。将本地文件复制到HDFS中去,接着J哦不Client会通过addCacheFile() 和addCacheArchive()方法告诉DistributedCache在HDFS中的位置。当文件存放到文地时,JobClient同样获得DistributedCache来创建符号链接,其形式为文件的URI加fragment标识。当用户需要获得缓存中所有有效文件的列表时,JobConf 的方法 getLocalCacheFiles() 和getLocalArchives()都返回一个指向本地文件路径对象数组。

下面贴一下我的部分代码:

在run函数中

DistributedCache.createSymlink(job.getConfiguration());//
		try {//#的作用是以后用的时候直接input就可以了
			DistributedCache.addCacheFile(new URI(args[1]+"/#input"), job.getConfiguration());
		} catch (URISyntaxException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
		
在map端打开分布式缓存的文件并读如Hashtable中

         
		private Hashtable<String, DefinedMyself> word_hash = new Hashtable<String,DefinedMyself>();
		
		public void setup(Context context) throws IOException, InterruptedException{
			String[] selected_region = null;
			Path p[] = DistributedCache.getLocalCacheFiles(context.getConfiguration());
			
			FileReader reader = new FileReader("input");
			BufferedReader br = new BufferedReader(reader);
			System.out.println("this is OK");
			String s1 = null;
			
			int i=0;
	
			while((s1 = br.readLine())!=null){
				String[] word = s1.split("\\|");

				//do something you want
				}
			}
	
			br.close();
			reader.close();                                                                                         }

至于面试的时候如果让解释一下分布式缓存,我就晕了,不知道什么好解释的,估计我就把代码一列吧,哈哈。




www.htsjk.Com true http://www.htsjk.com/Hadoop/29032.html NewsArticle hadoop中的分布式缓存——DistributedCache, 分布式缓存一个最重要的应用就是在进行join操作的时候,如果一个表很大,另一个表很小很小,我们就可以将这个小表进行广播处理,即每个计...
相关文章
    暂无相关文章
评论暂时关闭