上传文件
方式一:常规流程
@Test
public void uploadFile2Hdfs() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node001:8020");
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.copyFromLocalFile(new Path("pom.xml"),new Path("/sjj/test"));
fileSystem.close();
}

方式二:I/O上传文件
@Test
public void putFile2Hdfs() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node001:8020"), configuration, "sjj");
FileInputStream fis = new FileInputStream(new File("pom.xml"));
FSDataOutputStream fos = fileSystem.create(new Path("/sjj/test/pom.xml"));
IOUtils.copy(fis,fos);
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(fis);
fileSystem.close();
}
下载文件
方式一:常规流程
@Test
public void downloadFileFromHdfs() throws IOException {
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://node001:8020");
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.copyToLocalFile(new Path("/sjj/test/pom.xml"),new Path("/Users/soutsukyou/Desktop/ForHadoop/"));
fileSystem.close();
}

方式二:I/O下载文件
@Test
public void getFileFromHdfs() throws URISyntaxException, IOException, InterruptedException {
Configuration configuration = new Configuration();
FileSystem fileSystem = FileSystem.get(new URI("hdfs://node001:8020"), configuration, "sjj");
FSDataInputStream fis = fileSystem.open(new Path("/sjj/test/pom.xml"));
FileOutputStream fos = new FileOutputStream("/Users/soutsukyou/Desktop/ForHadoop/");
IOUtils.copy(fis,fos);
IOUtils.closeQuietly(fos);
IOUtils.closeQuietly(fis);
fileSystem.close();
}
其它操作
fileSystem.delete();
fileSystem.rename();