本章節咱們講講HDFS的一些其餘雜項功能,他們都是做爲輔助功能而存在的。java
咱們之間使用scp實現了兩個遠程主機之間的文件複製,該方式能夠實現文件的推拉。node
scp -r hello.txt root@h133:~/hello.txt //push scp -r root@h134:/user/hello.txt hello.txt //pull scp -r root@h1333:/user/hello.txt root@h134:/user/ //經過本地主機中轉實現兩個遠程主機的文件複製;若是在兩個遠程主機之間ssh沒有配置的狀況下可使用該方式。
咱們也能夠採用discp命令實現兩個hadoop集羣之間的遞歸數據複製web
bin/hadoop distcp hdfs://h133:9000/user/hello.txt hdfs://h233:9000/user/hello.txt
咱們目前的環境只有一個集羣,因此暫時沒法演示。shell
每一個文件均按塊存儲,每一個塊的元數據存儲在namenode的內存中,所以hadoop存儲小文件會很是低效。由於大量的小文件會耗盡namenode中的大部份內存。但注意,存儲小文件所須要的磁盤容量和存儲這些文件原始內容所須要的磁盤空間相比也不會增多。例如,一個1MB的文件以大小爲128MB的塊存儲,使用的是1MB的磁盤空間,而不是128MB。編程
Hadoop存檔文件或HAR文件,是一個更高效的文件存檔工具,它將文件存入HDFS塊,在減小namenode內存使用的同時,容許對文件進行透明的訪問。安全
這樣作的好處:Hadoop存檔文件能夠用做MapReduce的輸入。ssh
一、須要啓動yarn進程tcp
start-yarn.sh
二、歸檔文件 歸檔成一個叫作xxx.har的文件夾,該文件夾下有相應的數據文件。Xx.har目錄是一個總體,該目錄當作是一個歸檔文件便可。ide
archive -archiveName <NAME>.har -p <parent path> [-r <replication factor>]<src>* <dest>
咱們練習歸檔,將/user這個目錄進行歸檔。工具
#### 查看/user中的文件 [root@h135 current]# hadoop fs -ls /user Found 7 items -rw-r--r-- 3 root supergroup 5 2019-01-03 16:38 /user/444.txt -rw-r--r-- 3 root supergroup 19 2019-01-04 13:24 /user/consisit.txt -rw-r--r-- 3 root supergroup 19 2019-01-03 16:27 /user/h132.txt -rw-r--r-- 3 root supergroup 19 2019-01-03 16:28 /user/h133.txt -rw-r--r-- 3 root supergroup 23 2019-01-05 15:11 /user/h134.txt -rw-r--r-- 3 root supergroup 19 2019-01-03 16:28 /user/h135.txt drwxr-xr-x - root supergroup 0 2019-01-03 15:58 /user/zhaoyi #### 執行歸檔操做 [root@h135 current]# hadoop archive -archiveName myhar.har -p /user / #### 查看歸檔以後目錄變化 [root@h135 current]# hadoop fs -ls / Found 7 items -rw-r--r-- 3 root supergroup 19 2019-01-05 14:57 /h134.txt drwxr-xr-x - root supergroup 0 2019-01-06 10:23 /myhar.har -rw-r--r-- 3 root supergroup 23 2019-01-05 19:07 /newslaver.txt -rw-r--r-- 3 root supergroup 4 2019-01-04 15:50 /seen_txid drwxr-xr-x - root supergroup 0 2019-01-05 19:34 /system drwx------ - root supergroup 0 2019-01-06 10:20 /tmp drwxr-xr-x - root supergroup 0 2019-01-05 15:11 /user #### 查看歸檔生成的har文件內容 [root@h135 current]# hadoop fs -ls -R /myhar.har -rw-r--r-- 3 root supergroup 0 2019-01-06 10:23 /myhar.har/_SUCCESS -rw-r--r-- 5 root supergroup 699 2019-01-06 10:23 /myhar.har/_index -rw-r--r-- 5 root supergroup 23 2019-01-06 10:23 /myhar.har/_masterindex -rw-r--r-- 3 root supergroup 133 2019-01-06 10:23 /myhar.har/part-0 #### 查看歸檔文件原文件內容 [root@h135 current]# hadoop fs -ls -R har:///myhar.har -rw-r--r-- 3 root supergroup 5 2019-01-03 16:38 har:///myhar.har/444.txt -rw-r--r-- 3 root supergroup 19 2019-01-04 13:24 har:///myhar.har/consisit.txt -rw-r--r-- 3 root supergroup 19 2019-01-03 16:27 har:///myhar.har/h132.txt -rw-r--r-- 3 root supergroup 19 2019-01-03 16:28 har:///myhar.har/h133.txt -rw-r--r-- 3 root supergroup 23 2019-01-05 15:11 har:///myhar.har/h134.txt -rw-r--r-- 3 root supergroup 19 2019-01-03 16:28 har:///myhar.har/h135.txt drwxr-xr-x - root supergroup 0 2019-01-03 15:58 har:///myhar.har/zhaoyi -rw-r--r-- 3 root supergroup 29 2019-01-03 15:58 har:///myhar.har/zhaoyi/a.txt drwxr-xr-x - root supergroup 0 2019-01-03 15:14 har:///myhar.har/zhaoyi/input #### 操做歸檔文件的內容(複製一個文件) [root@h135 current]# hadoop fs -cp har:///myhar.har/444.txt / [root@h135 current]# hadoop fs -ls / Found 8 items -rw-r--r-- 3 root supergroup 5 2019-01-06 10:27 /444.txt -rw-r--r-- 3 root supergroup 19 2019-01-05 14:57 /h134.txt drwxr-xr-x - root supergroup 0 2019-01-06 10:23 /myhar.har -rw-r--r-- 3 root supergroup 23 2019-01-05 19:07 /newslaver.txt -rw-r--r-- 3 root supergroup 4 2019-01-04 15:50 /seen_txid drwxr-xr-x - root supergroup 0 2019-01-05 19:34 /system drwx------ - root supergroup 0 2019-01-06 10:20 /tmp drwxr-xr-x - root supergroup 0 2019-01-05 15:11 /user
能夠看到,歸檔過程當中輸出的日誌記錄代表後臺的操做就是由mapreduce完成的,因此咱們事先需將yarn相關守護進程開啓。
執行歸檔操做以後,會在/也就是咱們歸檔指定的目錄生成har文件,而且原先的歸檔文件夾仍是存在的,能夠理解這是一個安全的拷貝操做。歸檔完成以後,能夠按本身的需求決定是否刪除原歸檔文件。
假設咱們想要將歸檔文件「解壓」出來(注意這其實和解壓不同),咱們能夠執行下面的命令,其實就是拷貝操做。
[root@h135 current]# hadoop fs -cp har:///myhar.har/* /
快照至關於對目錄作一個備份。並不會當即複製全部文件,而是指向同一個文件。當寫入發生時,纔會產生新文件。
快照相關語法:
(1)hdfs dfsadmin -allowSnapshot 路徑 (功能描述:開啓指定目錄的快照功能) (2)hdfs dfsadmin -disallowSnapshot 路徑 (功能描述:禁用指定目錄的快照功能,默認是禁用) (3)hdfs dfs -createSnapshot 路徑 (功能描述:對目錄建立快照) (4)hdfs dfs -createSnapshot 路徑 名稱 (功能描述:指定名稱建立快照) (5)hdfs dfs -renameSnapshot 路徑 舊名稱 新名稱 (功能描述:重命名快照) (6)hdfs lsSnapshottableDir (功能描述:列出當前用戶全部可快照目錄) (7)hdfs snapshotDiff 路徑1 路徑2 (功能描述:比較兩個快照目錄的不一樣之處) (8)hdfs dfs -deleteSnapshot <path> <snapshotName> (功能描述:刪除快照)
咱們練習使用一下快照 一、在/目錄下建立一個文件夾test,並往裏面上傳2個文件。
[root@h133 current]# hadoop fs -mkdir /test [root@h133 ~]# hadoop fs -put a.txt /test [root@h133 ~]# hadoop fs -put b.txt /test [root@h133 ~]# hadoop fs -ls -R / drwxr-xr-x - root supergroup 0 2019-01-06 10:51 /test -rw-r--r-- 3 root supergroup 29 2019-01-06 10:51 /test/a.txt -rw-r--r-- 3 root supergroup 21 2019-01-06 10:51 /test/b.txt
二、爲test目錄建立快照,並重命名爲test-snap
[root@h133 ~]# hdfs dfsadmin -allowSnapshot /test Allowing snaphot on /test succeeded [root@h133 ~]# hdfs dfs -createSnapshot /test test-snap Created snapshot /test/.snapshot/test-snap
建立快照以前要開啓對應目錄的快照開啓功能
-allowSnapshot
。
三、查看快照目錄
[root@h133 ~]# hdfs lsSnapshottableDir drwxr-xr-x 0 root supergroup 0 2019-01-06 10:53 1 65536 /test
四、上傳一個文件到/test,而後查看和快照之間的不一樣
[root@h133 ~]# hadoop fs -put h133.txt /test [root@h133 ~]# hdfs snapshotDiff /test . .snapshot/test-snap Difference between current directory and snapshot test-snap under directory /test: M . - ./h133.txt
能夠看到,顯示有所修改,而且快照相比於最新版本的目錄少了一個h133.txt文件。
hdfs snapshotDiff /test . .snapshot/test-snap
中,.
表明的是當前狀態,查看命令說明:
hdfs snapshotDiff <snapshotDir> <from> <to>: Get the difference between two snapshots, or between a snapshot and the current tree of a directory. For <from>/<to>, users can use "." to present the current status, and use ".snapshot/snapshot_name" to present a snapshot, where ".snapshot/" can be omitted
該命令也能夠用於不一樣快照之間的差別性比較。
五、恢復快照文件
若是咱們想還原之間的快照版本,HDFS這個版本是沒有提供任何命令的,簡單的來講,咱們能夠經過cp命令直接copy快照文件到原路徑便可。算是一種恢復快照文件的方案。
[root@h133 ~]# hadoop fs -rm -r /test/* 19/01/06 12:51:42 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes. Deleted /test/a.txt 19/01/06 12:51:42 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes. Deleted /test/b.txt 19/01/06 12:51:42 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 0 minutes, Emptier interval = 0 minutes. Deleted /test/h133.txt [root@h133 ~]# hadoop fs -ls /test [root@h133 ~]# hdfs dfs -cp /test/.snapshot/test-snap/* /test [root@h133 ~]# hadoop fs -ls /test Found 2 items -rw-r--r-- 3 root supergroup 29 2019-01-06 12:55 /test/a.txt -rw-r--r-- 3 root supergroup 21 2019-01-06 12:55 /test/b.txt
上面的操做中,咱們先刪除了/test文件夾內的3個文件,而後從快照文件中將快照文件又還原了回來。須要注意的是,咱們的rm操做並無將.snapshoot快照文件刪除。
六、刪除快照
[root@h133 ~]# hdfs dfs -deleteSnapshot /test test-snap [root@h133 ~]# hdfs dfs -cp /test/.snapshot/test-snap/* /test cp: `/test/.snapshot/test-snap/*': No such file or directory
七、關閉文件目錄的快照功能
[root@h133 ~]# hdfs lsSnapshottableDir drwxr-xr-x 0 root supergroup 0 2019-01-06 12:55 0 65536 /test [root@h133 ~]# hdfs dfsadmin -disallowSnapshot /test Disallowing snaphot on /test succeeded [root@h133 ~]# hdfs lsSnapshottableDir
hdfs dfsadmin -disallowSnapshot /test
關閉以後,別忘了使用命令hdfs lsSnapshottableDir
查看一下操做的目錄是否還在。
HDFS也有回收站機制,只不過默認狀況下是關閉的。
與其相關的配置項主要有如下三個,他們能夠從core-site.xml中查詢到
<property> <name>fs.trash.interval</name> <value>0</value> <description>Number of minutes after which the checkpoint gets deleted. If zero, the trash feature is disabled. This option may be configured both on the server and the client. If trash is disabled server side then the client side configuration is checked. If trash is enabled on the server side then the value configured on the server is used and the client configuration value is ignored. </description> </property> <property> <name>fs.trash.checkpoint.interval</name> <value>0</value> <description>Number of minutes between trash checkpoints. Should be smaller or equal to fs.trash.interval. If zero, the value is set to the value of fs.trash.interval. Every time the checkpointer runs it creates a new checkpoint out of current and removes checkpoints created more than fs.trash.interval minutes ago. </description> </property> <!-- Static Web User Filter properties. --> <property> <description> The user name to filter as, on static web filters while rendering content. An example use is the HDFS web UI (user to be used for browsing files). </description> <name>hadoop.http.staticuser.user</name> <value>dr.who</value> </property>
從描述文檔裏面咱們能夠了解到,fs.trash.interval的默認值爲0,0表示禁用回收站,能夠設置刪除文件的存活時間。 注意配置項fs.trash.checkpoint.interval=0,他用於配置檢查回收站的間隔時間的值。顯然,咱們必須保證fs.trash.checkpoint.interval<=fs.trash.interval。
這裏咱們不配置他,則其值會等於咱們配置的文件存活時間(fs.trash.interval)。
若是檢查點已經啓用,會按期使用時間戳重命名Current目錄。.Trash中的文件在用戶可配置的時間延遲後被永久刪除。回收站中的文件和目錄能夠簡單地經過將它們移動到.Trash目錄以外的位置來恢復。
第三個配置項爲hadoop.http.staticuser.user,咱們修改他的值爲root(正常狀況下應該是本身的hadoop擁有者帳戶)。
接下來咱們來測試一下回收站功能。
一、啓用回收站
往core-site.xml文件中加入以下配置,設置文件的有效時間爲1分鐘,WEB瀏覽者權限用戶名爲root.
<property> <name>fs.trash.interval</name> <value>1</value> </property> <property> <name>hadoop.http.staticuser.user</name> <value>root</value> </property>
hadoop.http.staticuser.user請配置爲您當前系統的HDFS文件擁有者。
二、刪除文件
[root@h133 ~]# hadoop fs -rm /test/a.txt 19/01/06 13:32:27 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 1 minutes, Emptier interval = 0 minutes. Moved: 'hdfs://h133:8020/test/a.txt' to trash at: hdfs://h133:8020/user/root/.Trash/Current
三、查看回收站
drwx------ - root supergroup 0 2019-01-06 13:32 /user/root [root@h133 ~]# hadoop fs -ls /user/root Found 1 items drwx------ - root supergroup 0 2019-01-06 13:32 /user/root/.Trash
四、若是經過WEB訪問是沒辦法進入到此目錄的,咱們重啓一下集羣
[root@h134 current]# stop-yarn.sh [root@h133 ~]# stop-dfs.sh [root@h133 ~]# start-dfs.sh [root@h134 current]# start-yarn.sh
這時候就能夠經過web端進行訪問了。
五、恢復回收站數據 經過mv命令能夠移動回收站文件到正常的目錄,但須要注意的是,文件會在刪除後一分鐘從回收站中完全刪除。
[root@h133 ~]# hadoop fs -rm /test/a.txt 19/01/06 13:45:56 INFO fs.TrashPolicyDefault: Namenode trash configuration: Deletion interval = 1 minutes, Emptier interval = 0 minutes. Moved: 'hdfs://h133:8020/test/a.txt' to trash at: hdfs://h133:8020/user/root/.Trash/Current [root@h133 ~]# hadoop fs -mv /user/root/.Trash/Current/a.txt /test [root@h133 ~]# hadoop fs -mv /user/root/.Trash/190106134600/test/a.txt /test [root@h133 ~]# hadoop fs -ls /test Found 1 items -rw-r--r-- 3 root supergroup 29 2019-01-06 13:45 /test/a.txt
六、清空回收站
hdfs dfs -expunge
經過程序刪除的文件不會通過回收站,須要調用moveToTrash()才進入回收站
Trash trash = New Trash(conf); trash.moveToTrash(path);
回收站功能默認是禁用的。對於生產環境,建議啓用回收站功能以免意外的刪除操做。啓用回收站提供了從用戶操做刪除或用戶意外刪除中恢復數據的機會。可是爲fs.trash.interval和fs.trash.checkpoint.interval設置合適的值也是很是重要的,以使垃圾回收以你指望的方式運做。例如,若是你須要常常從HDFS上傳和刪除文件,則可能須要將fs.trash.interval設置爲較小的值,不然檢查點將佔用太多空間。
當啓用垃圾回收並刪除一些文件時,HDFS容量不會增長,由於文件並未真正刪除。HDFS不會回收空間,除非文件從回收站中刪除,只有在檢查點過時後纔會發生。
回收站功能默認只適用於使用Hadoop shell刪除的文件和目錄。使用其餘接口(例如WebHDFS或Java API)以編程的方式刪除的文件或目錄不會移動到回收站,即便已啓用回收站,除非程序已經實現了對回收站功能的調用。
有時你可能想要在刪除文件時臨時禁用回收站,也就是刪除的文件或目錄不用放在回收站而直接刪除,在這種狀況下,可使用-skipTrash選項運行rm命令。
至此,關於HDFS的學習就完成了,接下來咱們學習最有趣也是最核心的功能模塊:MapReduce——這關係到咱們的應用程序開發。