Java 文件未添加到DistributedCache
我正在eclipse环境中的本地系统上运行Hadoop 我尝试将工作区中的本地文件放入驱动程序函数中的分布式缓存中,如下所示:Java 文件未添加到DistributedCache,java,hadoop,mapreduce,Java,Hadoop,Mapreduce,我正在eclipse环境中的本地系统上运行Hadoop 我尝试将工作区中的本地文件放入驱动程序函数中的分布式缓存中,如下所示: DistributedCache.addCacheFile(new Path( "/home/hduser/workspace/myDir/myFile").toUri(), conf); 但当我试图从Mapper访问它时,它返回null。 在mapper中,我检查了文件是否缓存 System.out.println("Cache: "+con
DistributedCache.addCacheFile(new Path(
"/home/hduser/workspace/myDir/myFile").toUri(), conf);
但当我试图从Mapper访问它时,它返回null。
在mapper中,我检查了文件是否缓存
System.out.println("Cache: "+context.getConfiguration().get("mapred.cache.files"));
它还打印“null”
返回null
出什么问题了?这是因为您只能从HDFS而不是本地文件系统向分布式缓存添加文件。所以路径不存在。将文件放在HDFS上,并在添加到DistributedCache时使用HDFS路径引用它
有关详细信息,请参阅。添加缓存文件时在路径中添加文件:/ DistributedCache.addCacheFile(新路径(“file:///home/hduser/workspace/myDir/myFile",conf), 试试这个 驾驶员等级
Path p = new Path(your/file/path);
FileStatus[] list = fs.globStatus(p);
for (FileStatus status : list) {
/*Storing file to distributed cache*/
DistributedCache.addCacheFile(status.getPath().toUri(), conf);
}
映射器类
public void setup(Context context) throws IOException{
/*Accessing data in file */
Configuration conf = context.getConfiguration();
FileSystem fs = FileSystem.get(conf);
URI[] cacheFiles = DistributedCache.getCacheFiles(conf);
/* Accessing 0 th cached file*/
Path getPath = new Path(cacheFiles[0].getPath());
/*Read data*/
BufferedReader bf = new BufferedReader(new InputStreamReader(fs.open(getPath)));
String setupData = null;
while ((setupData = bf.readLine()) != null) {
/*Print file content*/
System.out.println("Setup Line "+setupData);
}
bf.close();
}
public void map(){
}
getCacheFiles()返回原始路径。不是复制文件的本地路径。此解决方案绕过分布式缓存本身
public void setup(Context context) throws IOException{
/*Accessing data in file */
Configuration conf = context.getConfiguration();
FileSystem fs = FileSystem.get(conf);
URI[] cacheFiles = DistributedCache.getCacheFiles(conf);
/* Accessing 0 th cached file*/
Path getPath = new Path(cacheFiles[0].getPath());
/*Read data*/
BufferedReader bf = new BufferedReader(new InputStreamReader(fs.open(getPath)));
String setupData = null;
while ((setupData = bf.readLine()) != null) {
/*Print file content*/
System.out.println("Setup Line "+setupData);
}
bf.close();
}
public void map(){
}