Configuration configuration = new Configuration();
configuration.set("fs.defaultFS","hdfs://ip地址:9000");
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.create(new Path("/shangdongqinggong"));
fileSystem.mkdirs(new Path("/shangdongqinggong02"));
fileSystem.close();
我們可以修改hadoop的組態檔 hdfs-site.xml
[外連圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-0vhIYRoy-1601095079060)(C:\Users\陳同學\AppData\Roaming\Typora\typora-user-images\image-20200926122028181.png)]
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://ip地址:9000");
FileSystem fileSystem = FileSystem.get(configuration);
InputStream inputStream=new FileInputStream(new File("本地檔案的路徑"));
OutputStream outputStream =fileSystem.create(new Path("/上傳路徑"));
IOUtils.copyBytes(inputStream,outputStream,configuration);
outputStream.close();
inputStream.close();
fileSystem.close();
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://ip地址:9000");
FileSystem fileSystem = FileSystem.get(configuration);
InputStream inputStream=fileSystem.open(new Path("檔案路徑名"));
OutputStream outputStream=new FileOutputStream(new File("E:\\apps\\"));
IOUtils.copyBytes(inputStream,outputStream,configuration);
outputStream.close();
inputStream.close();
fileSystem.close();
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://ip地址:9000");
FileSystem fileSystem = FileSystem.get(configuration);
fileSystem.delete(new Path("/檔名"),true);
fileSystem.delete(new Path("/檔名"), false);
fileSystem.close();
Configuration configuration = new Configuration();
configuration.set("fs.defaultFS", "hdfs://ip地址:9000");
FileSystem fileSystem = FileSystem.get(configuration);
RemoteIterator<LocatedFileStatus> iterator = fileSystem.listFiles(new Path("/"), true);
while (iterator.hasNext()){
LocatedFileStatus fileStatus=iterator.next();
System.out.println(fileStatus.getPath());
System.out.println(fileStatus.getLen());
System.out.println(fileStatus.getReplication());
System.out.println(fileStatus.getOwner());
}
fileSystem.close();
intln(fileStatus.getLen());
System.out.println(fileStatus.getReplication());
System.out.println(fileStatus.getOwner());
}
## 4.關閉資源
```java
fileSystem.close();
[外連圖片轉存失敗,源站可能有防盜鏈機制,建議將圖片儲存下來直接上傳(img-vj5oWsZ9-1601095079062)(C:\Users\陳同學\AppData\Roaming\Typora\typora-user-images\image-20200926122217224.png)]