HDFS中API的使用

在項目開發(fā)中风响,有時我們需要通過HDFS的api來對文件進行操作袱衷,比如將數(shù)據(jù)上傳到HDFS或者從HDFS獲取數(shù)據(jù)等敬察。本篇來介紹一下HDFS中API的具體使用薪捍。直接上代碼:

package com.lzb.hdfs.fs;


import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.*;

import java.io.IOException;

public class HDFSHelper {

    private FileSystem fs;

    public HDFSHelper() {
        fs = getFileSystem();
    }

    /**
     * Configuration是配置對象,conf可以理解為包含了所有配置信息的一個集合抽兆,可以認為是Map,
     * 在初始化的時候底層會加載一堆配置文件 core-site.xml;hdfs-site.xml;mapred-site.xml;yarn-site.xml
     * 如果需要項目代碼自動加載配置文件中的信息识补,那么就必須把配置文件改成-default.xml或者-site.xml的名稱,
     * 而且必須放置在src下,如果不叫這個名郊丛,或者不在src下李请,也需要加載這些配置文件中的參數(shù)瞧筛,必須使用conf對象提供的方法手動加載.
     * 依次加載的參數(shù)信息的順序是:
     * 1.加載core/hdfs/mapred/yarn-default.xml
     * 2.加載通過conf.addResource()加載的配置文件
     * 3.加載conf.set(name,value)
     */
    private Configuration getConfiguration(){
        Configuration conf = new Configuration();
        //conf.addResource("xxx");
        //conf.set("xxx","xxx");
        //Configuration.addDefaultResource("core-site.xml");
        //Configuration.addDefaultResource("hdfs-site.xml");
        //conf.set("fs.default.name","hdfs://probd01:8020");

        //HA模式的配置
        conf.set("fs.defaultFS", "hdfs://probd");
        conf.set("dfs.nameservices", "probd");
        conf.set("dfs.ha.namenodes.probd", "nn1,nn2");
        conf.set("dfs.namenode.rpc-address.probd.nn1", "probd01:8020");
        conf.set("dfs.namenode.rpc-address.probd.nn2", "probd02:8020");
        conf.set("dfs.client.failover.proxy.provider.probd", "org.apache.hadoop.hdfs.server.namenode.ha.ConfiguredFailoverProxyProvider");

        //防止報錯:no FileSystem for scheme: hdfs...
        conf.set("fs.hdfs.impl", "org.apache.hadoop.hdfs.DistributedFileSystem");
        return conf;
    }

