pom文件:
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<version>2.6.0</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-hdfs -->
<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-hdfs</artifactId>
<version>2.6.0</version>
</dependency>
</dependencies>
Java API 创建文件夹:
public void testMkdir() throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.142.150:9000"),conf,"root");
fs.mkdirs(new Path("/user/java"));
fs.close();
}
Java API 写文件:
public void testCopyFromLocalFile() throws Exception {
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.142.150:9000"),conf,"root");
fs.copyFromLocalFile(new Path("d://abc1.txt"),new Path("/user/java/mobile.txt"));
fs.close();
}
Java API 读取文件:
public void testCopyToLocalFile() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.142.150:9000"),conf,"root");
fs.copyToLocalFile(new Path("/user/java/mobile.txt"),new Path("d://abc1.txt"));
fs.close();
}
Java API 删除文件:
public void testDel() throws Exception{
Configuration conf = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://192.168.142.150:9000"),conf,"root");
fs.delete(new Path("/user"),true);
fs.close();
}