import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import org.apache.commons.compress.utils.IOUtils;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FSDataInputStream;
import org.apache.hadoop.fs.FSDataOutputStream;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
public class DownlandFile {
public static void main(String [] args) throws IOException, URISyntaxException {
downLoad();//方法一文件下载
downLoad2();//方法二文件下载
uploadfile();//文件上传
}
public static void downLoad() throws IOException, URISyntaxException {
FileSystem file = FileSystem.get(new URI("hdfs://Master:9000"),new Configuration());
file.copyToLocalFile(new Path("/aaa/bbb/ccc/ddd.txt"), new Path("/home/hadoop/Desktop/ddd.txt"));
file.close();
}
public static void downLoad2() throws IOException, URISyntaxException {
FileSystem file = FileSystem.get(new URI("hdfs://Master:9000"),new Configuration());
FSDataInputStream fsinput = file.open(new Path("/aaa/bbb/ccc/ddd.txt"));
FileOutputStream outputstream = new FileOutputStream(("/home/hadoop/Desktop/aaa.txt"));
IOUtils.copy(fsinput, outputstream);
IOUtils.closeQuietly(fsinput);
IOUtils.closeQuietly(outputstream);
file.close();
}
public static void uploadfile() throws IOException, URISyntaxException {
FileSystem file = FileSystem.get(new URI("hdfs://Master:9000"),new Configuration());
file.copyFromLocalFile(new Path("/home/hadoop/Desktop/aaa.txt"), new Path("/user/hadoop"));
file.close();
}
}
hdfsファイルのアップロードとファイルのダウンロード
おすすめ
転載: blog.csdn.net/NewDay_/article/details/108854990
ランキング