hadoop DistributedCache返回null

时间:2013-06-23 01:50:29

标签: hadoop nullpointerexception distributed-cache

我正在使用hadoop DistributedCache,但我遇到了一些麻烦。 我的hadoop处于伪分布模式。

from here we can see in pseudo-distributed mode we use DistributedCache.getLocalCache(xx) to retrive cached file.

首先我将我的文件放入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());

uris回归: HDFS://本地主机:8022 /用户/管理员/ MYFILE

当我尝试从uri读取时,错误文件未找到异常。

1 个答案:

答案 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)));