hadoop之旅5-idea通過maven搭建hdfs環(huán)境

相信大家通過之前的做法都已經(jīng)搭建起了一個hadoop的開發(fā)環(huán)境。今天帶大家通過java api來訪問hdfs文件系統(tǒng)

首先啟動hadoop集群

start-dfs.sh
或者
start-all.sh  //一鍵啟動hadoop集群和yarn集群

打開idea

pom.xml文件里加入hadoop的依賴烛缔,我這里使用的是我搭建的一樣版本的依賴
hadoop 2.7.3

<properties>
    <hadoop.version>2.7.3</hadoop.version>
</properties>

<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-common</artifactId>
    <version>${hadoop.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-client</artifactId>  
    <version>${hadoop.version}</version>
</dependency>
<dependency>
    <groupId>org.apache.hadoop</groupId>
    <artifactId>hadoop-hdfs</artifactId>
    <version>${hadoop.version}</version>
</dependency>

Java api在hdfs上創(chuàng)建一個文件目錄

//創(chuàng)建配置文件
Configuration conf = new Configuration();
 //windows下無法找到對應的環(huán)境變量,需要設置砍鸠。把hadoop解壓下來的根目錄
System.setProperty("hadoop.home.dir", "F:\\linux\\hadoop-2.7.3");
// 指定hadoop fs的地址
conf.set("fs.defaultFS", "hdfs://master:9000");
//定義訪問的根目錄
String userRootPath = "/userSpace"
//拿到文件操作對象
FileSystem fs = FileSystem.get(conf);
//創(chuàng)建一個path對象,hdfs上的目錄都需要用path對象來訪問
Path dir = new Path(userRootPath);  //表示根目錄下的 userSpace目錄
boolean result = fs.mkdirs(dir);
if(result){
    System.out.println("創(chuàng)建目錄成功耕驰!");
}

其實就和java訪問文件一樣的操作類似爷辱,非常簡單,不過操作hdfs主要是通過FileSystem類朦肘。下面給大家貼一下完整代碼

package com.mmcc.springboothadoop.hadoop;

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

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class HdfsUtils {
    private static Configuration conf = new Configuration();
    public static String userRootPath = "/userSpace";

    static {
        //windows下無法找到對應的環(huán)境變量饭弓,需要設置
        System.setProperty("hadoop.home.dir", "F:\\linux\\hadoop-2.7.3");
        // 指定hadoop fs的地址
        conf.set("fs.defaultFS", "hdfs://master:9000");
    }

    //判斷路徑是否存在
    public static boolean exists(String path) throws IOException {
        FileSystem fileSystem = FileSystem.get(conf);
        return fileSystem.exists(new Path(path));
    }

    //創(chuàng)建文件
    public static void createFile(String filePath, byte[] contents) throws IOException {
        FileSystem fileSystem = FileSystem.get(conf);
        Path path = new Path(filePath);
        FSDataOutputStream fdo = fileSystem.create(path);
        fdo.write(contents);
        fdo.close();
        fileSystem.close();

    }

    /**
     * 創(chuàng)建文件
     *
     * @param filePath
     * @param fileContent
     * @throws IOException
     */
    public static void createFile(String filePath, String fileContent)
            throws IOException {
        createFile(filePath, fileContent.getBytes());
    }

    //從本地復制到hdfs上
    public static void copyFromLocalFile(String localFilePath, String remoteFilePath) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        Path localPath = new Path(localFilePath);
        Path remotePath = new Path(remoteFilePath);
        fs.copyFromLocalFile(false, true, localPath, remotePath);
        fs.close();
    }


    /**
     * 刪除目錄或文件
     *
     * @param remoteFilePath
     * @param recursive
     * @return
     * @throws IOException
     */
    public static boolean deleteFile(String remoteFilePath, boolean recursive)
            throws IOException {
        FileSystem fs = FileSystem.get(conf);
        boolean result = fs.delete(new Path(remoteFilePath), recursive);
        fs.close();
        return result;
    }
    /**
     * 刪除目錄或文件(如果有子目錄,則級聯(lián)刪除)
     *
     * @param remoteFilePath
     * @return
     * @throws IOException
     */
    public static boolean deleteFile(String remoteFilePath) throws IOException {
        return deleteFile(remoteFilePath, true);
    }
    /**
     * 文件重命名
     *
     * @param oldFileName
     * @param newFileName
     * @return
     * @throws IOException
     */
    public static boolean renameFile(String oldFileName, String newFileName)
            throws IOException {
        FileSystem fs = FileSystem.get(conf);
        Path oldPath = new Path(oldFileName);
        Path newPath = new Path(newFileName);
        boolean result = fs.rename(oldPath, newPath);
        fs.close();
        return result;
    }
    /**
     * 創(chuàng)建目錄
     *
     * @param dirName
     * @return
     * @throws IOException
     */
    public static boolean createDirectory(String dirName) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        Path dir = new Path(dirName);
        boolean result = false;
        if (!fs.exists(dir)) {
            result = fs.mkdirs(dir);
        }
        fs.close();
        return result;
    }

    //列出指定路徑下的文件
    public static  RemoteIterator<LocatedFileStatus> listFiles(String dirPath,boolean recursive) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        RemoteIterator<LocatedFileStatus> listFiles = fs.listFiles(new Path(dirPath), recursive);//不進行遞歸
        fs.close();
        return listFiles;
    }
    /**
     * 列出指定路徑下的文件(非遞歸)
     *
     * @param basePath
     * @return
     * @throws IOException
     */
    public static RemoteIterator<LocatedFileStatus> listFiles(String basePath)
            throws IOException {
        FileSystem fs = FileSystem.get(conf);
        RemoteIterator<LocatedFileStatus> remoteIterator = fs.listFiles(
                new Path(basePath), false);
        fs.close();
        return remoteIterator;
    }

    /**
     * 列出指定目錄下的文件\子目錄信息(非遞歸)
     *
     * @param dirPath
     * @return
     * @throws IOException
     */
    public static FileStatus[] listStatus(String dirPath) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        FileStatus[] fileStatuses = fs.listStatus(new Path(dirPath));
        fs.close();
        return fileStatuses;
    }

    //讀取文件內容
    public static byte[] readFile(String filePath) throws IOException {
        byte[] fileContent = null;
        FileSystem fs = FileSystem.get(conf);
        Path path = new Path(filePath);

        if (fs.exists(path)){
            FSDataInputStream fsin = fs.open(path);
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            IOUtils.copyBytes(fsin,bos,conf);
            fileContent = bos.toByteArray();
        }
        return fileContent;
    }

    //下載hdfs上的文件
    public static void download(String remote,String local) throws IOException {
        FileSystem fs = FileSystem.get(conf);
        //遠程hdfs上的文件
        Path remotePath = new Path(remote);
        //本地的文件
        Path localPath = new Path(local);
        fs.copyToLocalFile(remotePath,localPath);
        fs.close();
    }
}
?著作權歸作者所有,轉載或內容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市媒抠,隨后出現(xiàn)的幾起案子示启,更是在濱河造成了極大的恐慌,老刑警劉巖领舰,帶你破解...
    沈念sama閱讀 221,820評論 6 515
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異迟螺,居然都是意外死亡冲秽,警方通過查閱死者的電腦和手機,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 94,648評論 3 399
  • 文/潘曉璐 我一進店門矩父,熙熙樓的掌柜王于貴愁眉苦臉地迎上來锉桑,“玉大人,你說我怎么就攤上這事窍株∶裰幔” “怎么了攻柠?”我有些...
    開封第一講書人閱讀 168,324評論 0 360
  • 文/不壞的土叔 我叫張陵,是天一觀的道長后裸。 經(jīng)常有香客問我瑰钮,道長,這世上最難降的妖魔是什么微驶? 我笑而不...
    開封第一講書人閱讀 59,714評論 1 297
  • 正文 為了忘掉前任浪谴,我火速辦了婚禮,結果婚禮上因苹,老公的妹妹穿的比我還像新娘苟耻。我一直安慰自己,他們只是感情好扶檐,可當我...
    茶點故事閱讀 68,724評論 6 397
  • 文/花漫 我一把揭開白布凶杖。 她就那樣靜靜地躺著,像睡著了一般款筑。 火紅的嫁衣襯著肌膚如雪智蝠。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 52,328評論 1 310
  • 那天醋虏,我揣著相機與錄音寻咒,去河邊找鬼。 笑死颈嚼,一個胖子當著我的面吹牛毛秘,可吹牛的內容都是我干的。 我是一名探鬼主播阻课,決...
    沈念sama閱讀 40,897評論 3 421
  • 文/蒼蘭香墨 我猛地睜開眼叫挟,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了限煞?” 一聲冷哼從身側響起抹恳,我...
    開封第一講書人閱讀 39,804評論 0 276
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎署驻,沒想到半個月后奋献,有當?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,345評論 1 318
  • 正文 獨居荒郊野嶺守林人離奇死亡旺上,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 38,431評論 3 340
  • 正文 我和宋清朗相戀三年瓶蚂,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片宣吱。...
    茶點故事閱讀 40,561評論 1 352
  • 序言:一個原本活蹦亂跳的男人離奇死亡窃这,死狀恐怖,靈堂內的尸體忽然破棺而出征候,到底是詐尸還是另有隱情杭攻,我是刑警寧澤祟敛,帶...
    沈念sama閱讀 36,238評論 5 350
  • 正文 年R本政府宣布,位于F島的核電站兆解,受9級特大地震影響馆铁,放射性物質發(fā)生泄漏。R本人自食惡果不足惜痪宰,卻給世界環(huán)境...
    茶點故事閱讀 41,928評論 3 334
  • 文/蒙蒙 一叼架、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧衣撬,春花似錦乖订、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,417評論 0 24
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至扛点,卻和暖如春哥遮,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背陵究。 一陣腳步聲響...
    開封第一講書人閱讀 33,528評論 1 272
  • 我被黑心中介騙來泰國打工眠饮, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人铜邮。 一個月前我還...
    沈念sama閱讀 48,983評論 3 376
  • 正文 我出身青樓仪召,卻偏偏與公主長得像,于是被迫代替她去往敵國和親松蒜。 傳聞我的和親對象是個殘疾皇子扔茅,可洞房花燭夜當晚...
    茶點故事閱讀 45,573評論 2 359

推薦閱讀更多精彩內容

  • 做好自己就好了,我沒必要取悅任何人
    昨日蒙冉閱讀 127評論 0 0
  • toString() 基礎數(shù)據(jù)類型使用該方法將值按其字面量形式輸出 復雜數(shù)據(jù)類型調用該方法時將 Object 類...
    BigBigWen大文閱讀 588評論 0 0
  • 今天微博再一次次把微微一笑很傾城推上了熱搜秸苗,火到思密達去了召娜。韓國妹子說楊洋的聲音好蘇,自帶低音炮惊楼【寥常看得自己的小心臟...
    那自敘閱讀 758評論 2 7
  • 在滔滔不絕的暢聊中 一句謊言 忽然被挑破 所有一切陷入沉默 似乎身體里的單細胞都放棄了思考 干脆 快捷 像雞蛋碰到...
    玄素_cc30閱讀 294評論 0 0
  • 去年朋友家的白貓生了五只店读,一三五頭上都有一撮黑毛,二四全白攀芯。問我要哪只。 我說全白的文虏! 朋友說好嘞侣诺! 我給它起了名...
    病字號隱君子閱讀 549評論 11 6