从hdfs中下载文件,下载的文件默认存储在E:盘下,如需修改下载路径,只需修改downloadPath,具体代码:
public static void readFromHdfs(String filename,String downloadPath) throws FileNotFoundException,IOException {
String dst = "hdfs://192.168.248.129:9000/"+filename;
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(URI.create(dst), conf);
FSDataInputStream hdfsInStream = fs.open(new Path(dst));
OutputStream out = new FileOutputStream(downloadPath);
byte[] ioBuffer = new byte[1024];
int readLen = hdfsInStream.read(ioBuffer);
while(-1 != readLen){
out.write(ioBuffer, 0, readLen);
readLen = hdfsInStream.read(ioBuffer);
}
out.close();
hdfsInStream.close();
fs.close();
}