快速學習-HDFS的Shell操作

  • 2020 年 2 月 19 日
  • 筆記

第2章 HDFS的Shell操作(開發重點)

1.基本語法

bin/hadoop fs 具體命令 OR bin/hdfs dfs 具體命令 dfs是fs的實現類。

2.命令大全

[atguigu@hadoop102 hadoop-2.7.2]$ bin/hadoop fs    [-appendToFile <localsrc> ... <dst>]          [-cat [-ignoreCrc] <src> ...]          [-checksum <src> ...]          [-chgrp [-R] GROUP PATH...]          [-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]          [-chown [-R] [OWNER][:[GROUP]] PATH...]          [-copyFromLocal [-f] [-p] <localsrc> ... <dst>]          [-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]          [-count [-q] <path> ...]          [-cp [-f] [-p] <src> ... <dst>]          [-createSnapshot <snapshotDir> [<snapshotName>]]          [-deleteSnapshot <snapshotDir> <snapshotName>]          [-df [-h] [<path> ...]]          [-du [-s] [-h] <path> ...]          [-expunge]          [-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]          [-getfacl [-R] <path>]          [-getmerge [-nl] <src> <localdst>]          [-help [cmd ...]]          [-ls [-d] [-h] [-R] [<path> ...]]          [-mkdir [-p] <path> ...]          [-moveFromLocal <localsrc> ... <dst>]          [-moveToLocal <src> <localdst>]          [-mv <src> ... <dst>]          [-put [-f] [-p] <localsrc> ... <dst>]          [-renameSnapshot <snapshotDir> <oldName> <newName>]          [-rm [-f] [-r|-R] [-skipTrash] <src> ...]          [-rmdir [--ignore-fail-on-non-empty] <dir> ...]          [-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]          [-setrep [-R] [-w] <rep> <path> ...]          [-stat [format] <path> ...]          [-tail [-f] <file>]          [-test -[defsz] <path>]          [-text [-ignoreCrc] <src> ...]          [-touchz <path> ...]          [-usage [cmd ...]]

3.常用命令實操

(0)啟動Hadoop集群(方便後續的測試)

[atguigu@hadoop102 hadoop-2.7.2]$ sbin/start-dfs.sh  [atguigu@hadoop103 hadoop-2.7.2]$ sbin/start-yarn.sh

(1)-help:輸出這個命令參數

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -help rm

(2)-ls: 顯示目錄資訊

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -ls /

(3)-mkdir:在HDFS上創建目錄

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -mkdir -p /sanguo/shuguo

(4)-moveFromLocal:從本地剪切粘貼到HDFS

[atguigu@hadoop102 hadoop-2.7.2]$ touch kongming.txt  [atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs  -moveFromLocal  ./kongming.txt  /sanguo/shuguo

(5)-appendToFile:追加一個文件到已經存在的文件末尾

[atguigu@hadoop102 hadoop-2.7.2]$ touch liubei.txt  [atguigu@hadoop102 hadoop-2.7.2]$ vi liubei.txt  輸入  san gu mao lu  [atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -appendToFile liubei.txt /sanguo/shuguo/kongming.txt

(6)-cat:顯示文件內容

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -cat /sanguo/shuguo/kongming.txt

(7)-chgrp 、-chmod、-chown:Linux文件系統中的用法一樣,修改文件所屬許可權

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs  -chmod  666  /sanguo/shuguo/kongming.txt  [atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs  -chown  atguigu:atguigu   /sanguo/shuguo/kongming.txt

(8)-copyFromLocal:從本地文件系統中拷貝文件到HDFS路徑去

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -copyFromLocal README.txt /

(9)-copyToLocal:從HDFS拷貝到本地

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -copyToLocal /sanguo/shuguo/kongming.txt ./

(10)-cp :從HDFS的一個路徑拷貝到HDFS的另一個路徑

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -cp /sanguo/shuguo/kongming.txt /zhuge.txt

(11)-mv:在HDFS目錄中移動文件

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -mv /zhuge.txt /sanguo/shuguo/

(12)-get:等同於copyToLocal,就是從HDFS下載文件到本地

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -get /sanguo/shuguo/kongming.txt ./

(13)-getmerge:合併下載多個文件,比如HDFS的目錄 /user/atguigu/test下有多個文件:log.1, log.2,log.3,…

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -getmerge /user/atguigu/test/* ./zaiyiqi.txt

(14)-put:等同於copyFromLocal

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -put ./zaiyiqi.txt /user/atguigu/test/

(15)-tail:顯示一個文件的末尾

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -tail /sanguo/shuguo/kongming.txt

(16)-rm:刪除文件或文件夾

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -rm /user/atguigu/test/jinlian2.txt

(17)-rmdir:刪除空目錄

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -mkdir /test  [atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -rmdir /test

(18)-du統計文件夾的大小資訊

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -du -s -h /user/atguigu/test  2.7 K  /user/atguigu/test    [atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -du  -h /user/atguigu/test  1.3 K  /user/atguigu/test/README.txt  15     /user/atguigu/test/jinlian.txt  1.4 K  /user/atguigu/test/zaiyiqi.txt

(19)-setrep:設置HDFS中文件的副本數量

[atguigu@hadoop102 hadoop-2.7.2]$ hadoop fs -setrep 10 /sanguo/shuguo/kongming.txt

這裡設置的副本數只是記錄在NameNode的元數據中,是否真的會有這麼多副本,還得看DataNode的數量。因為目前只有3台設備,最多也就3個副本,只有節點數的增加到10台時,副本數才能達到10。