溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

HDFS中客戶端操作有哪些

發(fā)布時間:2021-12-09 14:28:59 來源:億速云 閱讀:180 作者:小新 欄目:大數(shù)據(jù)

這篇文章主要為大家展示了“HDFS中客戶端操作有哪些”,內(nèi)容簡而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領大家一起研究并學習一下“HDFS中客戶端操作有哪些”這篇文章吧。

1、客戶端Hadoop環(huán)境準備

1. 將支持window系統(tǒng)的hadoop安裝包,解壓到本地,并配置環(huán)境變量( 無中文或特殊符號的路徑)
--比如:我的Hadoop安裝包位置是:D:\hadoop\hadoop-3.1.0
配置環(huán)境變量:
	HADOOP_HOME   	D:\hadoop\hadoop-3.1.0
	path		%HADOOP_HOME%\bin

2. 如果上述操作后還有問題可以將bin目錄下hadoop.dll和winutils.exe放到C:/windows/system32目錄下,然后重啟電腦。

2、HDFS的API操作

2.1參數(shù)初始化配置

操作HDFS:

1.創(chuàng)建文件系統(tǒng)對象
2.具體操作 :上傳,刪除,下載.....
3.關資源
private FileSystem fs;
    //1.創(chuàng)建fs文件系統(tǒng)對象
    @Before
    public void before() throws Exception {
         /*
        get(final URI uri, final Configuration conf,final String user)
        uri : HDFS的地址
        conf : 需要使用的配置信息
        user : 用來操作HDFS的用戶
         */
        // uri : HDFS的地址
        URI uri = new URI("hdfs://hadoop102:9820");
        //conf : 需要使用的配置信息
        Configuration conf = new Configuration();
        //user : 用來操作HDFS的用戶
        String user = "luck";
        fs = FileSystem.get(uri, conf, user);
    }
//3.關資源
@After
    public void after(){
        try {
            if (fs != null) {
                fs.close();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

2.2文件上傳、下載、刪除、文件詳情查看、判斷是文件還是目錄

    //2.具體的操作
    //上傳
    @Test
    public void test() throws IOException {
        /*
        copyFromLocalFile(boolean delSrc, boolean overwrite, Path src, Path dst)
          delSrc : 是否刪除源文件(是剪切還是復制)
          overwrite : 如果目標文件已存在是否覆蓋目標文件
                --注意:如果不覆蓋但目標文件又存在則會報錯
          src : 源文件路徑(本地)
          dst : 目標文件路徑(HDFS)
         */
        fs.copyFromLocalFile(true, true,new Path("D:\\io\\hdfs\\aa.txt"),new Path("/hdfs"));
    }
    //下載
    @Test
    public void test2() throws IOException {
        /*
            copyToLocalFile(boolean delSrc, Path src, Path dst,
      boolean useRawLocalFileSystem)
        delSrc : 是否刪除源文件(HDFS上的文件)
        src : 源文件路徑(HDFS)
        dst : 目標文件路徑(本地)
        useRawLocalFileSystem : 是否使用useRawLocalFileSystem
                如果使用:不會下載crc校驗文件
                如果不使用 : 會下載crc校驗文件
         */
        fs.copyToLocalFile(false,new Path("/hdfs/aa.txt"),new Path("D:\\io\\hdfs"),
                false);

    }

    //刪除
    @Test
    public void test3() throws IOException {
        /*
            delete(Path f, boolean recursive)
            f : 刪除的數(shù)據(jù)的路徑
            recursive : 是否遞歸(如果是目錄(非空)必須是true否則會報錯。如果是文件true和false都可以)
         */
        fs.delete(new Path("/longge/sanguo.txt"),true);
    }

    //改名
    @Test
    public void test4() throws IOException {
        /*
            rename(Path src, Path dst)
            src : 源文件路徑
            dst : 目標文件路徑
         */
        //fs.rename(new Path("/longge/xiyou.txt"),new Path("/longge/xiyouji.txt"));
        //移動
        fs.rename(new Path("/longge/xiyouji.txt"),new Path("/hdfs/xiyouji.txt"));
    }

    //文件詳情查看
    @Test
    public void test5() throws IOException {
        /*
            listFiles(
                    final Path f, final boolean recursive)
            f : 目標路徑
            recursive : 是否遞歸
         */
        RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(new Path("/"), true);

        while(remoteIterator.hasNext()){
            //文件詳情對象
            LocatedFileStatus fileStatus = remoteIterator.next();
            //文件名
            System.out.println("=============" + fileStatus.getPath().getName() + "===================");
            System.out.println("=====所屬主:" + fileStatus.getOwner());
            System.out.println("=====副本數(shù)量:" + fileStatus.getReplication());
            //獲取塊的信息
            BlockLocation[] blockLocations = fileStatus.getBlockLocations();
            for (BlockLocation block : blockLocations) {
                System.out.println(block);
            }
        }

2.3用流的方式實現(xiàn)HDFS上傳和下載內(nèi)容

//用流的方式實現(xiàn)HDFS上傳和下載內(nèi)容
    //上傳
    @Test
    public void test7() throws IOException {
        //讀-從本地讀(文件輸入流)
        FileInputStream fis = new FileInputStream("D:\\io\\hdfs\\aa.txt");
        //寫-向HDFS寫
        FSDataOutputStream fos = fs.create(new Path("/hdfs/aa.txt"));
        //一邊讀一邊寫
        /*
            文件對拷
            copyBytes(InputStream in, OutputStream out,int buffSize, boolean close)
             in : 輸入流
             out : 輸出流
             buffsize :緩存大小
             close : 是否關流
         */
        IOUtils.copyBytes(fis,fos,1024,false);
        //關流
        IOUtils.closeStream(fis);
        IOUtils.closeStream(fos);
        /**
         *
         */
    }

    //下載
    @Test
    public void test8() throws IOException {
        //讀 - 從HDFS上讀
        FSDataInputStream fis = fs.open(new Path("/hdfs/xiyouji.txt"));
        //寫 - 向本地寫(文件輸出流)
        FileOutputStream fos = new FileOutputStream("D:\\io\\hdfs\\xiyouji.txt");
        //文件對拷
        IOUtils.copyBytes(fis,fos,1024,true);
    }

以上是“HDFS中客戶端操作有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對大家有所幫助,如果還想學習更多知識,歡迎關注億速云行業(yè)資訊頻道!

向AI問一下細節(jié)

免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI