0
원격 HDFS에서 파일을 읽으려고합니다. 파일의 내용을 볼 수 없습니다. 친절하게 도와주세요. 여기에 내 코드를 첨부했습니다. 이 코드를 실행하는 동안 출력이 나오지 않습니다. 프로그램은 어떤 결과도주지 않고 활동적입니다.원격 HDFS에서 파일 읽기
package com.cts.peg.iot.accessRemoteHDFS02;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.BlockLocation;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.hdfs.DistributedFileSystem;
import org.apache.hadoop.hdfs.protocol.DatanodeInfo;
public class ReadFromHDFS {
public static void main(String[] args) throws Exception {
Configuration 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());
String dest = args[0];
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"));
FileSystem fileSystem = FileSystem.get(conf);
Path dstPath = new Path(dest);
FSDataInputStream in = fileSystem.open(dstPath);
// Check if the file already exists
if (!(fileSystem.exists(dstPath))) {
System.out.println("No such destination " + dstPath);
return;
}
// Get the filename out of the file path
try{
String filename = dest.substring(dest.lastIndexOf('/') + 1, dest.length());
OutputStream out = new BufferedOutputStream(new FileOutputStream(
new File(filename)));
byte[] b = new byte[1024];
int numBytes = 0;
while ((numBytes = in.read(b)) > 0) {
out.write(b, 0, numBytes);
}
}catch(Exception e){
System.err.println("Exception caught! :" + e);
System.exit(1);
}finally{
in.close();
fileSystem.close();
}
}
}