    /**
     * 獲取文件系統(tǒng)
     * 本地文件系統(tǒng)為LocalFileSystem,URL形式:    file:///c:myProgram
     * HDFS文件系統(tǒng)為DistributedFileSystem导盅,URL形式:    fs.defaultFS=hdfs://hadoop01:9000
     */
    public FileSystem getFileSystem(){
        Configuration conf = getConfiguration();
        FileSystem fs = null;
        try {
            fs = FileSystem.get(conf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println(fs);
        return fs;
    }

    /**
     * 上傳本地文件到HDFS较幌,底層就是采用流的方式
     * @param localPath 本地文件路徑
     * @param remotePath HDFS文件路徑
     * @return 是否上傳成功
     */
    public boolean copyFromLocal(String localPath,String remotePath){
        if(fs == null) return false;
        try {
            fs.copyFromLocalFile(new Path(localPath),new Path(remotePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return true;
    }

    /**
     * 從HDFS下載文件,底層就是采用流的方式
     * @param remotePath HDFS文件路徑
     * @param localPath 本地路徑
     * @return 是否下載成功
     */
    public boolean copyToLocal(String remotePath,String localPath){
        if(fs == null) return false;
        try {
            fs.copyToLocalFile(new Path(remotePath),new Path(localPath));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }

    /**
     * 獲取目錄下的文件
     * @param remotePath HDFS文件路徑
     * @param recursive 是否級聯(lián)(該文件夾下面如果還有子文件 要不要看,注意沒有 子文件夾!!)
     */
    public void listFiles(String remotePath,boolean recursive){
        if(fs == null) return;
        try {
            RemoteIterator<LocatedFileStatus> iterator = fs.listFiles(new Path(remotePath), recursive);
            while (iterator.hasNext()){
                LocatedFileStatus fileStatus = iterator.next();

                //文件的存儲路徑白翻,以hdfs://開頭的全路徑 ==> hdfs://hadoop01:9000/a/gg.txt
                System.out.println( "file path === " + fileStatus.getPath());

                //文件名
                System.out.println("file name === " + fileStatus.getPath().getName());

                //文件長度
                System.out.println("file size === "+fileStatus.getLen());

                //文件所有者
                System.out.println("file owner === "+fileStatus.getOwner());

                //分組信息
                System.out.println("file group === " + fileStatus.getGroup());

                //文件權(quán)限信息
                System.out.println("file permission === " + fileStatus.getPermission());

                //文件副本數(shù)
                System.out.println("file blocks === " + fileStatus.getReplication());

                //塊大小
                System.out.println("file block size === " + fileStatus.getBlockSize());

                //塊位置相關信息
                BlockLocation[] blockLocations = fileStatus.getBlockLocations();

                //塊的數(shù)量
                System.out.println("file block nums === " + blockLocations.length);

                for (BlockLocation bl : blockLocations) {
                    String[] hosts = bl.getHosts();
                    for (String host: hosts) {
                        System.out.println("block host === " + host);
                    }
                    //塊的一個邏輯路徑
                    bl.getTopologyPaths();
                }

            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 獲取目錄下的文件
     * 此方法與listFiles不同,不支持傳true或false,即不能級聯(lián)乍炉,如果想實現(xiàn)級聯(lián)就采用遞歸的方式
     * @param remotePath HDFS文件路徑
     */
    public void listStatus(String remotePath){
        if(fs == null) return;
        try {
            FileStatus[] listStatus = fs.listStatus(new Path(remotePath));
            for (FileStatus fss : listStatus) {
                //判斷是不是文件夾
                boolean directory = fss.isDirectory();

                //判斷是不是文件
                boolean file = fss.isFile();

                String name = fss.getPath().getName();

                if(file) {
                    System.out.println(name+":文件");
                }else {
                    System.out.println(name+":文件夾");
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    /**
     * 刪除空文件夾或空文件
     * @param path
     */
    public void deleteEmptyDirAndFile(Path path){
        if(fs == null) return;
        try {

            FileStatus[] listStatus = fs.listStatus(path);
            if(listStatus.length == 0){
                //刪除空文件夾
                fs.delete(path,true);
                return;
            }

            RemoteIterator<LocatedFileStatus> iterator = fs.listLocatedStatus(path);

            while (iterator.hasNext()) {
                LocatedFileStatus next = iterator.next();
                Path currentPath = next.getPath();
                Path parentPath = next.getPath().getParent();


                if (next.isDirectory()) {
                    // 如果是空文件夾
                    if (fs.listStatus(currentPath).length == 0) {
                        // 刪除掉
                        fs.delete(currentPath, true);
                    } else {
                        // 不是空文件夾,那么則繼續(xù)遍歷
                        if (fs.exists(currentPath)) {
                            deleteEmptyDirAndFile(currentPath);
                        }
                    }
                } else {
                    // 獲取文件的長度
                    long fileLength = next.getLen();
                    // 當文件是空文件時滤馍, 刪除
                    if (fileLength == 0) {
                        fs.delete(currentPath, true);
                    }
                }

                // 當空文件夾或者空文件刪除時岛琼,有可能導致父文件夾為空文件夾,
                // 所以每次刪除一個空文件或者空文件的時候都需要判斷一下巢株,如果真是如此槐瑞,那么就需要把該文件夾也刪除掉
                int length = fs.listStatus(parentPath).length;
                if (length == 0) {
                    fs.delete(parentPath, true);
                }
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    /**
     * 創(chuàng)建文件夾
     * @param remotePath HDFS文件路徑
     * @return 是否創(chuàng)建成功
     */
    public boolean mkdir(String remotePath){
        if(fs == null) return false;
        boolean success = false;
        try {
            success = fs.mkdirs(new Path(remotePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }

    /**
     * 寫入文件
     * @param remotePath HDFS文件路徑
     * @param content 內(nèi)容
     * @return 是否寫入成功
     */
    public boolean writeToFile(String remotePath,String content){
        if(fs == null) return false;
        try {
            FSDataOutputStream out = fs.create(new Path(remotePath));
            out.writeUTF(content);
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
        return true;

    }

    /**
     * 讀取文件數(shù)據(jù)
     * @param remotePath HDFS文件路徑
     * @return 讀取的結(jié)果數(shù)據(jù)
     */
    public String readFromFile(String remotePath){
        String result = null;
        if(fs == null) return null;
        try {

            FSDataInputStream in = fs.open(new Path(remotePath));
            result = in.readUTF();
            in.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return result;
    }


    /**
     * 重命名文件
     * @param oldPath 舊文件路徑
     * @param newPath 新文件路徑
     * @return 是否重命名成功
     */
    public boolean renameFile(String oldPath,String newPath){
        if(fs == null) return false;
        Path old=new Path(oldPath);
        Path now=new Path(newPath);
        boolean rename = false;
        try {
            rename = fs.rename(old, now);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return rename;
    }

    /**
     * 刪除目錄和文件
     * @param remotePath HDFS文件路徑
     * @return 是否刪除成功
     */
    public boolean deleteFile(String remotePath){
        if(fs == null) return false;
        boolean success = false;
        try {
            success = fs.delete(new Path(remotePath), true);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return success;
    }

    /**
     * 檢查文件是否存在
     * @param remotePath HDFS文件路徑
     * @return 是否存在
     */
    public boolean existFile(String remotePath){
        if(fs == null) return false;
        boolean exist = false;
        try {
            exist = fs.exists(new Path(remotePath));
        } catch (IOException e) {
            e.printStackTrace();
        }
        return exist;
    }


    /**
     * 關閉FileSystem
     */
    public void closeFileSystem(){
        if(fs != null){
            try {
                fs.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

}

上面代碼都有注釋,這里就不過多解釋了阁苞,下面來看主類的執(zhí)行代碼:

package com.lzb.hdfs;


import com.lzb.hdfs.fs.HDFSHelper;

public class Demo {
    public static void main(String[] args) {

        HDFSHelper hdfsHelper = new HDFSHelper();

        String dir = "/test";
        String filename = "hello.txt";
        String path = dir + "/" + filename;

        boolean exist = hdfsHelper.existFile(path);
        System.out.println(path + " exist file ==> " + exist);

        if(!exist){
            boolean mkdir = hdfsHelper.mkdir(dir);
            System.out.println(dir + " create success ==> " + mkdir);

            boolean copyFromLocal = hdfsHelper.copyFromLocal("/"+filename, dir);
            System.out.println("upload success ==> " + copyFromLocal);

            hdfsHelper.listFiles(dir,false);

            String content = "hello world new";
            boolean write = hdfsHelper.writeToFile(path, content);
            System.out.println("write success ==> " + write);

            String data = hdfsHelper.readFromFile(path);
            System.out.println("read the data ==> " + data);

            String newPath = dir + "/hello2.txt";
            boolean renameFile = hdfsHelper.renameFile(path, newPath);
            System.out.println("rename success ==> " + renameFile);

            boolean copyToLocal = hdfsHelper.copyToLocal(newPath, "/hello2.txt");
            System.out.println("download success ==> " + copyToLocal);

            //boolean deleteFile = hdfsHelper.deleteFile(newPath);
            //System.out.println("delete success ==> " + deleteFile);

        }

        hdfsHelper.closeFileSystem();

    }
}

執(zhí)行結(jié)果如下:

log4j:WARN No appenders could be found for logger (org.apache.hadoop.metrics2.lib.MutableMetricsFactory).
log4j:WARN Please initialize the log4j system properly.
DFS[DFSClient[clientName=DFSClient_NONMAPREDUCE_-1866182384_1, ugi=root (auth:SIMPLE)]]
/test/hello.txt exist file ==> false
/test create success ==> true
upload success ==> true
file path === hdfs://probd/test/hello.txt
file name === hello.txt
file size === 12
file owner === root
file group === supergroup
file permission === rw-r--r--
file blocks === 3
file block size === 134217728
file block nums === 1
block host === Probd01
block host === Probd03
block host === Probd02
write success ==> true
read the data ==> hello world new
rename success ==> true
download success ==> true
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末困檩,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子那槽,更是在濱河造成了極大的恐慌悼沿,老刑警劉巖,帶你破解...
    沈念sama閱讀 217,185評論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件骚灸,死亡現(xiàn)場離奇詭異糟趾,居然都是意外死亡,警方通過查閱死者的電腦和手機甚牲,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評論 3 393
  • 文/潘曉璐 我一進店門义郑,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人鳖藕,你說我怎么就攤上這事魔慷≈欢В” “怎么了著恩?”我有些...
    開封第一講書人閱讀 163,524評論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長蜻展。 經(jīng)常有香客問我喉誊,道長,這世上最難降的妖魔是什么纵顾? 我笑而不...
    開封第一講書人閱讀 58,339評論 1 293
  • 正文 為了忘掉前任伍茄,我火速辦了婚禮,結(jié)果婚禮上施逾,老公的妹妹穿的比我還像新娘敷矫。我一直安慰自己例获,他們只是感情好,可當我...
    茶點故事閱讀 67,387評論 6 391
  • 文/花漫 我一把揭開白布曹仗。 她就那樣靜靜地躺著榨汤,像睡著了一般。 火紅的嫁衣襯著肌膚如雪怎茫。 梳的紋絲不亂的頭發(fā)上收壕,一...
    開封第一講書人閱讀 51,287評論 1 301
  • 那天,我揣著相機與錄音轨蛤,去河邊找鬼蜜宪。 笑死,一個胖子當著我的面吹牛祥山,可吹牛的內(nèi)容都是我干的圃验。 我是一名探鬼主播,決...
    沈念sama閱讀 40,130評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼缝呕,長吁一口氣:“原來是場噩夢啊……” “哼损谦!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起岳颇,我...
    開封第一講書人閱讀 38,985評論 0 275
  • 序言:老撾萬榮一對情侶失蹤照捡,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后话侧,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體栗精,經(jīng)...
    沈念sama閱讀 45,420評論 1 313
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,617評論 3 334
  • 正文 我和宋清朗相戀三年瞻鹏,在試婚紗的時候發(fā)現(xiàn)自己被綠了悲立。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 39,779評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡新博,死狀恐怖薪夕,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情赫悄,我是刑警寧澤原献,帶...
    沈念sama閱讀 35,477評論 5 345
  • 正文 年R本政府宣布,位于F島的核電站埂淮,受9級特大地震影響姑隅,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜倔撞,卻給世界環(huán)境...
    茶點故事閱讀 41,088評論 3 328
  • 文/蒙蒙 一讲仰、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧痪蝇,春花似錦鄙陡、人聲如沸冕房。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽毒费。三九已至,卻和暖如春愈魏,著一層夾襖步出監(jiān)牢的瞬間觅玻,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評論 1 269
  • 我被黑心中介騙來泰國打工培漏, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留溪厘,地道東北人。 一個月前我還...
    沈念sama閱讀 47,876評論 2 370
  • 正文 我出身青樓牌柄,卻偏偏與公主長得像畸悬,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子珊佣,可洞房花燭夜當晚...
    茶點故事閱讀 44,700評論 2 354