欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java API操作HDFS方法詳細講解

 更新時間:2023年02月20日 08:29:14   作者:X_Serendipity  
這篇文章主要介紹了Java API操作Hdfs詳細示例,遍歷當(dāng)前目錄下所有文件與文件夾,可以使用listStatus方法實現(xiàn)上述需求,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

一、判斷Path指向目錄還是文件

net.xxr.hdfs包里創(chuàng)建PathToFileOrDir

package net.xxr.hdfs;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import java.net.URI;
/**
 * 功能:判斷路徑指向目錄還是文件
 */
public class PathToFileOrDir {
    public static void main(String[] args) throws Exception {
        // 創(chuàng)建配置對象
        Configuration conf = new Configuration();
        // 設(shè)置數(shù)據(jù)節(jié)點主機名屬性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定義uri字符串
        String uri = "hdfs://master:9000";
        // 創(chuàng)建文件系統(tǒng)對象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 創(chuàng)建路徑對象,指向目錄
        Path path1 = new Path("/ied01");
        if (fs.isDirectory(path1)) {
            System.out.println("[" + path1 + "]指向的是目錄!");
        } else {
            System.out.println("[" + path1 + "]指向的是文件!");
        }
        // 創(chuàng)建路徑對象,指向文件
        Path path2 = new Path("/lzy01/test2.txt");
        if (fs.isFile(path2)) {
            System.out.println("[" + path2 + "]指向的是文件!");
        } else {
            System.out.println("[" + path2 + "]指向的是目錄!");
        }
    }
}

結(jié)果

二、刪除目錄或文件

net.xxr.hdfs包里創(chuàng)建DeleteFileOrDir

1、刪除文件

  • 刪除/lzy/hello.txt文件
  • 編寫deleteFile()方法
package net.xxr.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.net.URI;
/**
 * 功能:刪除目錄或文件
 */
public class DeleteFileOrDir {
    @Test
    public void deleteFile() throws Exception {
        // 創(chuàng)建配置對象
        Configuration conf = new Configuration();
        // 設(shè)置數(shù)據(jù)節(jié)點主機名屬性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定義uri字符串
        String uri = "hdfs://master:9000";
        // 創(chuàng)建文件系統(tǒng)對象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 創(chuàng)建路徑對象(指向文件)
        Path path = new Path(uri + "/lzy01/hello.txt");
        // 刪除路徑對象指向的文件(第二個參數(shù)表明是否遞歸,刪除文件,不用遞歸)
        boolean result = fs.delete(path, false);
        // 根據(jù)返回結(jié)果提示用戶
        if (result) {
            System.out.println("文件[" + path + "]刪除成功!");
        } else {
            System.out.println("文件[" + path + "]刪除失敗!");
        }
    }
}

結(jié)果

利用Hadoop WebUI界面查看

再運行deleteFile()測試方法,查看結(jié)果

可以在刪除文件之前,判斷文件是否存在,需要修改代碼

package net.xxr.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.net.URI;
/**
 * 功能:刪除目錄或文件
 */
public class DeleteFileOrDir {
    @Test
    public void deleteFile() throws Exception {
        // 創(chuàng)建配置對象
        Configuration conf = new Configuration();
        // 設(shè)置數(shù)據(jù)節(jié)點主機名屬性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定義uri字符串
        String uri = "hdfs://master:9000";
        // 創(chuàng)建文件系統(tǒng)對象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 創(chuàng)建路徑對象(指向文件)
        Path path = new Path(uri + "/lzy01/hi.txt");
        // 判斷路徑對象指向的文件是否存在
        if (fs.exists(path)) {
            // 刪除路徑對象指向的文件(第二個參數(shù)表明是否遞歸,刪除文件,不用遞歸)
            boolean result = fs.delete(path, false);
            // 根據(jù)返回結(jié)果提示用戶
            if (result) {
                System.out.println("文件[" + path + "]刪除成功!");
            } else {
                System.out.println("文件[" + path + "]刪除失敗!");
            }
        } else {
            System.out.println("文件[" + path + "]不存在!");
        }
    }
}

結(jié)果

2、刪除目錄

  • 刪除/ied01目錄
  • 編寫deleteDir()方法
@Test
    public void deleteDir() throws Exception {
        // 創(chuàng)建配置對象
        Configuration conf = new Configuration();
        // 設(shè)置數(shù)據(jù)節(jié)點主機名屬性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定義uri字符串
        String uri = "hdfs://master:9000";
        // 創(chuàng)建文件系統(tǒng)對象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 創(chuàng)建路徑對象(指向目錄)
        Path path = new Path(uri + "/ied01");
        // 判斷路徑對象指向的目錄否存在
        if (fs.exists(path)) {
            // 刪除路徑對象指向的目錄(第二個參數(shù)表明是否遞歸,刪除文件,要遞歸)
            boolean result = fs.delete(path, true);
            // 根據(jù)返回結(jié)果提示用戶
            if (result) {
                System.out.println("目錄[" + path + "]刪除成功!");
            } else {
                System.out.println("目錄[" + path + "]刪除失??!");
            }
        } else {
            System.out.println("目錄[" + path + "]不存在!");
        }
    }

再運行deleteDir()方法,查看結(jié)果

3、刪除目錄或文件

  • 進行三個層面的判斷:判斷類型(目錄或文件)、判斷是否存在、判斷刪除是否成功
  • 刪除/ied03/exam.txt文件和/ied02目錄
  • 編寫delete()方法
