Hadoop中的DistributedCache
来源:互联网 发布:ubuntu caffe 教程 编辑:程序博客网 时间:2024/05/11 20:49
1、DistributedCache In Hadoop
此篇文章主要是前一篇的后续,主要讲Hadoop的分布式缓存机制的原理与运用。
分布式缓存在MapReduce中称之为DistributedCache,它可以方便map task之间或者reduce task之间共享一些信息,同时也可以将第三方包添加到其classpath路径中去。Hadoop会将缓存数据分发到集群的所有准备启动的节点上,复制到在mapred.temp.dir中配置的目录。
2、DistributedCache的使用
DistributedCache的使用的本质其实是添加Configuraton中的属性:mapred.cache.{files|archives}。图方便的话,可以使用DistributedCache类的静态方法。
不省事法:
conf.set("mapred.cache.files", "/data/data");
conf.set("mapred.cache. archives", "/data/data.zip");
省事法:
DistributedCache. addCacheFile
(URI,
Configuration)
DistributedCache.addArchiveToClassPath
(Path,
Configuration,
FileSystem)
需要注意的是,上面几行代码需要写在Job类初始化之前,否则在运行会中找不到文件(被折磨了很长时间),因为Job初始化时将传入Configuration对象克隆一份给了JobContext。
在MapReduce的0.21版本以后的org.apache.hadoop.mapreduce均移到org.apache.hadoop.mapred包下。但文档中提供的configure方法是重写的MapReduceBase中的,而新版本中map继承于mapper,reduce继承于reducer,所以configure方法一律改成了setup。要获得cache数据,就得在map/reduce task中的setup方法中取得cache数据,再进行相应操作:
- @Override
- protected void setup(Context context) throws IOException,
- InterruptedException {
- super.setup(context);
- URI[] uris = DistributedCache.getCacheFiles(context
- .getConfiguration());
- Path[] paths = DistributedCache.getLocalCacheFiles(context
- .getConfiguration());
- // TODO
- }
而三方库的使用稍微简单,只需要将库上传至hdfs,再用代码添加至classpath即可:
DistributedCache.addArchiveToClassPath(new Path("/data/test.jar"), conf);
3、symlink的使用
Symlink其实就是hdfs文件的一个快捷方式,只需要在路径名后加入#linkname,之后在task中使用linkname即使用相应文件,如下:
conf.set("mapred.cache.files", "/data/data#mData");
conf.set("mapred.cache. archives", "/data/data.zip#mDataZip");
- @Override
- protected void setup(Context context) throws IOException,
- InterruptedException {
- super.setup(context);
- FileReader reader = new FileReader(new File("mData"));
- BufferedReader bReader = new BufferedReader(reader);
- // TODO
- }
在使用symlink之前,需要告知hadoop,如下:
conf.set("mapred.create.symlink", "yes"); // 是yes,不是true
DistributedCache.createSymlink(Configuration)
4、注意事项
1)缓存文件(数据、三方库)需上传至HDFS,方能使用;
2)缓存较小的情况下,建议将数据全部读入相应节点内存,提高访问速度;
3)缓存文件是read-only的,不能修改。若要修改得重新输出,将新输出文件作为新缓存进入下一次迭代。
- hadoop中的DistributedCache
- Hadoop中的DistributedCache
- Hadoop中的DistributedCache
- Hadoop DistributedCache
- Hadoop DistributedCache
- Hadoop DistributedCache
- hadoop DistributedCache
- hadoop中的分布式缓存——DistributedCache
- hadoop中的分布式缓存——DistributedCache
- Using Hadoop’s DistributedCache
- hadoop distributedcache使用
- hadoop DistributedCache的使用
- hadoop的DistributedCache
- Hadoop DistributedCache详解
- Hadoop DistributedCache使用案例
- Hadoop分布式缓存(DistributedCache)
- Hadoop分布式缓存(DistributedCache)
- Hadoop DistributedCache详解
- android摄像头(camera)之 V4L2编程中用到的各种结构体
- Jquery
- JSP
- Ext4 日期控件为[Y-m]格式,选择2月变3月解决方案
- Linux
- Hadoop中的DistributedCache
- 单例模式(单一实例) C++版本
- HTMl5的sessionStorage和localStorage
- [转载]addSubview和insertSubview的区别
- 使用jquery操作iframe
- 监听器
- 内存映射
- 应用程序之间的交互
- 欲为Java技术大牛所需的25个学习要点