hadoop archive詳解

前言

hdfs並不擅長存儲小文件,因爲每個文件最少一個block,每個block的元數據都會在NameNode中佔用150byte內存。如果存儲大量的小文件,它們會喫掉NameNode節點的大量內存。MR案例:小文件處理方案

Hadoop Archive或者HAR,是一個高效地將小文件放入HDFS塊中的文件存檔工具。它能將多個小文件打包成一個HAR文件,這樣在減少NameNode內存使用的同時,仍然允許對小文件進行透明的訪問,比如作爲MapReduce的輸入。

[root@master ~]# hadoop fs -rm /c23.txt
20/01/27 04:29:38 INFO fs.TrashPolicyDefault: Moved: ‘hdfs://master:8020/c23.txt’ to trash at: hdfs://master:8020/user/root/.Trash/Current/c23.txt
[root@master ~]#

使用方法

歸檔前的目錄

目錄small-file下的小文件

[root@master ~]# hadoop fs -ls /1daoyun/small-file
Found 11 items
-rw-r--r--   3 root hdfs         60 2020-01-27 01:22 /1daoyun/small-file/1.txt
-rw-r--r--   3 root hdfs         49 2020-01-27 01:22 /1daoyun/small-file/10.txt
-rw-r--r--   3 root hdfs         54 2020-01-27 01:22 /1daoyun/small-file/11.txt
-rw-r--r--   3 root hdfs         56 2020-01-27 01:22 /1daoyun/small-file/2.txt
-rw-r--r--   3 root hdfs         44 2020-01-27 01:22 /1daoyun/small-file/3.txt
-rw-r--r--   3 root hdfs         53 2020-01-27 01:22 /1daoyun/small-file/4.txt
-rw-r--r--   3 root hdfs         69 2020-01-27 01:22 /1daoyun/small-file/5.txt
-rw-r--r--   3 root hdfs         55 2020-01-27 01:22 /1daoyun/small-file/6.txt
-rw-r--r--   3 root hdfs         59 2020-01-27 01:22 /1daoyun/small-file/7.txt
-rw-r--r--   3 root hdfs         54 2020-01-27 01:22 /1daoyun/small-file/8.txt
-rw-r--r--   3 root hdfs         64 2020-01-27 01:22 /1daoyun/small-file/9.txt

歸檔命令

可以通過參數 -D har.block.size 指定HAR的大小

shell> hadoop archive -archiveName NAME -p <parent path> <src>* <dest>
-archiveName xiandian.har  : 指定歸檔後的文件名
-p /1daoyun/small-file : 被歸檔文件所在的父目錄
1.txt 6.txt : 要被歸檔的目錄,一至多個,比如只歸檔1txt和6.txt),一個都沒選,就是默認全選
/user/root : 生成的歸檔文件存儲目錄

歸檔後的目錄結構

這裏看的是/user/root/xiandian.har目錄下的文件

[root@master ~]# hadoop fs -lsr /user/root
lsr: DEPRECATED: Please use 'ls -R' instead.
drwx------   - root hdfs          0 2020-01-27 01:23 /user/root/.staging
drwxr-xr-x   - root hdfs          0 2020-01-27 01:23 /user/root/xiandian.har
-rw-r--r--   3 root hdfs          0 2020-01-27 01:23 /user/root/xiandian.har/_SUCCESS
-rw-r--r--   3 root hdfs        735 2020-01-27 01:23 /user/root/xiandian.har/_index
-rw-r--r--   3 root hdfs         23 2020-01-27 01:23 /user/root/xiandian.har/_masterindex
-rw-r--r--   3 root hdfs        617 2020-01-27 01:23 /user/root/xiandian.har/part-0
[root@master ~]# 

查看結果文件【part-0】內容

[root@master ~]# hadoop fs -cat /user/root/xiandian.har/part-0
HTML XHTML CSS JS JQuery   ҳܹ  Web   Ԥ    ̸ Ԥ     HTML XHTML CSS JS JQuery   ҳܹ HTML XHTML CSS JS JQuery   ҳܹL XHTML JQuery   ҳܹ  Web רҵ      ֤ȯ       רҵ      ֤ȯ     רҵ    ֤ȯ       רҵ            Ԥ       ̸oot@master ~]# 

1)使用har uri去訪問原始數據

HAR是HDFS之上的一個文件系統,因此所有 fs shell 命令對 HAR 文件均可用,只不過文件路徑格式不一樣

[root@master ~]# hadoop fs -ls har:///user/root/xiandian.har
Found 11 items
-rw-r--r--   3 root hdfs         60 2020-01-27 01:22 har:///user/root/xiandian.har/1.txt
-rw-r--r--   3 root hdfs         49 2020-01-27 01:22 har:///user/root/xiandian.har/10.txt
-rw-r--r--   3 root hdfs         54 2020-01-27 01:22 har:///user/root/xiandian.har/11.txt
-rw-r--r--   3 root hdfs         56 2020-01-27 01:22 har:///user/root/xiandian.har/2.txt
-rw-r--r--   3 root hdfs         44 2020-01-27 01:22 har:///user/root/xiandian.har/3.txt
-rw-r--r--   3 root hdfs         53 2020-01-27 01:22 har:///user/root/xiandian.har/4.txt
-rw-r--r--   3 root hdfs         69 2020-01-27 01:22 har:///user/root/xiandian.har/5.txt
-rw-r--r--   3 root hdfs         55 2020-01-27 01:22 har:///user/root/xiandian.har/6.txt
-rw-r--r--   3 root hdfs         59 2020-01-27 01:22 har:///user/root/xiandian.har/7.txt
-rw-r--r--   3 root hdfs         54 2020-01-27 01:22 har:///user/root/xiandian.har/8.txt
-rw-r--r--   3 root hdfs         64 2020-01-27 01:22 har:///user/root/xiandian.har/9.txt
[root@master ~]# 

2)用har uri訪問下一級目錄

[root@master ~]# hadoop fs -ls har:///user/root/xiandian.har/1.txt
-rw-r--r--   3 root hdfs         60 2020-01-27 01:22 har:///user/root/xiandian.har/1.txt
[root@master ~]# 

3)遠程訪問,可以使用以下命令

[root@ncst ~]# hadoop fs -lsr har://hdfs-ncst:9000/test/in/har/small.har

4)刪除har文件必須用rmr命令,rm是不行的

[root@master ~]# hadoop fs -rmr /user/root/xiandian.har

5)使用 HAR 作爲 MapReduce 的輸入

[root@ncst ~]#  hadoop jar /***/hadoop-mapreduce-examples-2.2.0.jar wordcount \
> har:///test/in/har/0825.har/mapjoin //輸入路徑
> /test/out/0825/05 //輸出路徑

存在的問題

存檔文件的源文件及目錄都不會自動刪除,需要手動刪除
存檔過程實際是一個MapReduce過程,所以需要hadoop的MapReduce支持
存檔文件本身不支持壓縮
存檔文件一旦創建便不可修改,要想從中刪除或增加文件,必須重新建立存檔文件
創建存檔文件會創建原始文件的副本,所以至少需要有與存檔文件容量相同的磁盤空間
使用 HAR 作爲MR的輸入,MR可以訪問其中所有的文件。但是由於InputFormat不會意識到這是個歸檔文件,也就不會有意識的將多個文件劃分到單獨的Input-Split中,所以依然是按照多個小文件來進行處理,效率依然不高

6)HAR 結構,二級索引

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章