Hadoop2.7.3客户端之Java API上传/下载文件
- 在Idea或eclipse中创建maven项目
- 在pom.xml中添加hadoop依赖(请参考前面的博文)
- 编写测试代码
- 上传代码
public static void testUpload() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://hadoop001:9000");
FileSystem fileSystem = FileSystem.get(configuration);
FileInputStream inputStream = new FileInputStream(new File("D:\\downloadFiles\\part-r-00000"));
FSDataOutputStream outputStream = fileSystem.create(new Path("/output/part-r-00000"));
IOUtils.copyBytes(inputStream,outputStream,configuration);
fileSystem.close();
System.out.println("上传成功~");
}
- 下载代码
public static void downloadFromHDFS() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://hadoop001:9000");
FileSystem fileSystem = FileSystem.get(configuration);
FileOutputStream outputStream = new FileOutputStream(new File("D:\\downloadFiles\\part-r-00000"));
FSDataInputStream inputStream = fileSystem.open(new Path("/output/part-r-00000"));
IOUtils.copyBytes(inputStream,outputStream,configuration);
fileSystem.close();
System.out.println("下载成功~");
}