hadoop 學(xué)習(xí)筆記(2)-- hadoop 文件系統(tǒng)

當(dāng)數(shù)據(jù)量很大,一臺物理機(jī)無法容納時,我們就需要將數(shù)據(jù)存儲到由網(wǎng)絡(luò)連接的若干臺機(jī)器上,這就是所謂的分布式文件系統(tǒng)。hadoop 使用 hdfs 作為自己的分布式文件系統(tǒng)。目前 hdfs 不適合低延遲訪問,也不適合存儲大量的小文件以及隨機(jī)寫入(hdfs 中只能在文件末尾進(jìn)行寫操作)。

1 hdfs 的概念

硬盤有自己的塊大小,表示其可以讀寫的最小數(shù)據(jù)量。文件系統(tǒng)塊的大小為若干倍的硬盤大小。
hdfs 上的文件也以塊來劃分,默認(rèn)為 128M,為了容錯,每個 block 通常在其他節(jié)點(diǎn)有若干個備份,一個節(jié)點(diǎn)出問題了可以使用別的節(jié)點(diǎn)的數(shù)據(jù)。

使用下面的命令可以查看 hdfs 的信息:

hdfs fsck / -files -blocks

2 namenode 與 datanode

hdfs 集群包括兩種節(jié)點(diǎn):

  • namenode:是集群的管理者,記錄著文件系統(tǒng)樹以及所有文件和目錄的元數(shù)據(jù)。namenode 還記錄著組成文件的 block 的位置(在每次系統(tǒng)啟動時建立)
  • datanode:存儲數(shù)據(jù)并提供數(shù)據(jù),定時向 namenode 同步自己存儲的 block 的信息

經(jīng)常被讀取的文件的相關(guān) block 會被緩存在 datanode 的內(nèi)存中,用戶也可以指定哪些文件的 block 應(yīng)當(dāng)被緩存。

3 命令行接口

在學(xué)習(xí) hadoop 時,我們使用偽分布式的方法啟動 hadoop 集群。在 **core-site.xml ** 中可以通過設(shè)置下面的屬性來配置默認(rèn)的文件系統(tǒng)。

<property>
       <name>fs.defaultFS</name>
       <value>hdfs://localhost:9000</value>
</property>

基礎(chǔ)的操作有:

//將本地文件系統(tǒng)的數(shù)據(jù)復(fù)制到 hdfs 中,由于已經(jīng)配置了 fs.defaultFS 可以省略 hdfs://localhost:9000
hdfs dfs -copyFromLocal /test/data/* /test1/input
//使用相對路徑會將數(shù)據(jù)復(fù)制到用戶目錄下 我這里是 /user/hadoop
hdfs dfs -copyFromLocal /test/data/* input
//從hdfs copy 到本地
hdfs dfs -copyToLocal test.txt text.txt
//創(chuàng)建文件夾
hdfs dfs -mkdir books
//列出所有文件
hdfs dfs -ls .
//print 文件的內(nèi)容
hdfs dfs -cat /user/hadoop/books

hdfs 的文件也有 r w x 的概念,當(dāng)然這里的 x 是指是否可以訪問一個目錄下面的文件。hdfs 中的文件也有所屬 user 以及 group 的概念,使用當(dāng)前調(diào)用者進(jìn)程的 user 和 group 來標(biāo)識,運(yùn)行 namenode 的用戶有 “root” 權(quán)限。

hadoop 有一個抽象的文件系統(tǒng),hdfs 是其實(shí)現(xiàn)之一,通過 uri 的方式,可以選擇不同的文件系統(tǒng),比如hdfs

hdfs dfs -ls file:///home/hadoop

4 java api

使用 URL 就可以訪問 hdfs 文件,不過要進(jìn)行一點(diǎn)小處理,設(shè)置 UrlStreamHandlerFactory:

不方便設(shè)置 UrlStreamHandlerFactory 時,可以使用 FileSystem Api:

public class ReadFile {
    public static void main(String[] args) throws IOException {
        if (args.length != 1) {
            System.out.println("Usage: ReadFile path/to/file");
            System.exit(-1);
        }
        String filePath = args[0];
        if (!filePath.startsWith("/")) {
            filePath = "/" + filePath;
        }
        filePath = "hdfs://localhost:9000" + filePath;
        // conf 會根據(jù) classpath 中的 core-site.xml 創(chuàng)建
        Configuration conf = new Configuration();
        //創(chuàng)建 fileSystem 實(shí)例
        FileSystem fileSystem = FileSystem.get(URI.create(filePath), conf);
        InputStream is = null;
        try {
            //使用 open 打開 InputStream
            is = fileSystem.open(new Path(filePath));
            IOUtils.copyBytes(is, System.out, 4096, false);
        } finally {
            IOUtils.closeStream(is);
        }
    }
}

其中,F(xiàn)ileSystem.open 返回的是一個 FSDataInputStream 對象,可以使用 seek 調(diào)整讀取的位置:

另外,其還實(shí)現(xiàn)了 PositionedReadable 接口,可以從指定位置讀取指定的數(shù)據(jù):

public interface PositionedReadable {
  /**
   * Read upto the specified number of bytes, from a given
   * position within a file, and return the number of bytes read. This does not
   * change the current offset of a file, and is thread-safe.
   */
  public int read(long position, byte[] buffer, int offset, int length)
    throws IOException;
  
  /**
   * Read the specified number of bytes, from a given
   * position within a file. This does not
   * change the current offset of a file, and is thread-safe.
   */
  public void readFully(long position, byte[] buffer, int offset, int length)
    throws IOException;
  
  /**
   * Read number of bytes equal to the length of the buffer, from a given
   * position within a file. This does not
   * change the current offset of a file, and is thread-safe.
   */
  public void readFully(long position, byte[] buffer) throws IOException;
}

