只有當知識寫進你的長時記憶區,才是真正的學習
HDFS客戶端操作 --- 檔案操作
引數優先級測驗
1.撰寫測驗方法,設定檔案副本數量
@Test
public void testCopyFromLocalFile() throws IOException, InterruptedException, URISyntaxException {
// 1 獲取檔案系統
Configuration configuration = new Configuration();
// 組態檔副本數為2
configuration.set("dfs.replication", "2");
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zhutiansama");
// 2 上傳檔案
fs.copyFromLocalFile(new Path("e:/data.txt"), new Path("/data.txt"));
// 3 關閉資源
fs.close();
System.out.println("over");
}
2.將hdfs-site.xml拷貝到resources下,設定副本數為1
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet type="text/xsl" href="https://www.cnblogs.com/focusbigdata/p/configuration.xsl"?>
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
3.引數的優先級
引數優先級排序:(1)客戶端代碼中設定的值 >(2)ClassPath下的用戶自定義組態檔 >(3)然后是服務器的默認配置
HDFS檔案下載
@Test
public void testCopyToLocalFile() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取檔案系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zhutiansama");
// 2 執行下載操作
// boolean delSrc 指是否將原檔案洗掉
// Path src 指要下載的檔案路徑
// Path dst 指將檔案下載到的路徑
// boolean useRawLocalFileSystem 是否開啟檔案校驗
fs.copyToLocalFile(false, new Path("/data.txt"), new Path("e:/data.txt"), true);
// 3 關閉資源
fs.close();
}
HDFS檔案夾洗掉
@Test
public void testDelete() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取檔案系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zhutiansama");
// 2 執行洗掉
fs.delete(new Path("/input01/"), true);
// 3 關閉資源
fs.close();
}
HDFS檔案名更名
@Test
public void testRename() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取檔案系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zhutiansama");
// 2 修改檔案名稱
fs.rename(new Path("/data.txt"), new Path("/datarename.txt"));
// 3 關閉資源
fs.close();
}
HDFS檔案詳情查看
@Test
public void testListFiles() throws IOException, InterruptedException, URISyntaxException{
// 1獲取檔案系統
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zhutiansama");
// 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);
}
}
}
// 3 關閉資源
fs.close();
}
HDFS判斷檔案和檔案夾
@Test
public void testListStatus() throws IOException, InterruptedException, URISyntaxException{
// 1 獲取檔案配置資訊
Configuration configuration = new Configuration();
FileSystem fs = FileSystem.get(new URI("hdfs://hadoop102:9000"), configuration, "zhutiansama");
// 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();
}
上面學的API操都是框架封裝好的,如果我們想自己實作上述API的操作應該用IO流方式
相關資料

本文配套GitHub:https://github.com/zhutiansama/FocusBigData
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/150230.html
標籤:Java
上一篇:會話技術
