在之前的博客《HDFS系列(5) |進行API操作前的準備》
中,博主爲大家分享的是在進行API操作前的準備工作。而本篇博客,博主爲大家展現HDFS的API操作。
1. HDFS文件上傳
- 1. 源碼:
package com.buwenbuhuo.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.junit.Test;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
/**
* @author buwenbuhuo
* @create 2020-04-22 16:45
* com.buwenbuhuo.hdfs - the name of the target package where the new class or interface will be created.
* hdfs0422 - the name of the current project.
*/
public class HDFSClient {
@Test
public void testMkdirs() throws IOException, InterruptedException, URISyntaxException {
// 1 獲取文件系統
Configuration configuration = new Configuration();
// 配置在集羣上運行
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata");
// 2 上傳文件
fs.copyFromLocalFile(new Path("d:/buwenbuhuo.txt"), new Path("/buwenbuhuo.txt"));
// 3 關閉資源
fs.close();
System.out.println("over");
}
}
- 2. 運行結果
2. HDFS文件下載
- 1. 源碼
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata");
// 2 執行下載操作
// boolean delSrc 指是否將原文件刪除
// Path src 指要下載的文件路徑
// Path dst 指將文件下載到的路徑
// boolean useRawLocalFileSystem 是否開啓文件校驗
fs.copyToLocalFile(false, new Path("/buwenbuhuo.txt"), new Path("d:/buwenbuhuo1.txt"), true);
// 3 關閉資源
fs.close();
}
- 2. 結果:
3. HDFS文件夾刪除
- 1. 源碼:
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata");
// 2 執行刪除
fs.delete(new Path("/0422/"), true);
// 3 關閉資源
fs.close();
}
- 2.結果
4. HDFS文件名更改
- 1.源碼:
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata");
// 2 修改文件名稱
fs.rename(new Path("/buwenbuhuo.txt"), new Path("/VN1.txt"));
// 3 關閉資源
fs.close();
}
- 2. 結果
5. HDFS文件詳情查看
- 1. 源碼:
# 查看文件名稱、權限、長度、塊信息
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
// 1獲取文件系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata");
// 2 獲取文件詳情
RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path("/"), true);
while(listFiles.hasNext()){
LocatedFileStatus status = listFiles.next();
// 輸出詳情
// 文件名稱
System.out.println(status.getPath().getName());
// 長度
System.out.println(status.getLen());
// 權限
System.out.println(status.getPermission());
// 分組
System.out.println(status.getGroup());
// 獲取存儲的塊信息
BlockLocation[] blockLocations = status.getBlockLocations();
for (BlockLocation blockLocation : blockLocations) {
// 獲取塊存儲的主機節點
String[] hosts = blockLocation.getHosts();
for (String host : hosts) {
System.out.println(host);
}
}
System.out.println("-----------華麗的分割線----------");
}
// 3 關閉資源
fs.close();
}
- 2. 結果
6. HDFS文件和文件夾判斷
- 1. 源碼:
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取文件配置信息
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(URI.create("hdfs://hadoop001:9000"), configuration, "bigdata");
// 2 判斷是文件還是文件夾
FileStatus[] listStatus = fs.listStatus(new Path("/"));
for (FileStatus fileStatus : listStatus) {
// 如果是文件
if (fileStatus.isFile()) {
System.out.println("f:"+fileStatus.getPath().getName());
}else {
System.out.println("d:"+fileStatus.getPath().getName());
}
}
// 3 關閉資源
fs.close();
}
- 2. 結果
爲了方便大家理解,在代碼中博主都寫有註釋,因此在這裏就不作過多的過程說明了。那麼本次的分享就到這裏了,小夥伴們有什麼疑惑或好的建議可以積極在評論區留言,博主後續還會推出HDFS系列的其他內容,希望大家持續關注博主!