使用 FileSystem api 也可以進(jìn)行寫操作:

        String localFile = args[0];
        String hdfsFile = args[1];
        if (!hdfsFile.startsWith("/")) {
            hdfsFile = "/" + hdfsFile;
        }
        hdfsFile = "hdfs://localhost:9000" + hdfsFile;

        InputStream in = null;
        FSDataOutputStream out = null;
        try {
            in = new BufferedInputStream(new FileInputStream(localFile));

            FileSystem fileSystem = FileSystem.get(URI.create(hdfsFile), new Configuration());
            out = fileSystem.create(new Path(hdfsFile));

            IOUtils.copyBytes(in, out, 4096, true);
        } catch (Exception e) {
            e.printStackTrace();
        }

通過 FileSystem.getStatus 可以查看文件的狀態(tài)。

通過 FileSystem.listStatus 可以獲取文件夾下的所有文件的信息。

通過 FileSystem.globStatus 可以按照一定的 pattern 查看

通過 PathFilter 可以在globStatus 加入更細(xì)粒度的控制:

fs.globStatus(new Path("/2007/*/*"), new RegexExcludeFilter("^.*/2017/12/31$"));

刪除數(shù)據(jù)使用 FileSystem 的 delete 方法進(jìn)行。

文件讀取的過程中,從 namenode 獲取 block 的位置,再去對應(yīng)的 datanode 讀取數(shù)據(jù):

文件讀取

文件寫入數(shù)據(jù)的過程中,先在 namenode 創(chuàng)建元數(shù)據(jù),然后向一個 datanode 寫入數(shù)據(jù),該 datanode 會同時向其他 datanode 同步數(shù)據(jù)。


寫入數(shù)據(jù)

數(shù)據(jù)寫入時,應(yīng)該在需要的地方調(diào)用輸出流的 sync,以便于將緩存寫入文件系統(tǒng)中,注意 close 時也會調(diào)用一次 sync。

5 distcp

使用 distcp 可以并發(fā)的進(jìn)行數(shù)據(jù)復(fù)制,個人認(rèn)為功能可以與 rsync 類比。

hadoop@millions-server:/usr/local/hadoop/etc/hadoop$ hdfs dfs -ls testdata
Found 1 items
-rw-r--r-- 1 hadoop supergroup 288374 2017-03-07 23:22 testdata/synthetic_control.data

而后輸入:

hadoop distcp -update testdata/synthetic_control.data testdata/synthetic_control2.data

可以看到:

hadoop@millions-server:/usr/local/hadoop/etc/hadoop$ hdfs dfs -ls testdata
Found 2 items
-rw-r--r-- 1 hadoop supergroup 288374 2017-03-07 23:22 testdata/synthetic_control.data
-rw-r--r-- 1 hadoop supergroup 288374 2017-05-06 23:09 testdata/synthetic_control2.data

再來同步一個文件夾:

hadoop distcp testdata testdata2
hdfs dfs -ls testdata2
Found 2 items
-rw-r--r-- 1 hadoop supergroup 288374 2017-05-06 23:14 testdata2/synthetic_control.data
-rw-r--r-- 1 hadoop supergroup 288374 2017-05-06 23:14 testdata2/synthetic_control2.data

刪除 testdata 中的 synthetic_control2.data:

hdfs dfs -rm testdata/synthetic_control2.data

再進(jìn)行一次帶 -update 和 -delete 的 distcp,可以看到,多余的文件被刪除了:

hadoop distcp -update -delete testdata testdata2
hdfs dfs -ls testdata2
Found 1 items
-rw-r--r-- 1 hadoop supergroup 288374 2017-05-06 23:14 testdata2/synthetic_control.data

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 230,501評論 6 544
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件,死亡現(xiàn)場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī),發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 99,673評論 3 429
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 178,610評論 0 383
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經(jīng)常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 63,939評論 1 318
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當(dāng)我...
    茶點(diǎn)故事閱讀 72,668評論 6 412
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 56,004評論 1 329
  • 那天,我揣著相機(jī)與錄音,去河邊找鬼。 笑死,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 44,001評論 3 449
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 43,173評論 0 290
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 49,705評論 1 336
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 41,426評論 3 359
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 43,656評論 1 374
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 39,139評論 5 364
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 44,833評論 3 350
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 35,247評論 0 28
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 36,580評論 1 295
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 52,371評論 3 400
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 48,621評論 2 380

推薦閱讀更多精彩內(nèi)容