@Test
    public void delete() throws Exception {
        // 創(chuàng)建配置對象
        Configuration conf = new Configuration();
        // 設(shè)置數(shù)據(jù)節(jié)點主機名屬性
        conf.set("dfs.client.use.datanode.hostname", "true");
        // 定義uri字符串
        String uri = "hdfs://master:9000";
        // 創(chuàng)建文件系統(tǒng)對象
        FileSystem fs = FileSystem.get(new URI(uri), conf, "root");
        // 定義隨機對象
        Random random = new Random();
        // 產(chǎn)生隨機整數(shù) - [0, 1]
        int choice = random.nextInt(100) % 2;
        // 定義路徑字符串
        String[] strPath = {"/ied03/exam.txt", "/ied02"};
        // 創(chuàng)建路徑對象(指向目錄或文件)
        Path path = new Path(uri + strPath[choice]);
        // 判斷類型:目錄或文件
        String type = "";
        if (fs.isDirectory(path)) {
            type = "目錄";
        } else {
            type = "文件";
        }
        // 判斷存在性
        if (fs.exists(path)) {
            // 刪除路徑對象指向的目錄或文件
            boolean result = fs.delete(path, true);
            // 判斷刪除是否成功
            if (result) {
                System.out.println(type + "[" + path + "]刪除成功!");
            } else {
                System.out.println(type + "[" + path + "]刪除失敗!");
            }
        } else {
            System.out.println(type + "[" + path + "]不存在!");
        }
    }

到此這篇關(guān)于Java API操作HDFS方法詳細講解的文章就介紹到這了,更多相關(guān)Java API操作HDFS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • Springboot整合PageOffice 實現(xiàn)word在線編輯保存功能

    Springboot整合PageOffice 實現(xiàn)word在線編輯保存功能

    這篇文章主要介紹了Springboot整合PageOffice 實現(xiàn)word在線編輯保存,本文以Samples5 為示例文件結(jié)合示例代碼給大家詳細介紹,需要的朋友可以參考下
    2021-08-08
  • SpringBoot微服務(wù)注冊分布式Consul的詳細過程

    SpringBoot微服務(wù)注冊分布式Consul的詳細過程

    這篇文章主要介紹了SpringBoot(微服務(wù))注冊分布式Consul,Spring Boot應(yīng)用可以通過向Consul注冊自身來實現(xiàn)服務(wù)發(fā)現(xiàn)和治理,使得其他服務(wù)可以在Consul中發(fā)現(xiàn)并調(diào)用它,需要的朋友可以參考下
    2023-04-04
  • java使用minio上傳下載文件完整版教程

    java使用minio上傳下載文件完整版教程

    本示例教程介紹了如何使用SpringBoot框架結(jié)合MinIO服務(wù)實現(xiàn)文件的上傳和下載功能,并將文件信息存儲在數(shù)據(jù)庫的file表中,文中通過代碼介紹的非常詳細,需要的朋友可以參考下
    2024-09-09
  • SpringBoot打印POST請求原始入?yún)ody體方式

    SpringBoot打印POST請求原始入?yún)ody體方式

    這篇文章主要介紹了SpringBoot打印POST請求原始入?yún)ody體方式,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2021-09-09
  • java項目實現(xiàn)猜拳小游戲

    java項目實現(xiàn)猜拳小游戲

    這篇文章主要為大家詳細介紹了java項目實現(xiàn)猜拳小游戲,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2020-05-05
  • MyBatis接口的簡單實現(xiàn)原理分析

    MyBatis接口的簡單實現(xiàn)原理分析

    這里僅僅舉個簡單例子來說明原理,不是完全針對MyBatis的,這種思想我們也可以應(yīng)用在其他地方。地mybatis接口實現(xiàn)原理感興趣的朋友一起看看吧
    2017-07-07
  • 淺談java對象轉(zhuǎn)json,數(shù)字精確出現(xiàn)丟失問題

    淺談java對象轉(zhuǎn)json,數(shù)字精確出現(xiàn)丟失問題

    下面小編就為大家?guī)硪黄獪\談java對象轉(zhuǎn)json, 數(shù)字精確出現(xiàn)丟失問題。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2017-03-03
  • Java生成指定范圍內(nèi)的一個隨機整數(shù)2種方式

    Java生成指定范圍內(nèi)的一個隨機整數(shù)2種方式

    本文主要介紹了Java生成指定范圍內(nèi)的一個隨機整數(shù)2種方式,主要使用Math.random()和Random.nextInt()這兩種,具有一定的參考價值,感興趣的可以了解一下
    2023-04-04
  • Java的MyBatis+Spring框架中使用數(shù)據(jù)訪問對象DAO模式的方法

    Java的MyBatis+Spring框架中使用數(shù)據(jù)訪問對象DAO模式的方法

    Data Access Object數(shù)據(jù)訪問對象模式在Java操作數(shù)據(jù)庫部分的程序設(shè)計中經(jīng)常被使用到,這里我們就來看一下Java的MyBatis+Spring框架中使用數(shù)據(jù)訪問對象DAO模式的方法:
    2016-06-06
  • jdbc+jsp實現(xiàn)簡單員工管理系統(tǒng)

    jdbc+jsp實現(xiàn)簡單員工管理系統(tǒng)

    這篇文章主要為大家詳細介紹了jdbc+jsp實現(xiàn)簡單員工管理系統(tǒng),文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-02-02

最新評論