我正在使用hadoop DistributedCache,但我遇到了一些麻烦。 我的hadoop处于伪分布模式。
首先我将我的文件放入DistributedCache:
DistributedCache.addCacheFile(new Path(
"hdfs://localhost:8022/user/administrator/myfile").toUri(),
job.getConfiguration());
然后在mapper setup()中检索,但DistributedCache.getLocalCache
返回null.i可以通过
System.out.println("Cache: "+context.getConfiguration().get("mapred.cache.files"));
并打印出来:
hdfs://localhost:8022/user/administrator/myfile
这是我的伪代码:
public static class JoinMapper{
@Override
protected void setup(Context context){
Path[] cacheFiles = DistributedCache.getLocalCacheFiles(context
.getConfiguration());
System.out.println("Cache
:"+context.getConfiguration().get("mapred.cache.files"));
Path cacheFile;
if (cacheFiles != null) {}
}
}
xx....
public static void main(String[] args){
Job job = new Job(conf, "Join Test");
DistributedCache.addCacheFile(new Path("hdfs://localhost:8022/user/administrator/myfile").toUri(),
job.getConfiguration());}
抱歉排版不好。请帮忙......
不过,我可以使用获得uris URI[] uris = DistributedCache.getCacheFiles(context
.getConfiguration());
当我尝试从uri读取时,错误文件未找到异常。
答案 0 :(得分:-1)
分布式缓存会将文件从HDFS复制到所有TaskTracker的本地文件系统。 你怎么读这个文件?如果文件是HDFS,则必须获取HDFS FileSystem,否则将使用默认值(可能是本地文件)。所以要在HDFS中读取文件,请尝试:
FileSystem fs = FileSystem.get(new Path("hdfs://localhost:8022/user/administrator/myfile").toUri(), new Configuration());
Path path = new Path (url);
BufferedReader br = new BufferedReader(new InputStreamReader(fs.open(path)));