从HDFS中下载指定文件,如果本地文件与要下载的文件名称相同,则自动对下载的文件重命名;
package org.zkpk.hadoop.FileDownload;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URI;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IOUtils;
public class Main {
public static void main (String[]args){
Path in = null;
FileSystem fs = null;
Path out = null;
try{
in = new Path(args[0]);
Configuration conf = new Configuration();
fs = FileSystem.get(URI.create(args[1]), conf);
out = new Path(args[1]);
if(fs.exists(out)){
Integer i = Integer.valueOf(0);
while(true){
out = new Path(args[1] + "_" + i.toString());
if(!fs.exists(out)){
fs.copyToLocalFile(false, in, out,true);
break;
}else{
i++;
continue;
}
}
}else{
fs.copyToLocalFile(false, in, out,true);
}
}catch(FileNotFoundException e){
e.printStackTrace();
}catch(IOException e){
e.printStackTrace();
}
finally{
if(fs != null){
try{
fs.close();
}catch(IOException e ){
e.printStackTrace();
}
}
}
}
}
然后export 打包成jar
在terminal 中调用
hadoop jar /home/zkpk/FileDownload.jar org.zkpk.hadoop.FileDownload.Main hdfs://master:9000/file2.txt /home/zkpk/test.txt
可以用vim 查看文件 vim /home/zkpk/test.txt