HDFS的常见Shell操作
直接在命令行中输入hdfs dfs,可以查看dfs后面可以跟的所有参数
注意:这里面的[]表示是可选项,<>表示是必填项
[root@bigdata01 hadoop-3.3.2]# hdfs dfs
Usage: hadoop fs [generic options]
[-appendToFile <localsrc> ... <dst>]
[-cat [-ignoreCrc] <src> ...]
[-checksum [-v] <src> ...]
[-chgrp [-R] GROUP PATH...]
[-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH...]
[-chown [-R] [OWNER][:[GROUP]] PATH...]
[-concat <target path> <src path> <src path> ...]
[-copyFromLocal [-f] [-p] [-l] [-d] [-t <thread count>] [-q <thread pool queue size>] <localsrc> ... <dst>]
[-copyToLocal [-f] [-p] [-crc] [-ignoreCrc] [-t <thread count>] [-q <thread pool queue size>] <src> ... <localdst>]
[-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] [-e] [-s] <path> ...]
[-cp [-f] [-p | -p[topax]] [-d] [-t <thread count>] [-q <thread pool queue size>] <src> ... <dst>]
[-createSnapshot <snapshotDir> [<snapshotName>]]
[-deleteSnapshot <snapshotDir> <snapshotName>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] [-v] [-x] <path> ...]
[-expunge [-immediate] [-fs <path>]]
[-find <path> ... <expression> ...]
[-get [-f] [-p] [-crc] [-ignoreCrc] [-t <thread count>] [-q <thread pool queue size>] <src> ... <localdst>]
[-getfacl [-R] <path>]
[-getfattr [-R] {-n name | -d} [-e en] <path>]
[-getmerge [-nl] [-skip-empty-file] <src> <localdst>]
[-head <file>]
[-help [cmd ...]]
[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [-e] [<path> ...]]
[-mkdir [-p] <path> ...]
[-moveFromLocal [-f] [-p] [-l] [-d] <localsrc> ... <dst>]
[-moveToLocal <src> <localdst>]
[-mv <src> ... <dst>]
[-put [-f] [-p] [-l] [-d] [-t <thread count>] [-q <thread pool queue size>] <localsrc> ... <dst>]
[-renameSnapshot <snapshotDir> <oldName> <newName>]
[-rm [-f] [-r|-R] [-skipTrash] [-safely] <src> ...]
[-rmdir [--ignore-fail-on-non-empty] <dir> ...]
[-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set <acl_spec> <path>]]
[-setfattr {-n name [-v value] | -x name} <path>]
[-setrep [-R] [-w] <rep> <path> ...]
[-stat [format] <path> ...]
[-tail [-f] [-s <sleep interval>] <file>]
[-test -[defswrz] <path>]
[-text [-ignoreCrc] <src> ...]
[-touch [-a] [-m] [-t TIMESTAMP (yyyyMMdd:HHmmss) ] [-c] <path> ...]
[-touchz <path> ...]
[-truncate [-w] <length> <path> ...]
[-usage [cmd ...]]
>Generic options supported are:
-conf <configuration file> specify an application configuration file
-D <property=value> define a value for a given property
-fs <file:///|hdfs://namenode:port> specify default filesystem URL to use, overrides 'fs.defaultFS' property from configurations.
-jt <local|resourcemanager:port> specify a ResourceManager
-files <file1,...> specify a comma-separated list of files to be copied to the map reduce cluster
-libjars <jar1,...> specify a comma-separated list of jar files to be included in the classpath
-archives <archive1,...> specify a comma-separated list of archives to be unarchived on the compute machines
>
>The general command line syntax is:
command [genericOptions] [commandOptions]
-ls:查询指定路径信息
查看hdfs根目录下的内容,默认情况下hdfs中什么都没有
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -ls hdfs://bigdata01:9000/
Found 23 items
-rw-r--r-- 2 root supergroup 15217 2022-04-04 16:19 hdfs://bigdata01:9000/LICENSE.txt
-rw-r--r-- 3 30329 supergroup 482 2022-04-19 16:47 hdfs://bigdata01:9000/MapFile
-rw-r--r-- 2 root supergroup 1541 2022-04-04 16:19 hdfs://bigdata01:9000/NOTICE.txt
-rw-r--r-- 3 30329 supergroup 482 2022-04-19 16:50 hdfs://bigdata01:9000/SeqFile
-rw-r--r-- 2 root supergroup 1860100000 2022-04-20 13:14 hdfs://bigdata01:9000/hello_10000000.dat
drwxr-xr-x - root supergroup 0 2022-04-10 12:14 hdfs://bigdata01:9000/log
drwxr-xr-x - 30329 supergroup 0 2022-04-19 16:50 hdfs://bigdata01:9000/mapFile
drwxr-xr-x - root supergroup 0 2022-04-12 17:37 hdfs://bigdata01:9000/out
drwxr-xr-x - root supergroup 0 2022-04-12 17:51 hdfs://bigdata01:9000/out1
drwxr-xr-x - root supergroup 0 2022-04-19 17:18 hdfs://bigdata01:9000/out10
drwxr-xr-x - root supergroup 0 2022-04-20 16:32 hdfs://bigdata01:9000/out10000000
drwxr-xr-x - root supergroup 0 2022-04-19 17:28 hdfs://bigdata01:9000/out11
drwxr-xr-x - root supergroup 0 2022-04-19 17:30 hdfs://bigdata01:9000/out12
drwxr-xr-x - root supergroup 0 2022-04-12 18:05 hdfs://bigdata01:9000/out2
drwxr-xr-x - root supergroup 0 2022-04-13 19:31 hdfs://bigdata01:9000/out4
drwxr-xr-x - root supergroup 0 2022-04-13 19:51 hdfs://bigdata01:9000/out5
drwxr-xr-x - root supergroup 0 2022-04-13 20:03 hdfs://bigdata01:9000/out6
drwxr-xr-x - root supergroup 0 2022-04-28 21:25 hdfs://bigdata01:9000/outqueue
-rw-r--r-- 2 root supergroup 161 2022-04-29 08:23 hdfs://bigdata01:9000/relation.dat
drwxr-xr-x - root supergroup 0 2022-04-12 17:30 hdfs://bigdata01:9000/test
drwx------ - root supergroup 0 2022-04-13 19:51 hdfs://bigdata01:9000/tmp
drwx------ - root supergroup 0 2022-04-10 11:04 hdfs://bigdata01:9000/user
-rw-r--r-- 3 30329 supergroup 13 2022-04-05 20:58 hdfs://bigdata01:9000/user1.txt
其实后面hdfs的url这一串内容在使用时默认是可以省略的,因为hdfs在执行的时候会根据HDOOP_HOME自动识别配置文件中的fs.defaultFS属性
所以这样简写也是可以的
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -ls /
Found 23 items
-rw-r--r-- 2 root supergroup 15217 2022-04-04 16:19 /LICENSE.txt
-rw-r--r-- 3 30329 supergroup 482 2022-04-19 16:47 /MapFile
-rw-r--r-- 2 root supergroup 1541 2022-04-04 16:19 /NOTICE.txt
-rw-r--r-- 3 30329 supergroup 482 2022-04-19 16:50 /SeqFile
-rw-r--r-- 2 root supergroup 1860100000 2022-04-20 13:14 /hello_10000000.dat
drwxr-xr-x - root supergroup 0 2022-04-10 12:14 /log
drwxr-xr-x - 30329 supergroup 0 2022-04-19 16:50 /mapFile
drwxr-xr-x - root supergroup 0 2022-04-12 17:37 /out
drwxr-xr-x - root supergroup 0 2022-04-12 17:51 /out1
drwxr-xr-x - root supergroup 0 2022-04-19 17:18 /out10
drwxr-xr-x - root supergroup 0 2022-04-20 16:32 /out10000000
drwxr-xr-x - root supergroup 0 2022-04-19 17:28 /out11
drwxr-xr-x - root supergroup 0 2022-04-19 17:30 /out12
drwxr-xr-x - root supergroup 0 2022-04-12 18:05 /out2
drwxr-xr-x - root supergroup 0 2022-04-13 19:31 /out4
drwxr-xr-x - root supergroup 0 2022-04-13 19:51 /out5
drwxr-xr-x - root supergroup 0 2022-04-13 20:03 /out6
drwxr-xr-x - root supergroup 0 2022-04-28 21:25 /outqueue
-rw-r--r-- 2 root supergroup 161 2022-04-29 08:23 /relation.dat
drwxr-xr-x - root supergroup 0 2022-04-12 17:30 /test
drwx------ - root supergroup 0 2022-04-13 19:51 /tmp
drwx------ - root supergroup 0 2022-04-10 11:04 /user
-rw-r--r-- 3 30329 supergroup 13 2022-04-05 20:58 /user1.txt
-put: 从本地上传文件
接下来我们向hdfs中上传一个文件,使用Hadoop中的README.txt,直接上传到hdfs的根目录即可
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -put README.txt /
上传成功之后没有任何提示,注意,没有提示就是最好的结果
确认一下刚才上传的文件
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -ls /
在这里可以发现使用hdfs中的ls查询出来的信息和在linux中执行ll查询出来的信息是类似的
在这里能看到这个文件就说明刚才的上传操作是成功的
-cat: 查看HDFS文件内容
文件上传上去以后,查看一下HDFS中文件的内容,使用cat即可
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -cat /README.txt
-get: 下载文件到本地
如果我们想把hdfs中的文件下载到本地linux文件系统中需要怎么做呢?使用get即可实现
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -get /README.txt
-mkdir[-p]: 创建文件夹
后期我们需要在hdfs中维护很多文件,所以就需要创建文件夹来进行分类管理了
下面我们来创建一个文件夹,hdfs中使用mkdir命令
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -mkdir /test
如果要递归创建多级目录,还需要再指定-p参数
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -mkdir -o /abc/xyz
想要递归显示所有目录的信息,可以在ls后面添加-R参数
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -ls -R /
-rm[-r]: 删除文件/文件夹
如果想要删除hdfs中的目录或者文件,可以使用rm
删除文件
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -rm /README.txt
删除目录,注意,删除目录需要指定-r参数
[root@bigdata01 hadoop-3.3.2]# hdfs dfs -rm -r /abc
版权归原作者 hhhecker 所有, 如有侵权,请联系我们删除。