【发布时间】:2016-11-09 11:47:26
【问题描述】:
我正在尝试从远程 HDFS 系统读取文件并显示在本地计算机的控制台中。请注意,本地机器只能通过 .pem 文件形式的 SSH 密钥与任何 HDFS 节点建立连接。
当我执行下面显示的一段代码时,程序运行,保持空闲一段时间,最后显示:
BlockMissingException : Could not obtain block
我的代码:
try {
UserGroupInformation ugi = UserGroupInformation.createRemoteUser("remoteUser");
ugi.doAs(new PrivilegedExceptionAction<Void>() {
public Void run() throws Exception {
conf = new Configuration();
conf.set("fs.hdfs.impl", org.apache.hadoop.hdfs.DistributedFileSystem.class.getName());
conf.set("fs.file.impl", org.apache.hadoop.fs.LocalFileSystem.class.getName());
conf.addResource(new Path("/etc/hadoop/conf/core-site.xml"));
conf.addResource(new Path("/etc/hadoop/conf/hdfs-site.xml"));
conf.addResource(new Path("/etc/hadoop/conf/mapred-site.xml"));
conf.set("fs.default.name", hdfsurl);
conf.set("fs.defaultFS", hdfsurl);
conf.set("hadoop.job.ugi", "remoteUser");
conf.set("hadoop.ssl.enabled", "false");
readFromHDFS(hdfsurl);
return null;
}
});
} catch (Exception e) {
public static void readFromHDFS(String hdfsURL) throws Exception {
FileSystem fileSystem = FileSystem.get(conf);
Path path = new Path(hdfsURL);
if (!fileSystem.exists(path)) {
System.out.println("File does not exists");
return;
}
FSDataInputStream in = fileSystem.open(path);
Scanner sc = new Scanner(in);
while (sc.hasNextLine()) {
System.out.println("line read from hdfs...." + sc.nextLine());
}
in.close();
fileSystem.close();
}
【问题讨论】:
标签: java hadoop hdfs remote-access readfile