gpt4 book ai didi

hadoop DistributedCache 返回 null

转载 作者:可可西里 更新时间:2023-11-01 16:22:04 24 4
gpt4 key购买 nike

我正在使用 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。我可以通过

看到我的缓存文件

System.out.println("缓存:"+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(上下文
.getConfiguration());

uris 返回:hdfs://localhost:8022/user/administrator/myfile

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

最佳答案

分布式缓存会将您的文件从HDFS 复制到所有TaskTracker 的本地文件系统。你如何阅读文件?如果文件在 HDFS 中,您将必须获取 HDFS 文件系统,否则它将使用默认值(可能是本地文件系统)。所以要读取 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)));

关于hadoop DistributedCache 返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17257023/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com