亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

Java API操作HDFS方法詳細(xì)講解

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

一、判斷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é)點(diǎn)主機(jī)名屬性
        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é)點(diǎn)主機(jī)名屬性
        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界面查看

再運(yùn)行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é)點(diǎn)主機(jī)名屬性
        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é)點(diǎn)主機(jī)名屬性
        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 + "]不存在!");
        }
    }

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

3、刪除目錄或文件

  • 進(jìn)行三個層面的判斷:判斷類型(目錄或文件)、判斷是否存在、判斷刪除是否成功
  • 刪除/ied03/exam.txt文件和/ied02目錄
  • 編寫delete()方法
@Test
    public void delete() throws Exception {
        // 創(chuàng)建配置對象
        Configuration conf = new Configuration();
        // 設(shè)置數(shù)據(jù)節(jié)點(diǎn)主機(jī)名屬性
        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");
        // 定義隨機(jī)對象
        Random random = new Random();
        // 產(chǎn)生隨機(jī)整數(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方法詳細(xì)講解的文章就介紹到這了,更多相關(guān)Java API操作HDFS內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評論