【大数据开发运维解决方案】hadoop fs常用命令案例解释

文章目录

  • 前言
  • 一、hadoop fs常用命令参数列表
  • 二、案例展示
    • hadoop fs -mkdir
    • hadoop fs -ls
    • hadoop fs -put
    • hadoop fs -get
    • hadoop fs -get [-ignorecrc] [-crc]
    • hadoop fs -cat
    • hadoop fs -cp
    • hadoop fs -mv
    • hadoop fs -rm(-rm -r)
    • hadoop fs -rm -r -skipTrash
    • hadoop fs -expunge
    • hadoop fs -tail
    • hadoop fs -du -h
    • hadoop fs -du -s
    • hadoop fs -count
    • hadoop fs -df
    • hadoop fs -getmerge
    • hadoop fs -chgrp
    • hadoop fs -chmod
    • hadoop fs -chown
    • hadoop fs -copyFromLocal
    • hadoop fs -copyToLocal
  • 总结


前言

本文主要介绍一下 hadoop fs 常用得命令参数选项,用案例做一下测试,给自己和大家一个例子介绍!


一、hadoop fs常用命令参数列表

我们可以通过hadoop fs看一下目前有哪些命令可以使用:

[root@s133062 ~]# hadoop fs
Usage: hadoop fs [generic options][-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] [-l] [-d] [-t <thread count>] <localsrc> ... <dst>][-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>][-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] [-e] <path> ...][-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>][-createSnapshot <snapshotDir> [<snapshotName>]][-deleteSnapshot <snapshotDir> <snapshotName>][-df [-h] [<path> ...]][-du [-s] [-h] [-v] [-x] <path> ...][-expunge][-find <path> ... <expression> ...][-get [-f] [-p] [-ignoreCrc] [-crc] <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 <localsrc> ... <dst>][-moveToLocal <src> <localdst>][-mv <src> ... <dst>][-put [-f] [-p] [-l] [-d] <localsrc> ... <dst>][-renameSnapshot <snapshotDir> <oldName> <newName>][-rm [-f] [-r|-R] [-skipTrash] [-safely] <src> ...][-rmdir [--ignore-fail-on-non-empty]  ...][-setfacl [-R] [{-b|-k} {-m|-x <acl_spec>} <path>]|[--set  ]][-setfattr {-n name [-v value] | -x name} <path>][-setrep [-R] [-w] <rep> <path> ...][-stat [format] <path> ...][-tail [-f] <file>][-test -[defsz] <path>][-text [-ignoreCrc] <src> ...][-touchz <path> ...][-trace <fs commands>][-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 machinesThe general command line syntax is:
command [genericOptions] [commandOptions]

二、案例展示

hadoop fs -mkdir

功能:创建目录
案例:

[root@s133062 ~]# hadoop fs -mkdir /user/bip_txt/zyd_fd/test1
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/
Found 3 items
-rwxrwxrwx   2 bip_txt hadoop          0 2021-01-12 10:32 /user/bip_txt/zyd_fd/_SUCCESS
-rwxrwxrwx   2 bip_txt hadoop        232 2021-01-12 10:32 /user/bip_txt/zyd_fd/import_part_1604454823400_3689_0000000
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 10:27 /user/bip_txt/zyd_fd/test1
[root@s133062 ~]#

创建多个目录:

[root@s133062 ~]# hadoop fs -mkdir /user/bip_txt/zyd_fd/test1/ttest1 /user/bip_txt/zyd_fd/test1/ttest2
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1
Found 2 items
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 10:28 /user/bip_txt/zyd_fd/test1/ttest1
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 10:28 /user/bip_txt/zyd_fd/test1/ttest2
[root@s133062 ~]#

hadoop fs -ls

功能:查看目录
案例:

[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1
Found 2 items
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 10:28 /user/bip_txt/zyd_fd/test1/ttest1
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 10:28 /user/bip_txt/zyd_fd/test1/ttest2
[root@s133062 ~]#

注意:这个命令会打印出来目录的统计信息和详细信息,如文件如果要做批处理的话需要注意一下用正则处理

hadoop fs -put

功能:从本地文件系统上传到hdfs文件系统
案例:

[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1
Found 1 items
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 11:04 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]#

hadoop fs -get

功能:从hdfs文件系统下载文件到本地文件系统
案例:

[root@s133062 ~]# ls -ltrh
总用量 1.8M
-rw-------. 1 root root 7.5K 7月  10 2019 anaconda-ks.cfg
drwxr-xr-x  2 root root    6 710 2019 Videos
drwxr-xr-x  2 root root    6 710 2019 Templates
drwxr-xr-x  2 root root    6 710 2019 Public
drwxr-xr-x  2 root root    6 710 2019 Pictures
drwxr-xr-x  2 root root    6 710 2019 Music
drwxr-xr-x  2 root root    6 710 2019 Downloads
drwxr-xr-x  2 root root    6 710 2019 Documents
drwxr-xr-x  2 root root    6 710 2019 Desktop
drwxr-xr-x  4 root root   29 711 2019 hbase
-rwxr-xr-x  1 root root  847 1014 2020 oozie_shell.sh
-rwxr-xr-x  1 root root   37 1014 2020 zyd.sh
-rwxr-xr-x  1 root root  563 310 2021 test.sh
-rwxr-xr-x  1 root root 1.7M 319 2021 es_curl.sh
-rw-r--r--  1 root root  223 10月 21 16:56 000126_0
-rw-r--r--  1 root root   22 10月 28 11:28 aa
-rwxr-xr-x  1 root root  310 210 09:25 aa.sh
-rw-------  1 root root  64K 2月  27 11:06 dead.letter
-rw-r--r--  1 root root    0 3月   3 11:05 null
[root@s133062 ~]# hadoop fs -get  /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
get: `aa.sh': File exists
[root@s133062 ~]# rm -rf aa.sh
[root@s133062 ~]# hadoop fs -get  /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]#

可以看到提示,如果本地有同名文件会报错。

hadoop fs -get [-ignorecrc] [-crc]

功能:从hdfs文件系统下载文件到本地文件系统,可以使用-ignorecrc选项复制下载crc校验失败的文件,使用-crc选项复制文件及crc信息
案例:

[root@s133062 pwd]# hadoop fs -get -crc /user/bip_txt/zyd_fd/test1/ttest1/aa.sh .
[root@s133062 pwd]# ls
aa.sh

hadoop fs -cat

功能:查看hdfs文件系统文件
案例:

[root@s133062 ~]# hadoop fs -cat /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
#!/bin/bash
JAVA_OPTIONS="$JAVA_OPTIONS -Dweblogic.oif.serialFilterScope=weblogic  -Dfile.encoding=GBK -Duser.region=CN -Duser.language=zh -Djava.awt.headless=true -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError"
JAVA_OPTIONS="${JAVA_OPTIONS} -XX:+HeapDumpOnOutOfMemoryError"
echo $JAVA_OPTIONS

hadoop fs -cp

功能:在hdfs文件系统中复制文件,复制后的还是在hdfs
案例:

[root@s133062 ~]# hadoop fs -cp /user/bip_txt/zyd_fd/test1/ttest1/aa.sh /user/bip_txt/zyd_fd/test1/ttest1/bb.sh
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1
Found 2 items
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 11:04 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 11:08 /user/bip_txt/zyd_fd/test1/ttest1/bb.sh
[root@s133062 ~]#

hadoop fs -mv

功能:在hdfs文件系统中移动文件
案例:

[root@s133062 ~]# hadoop fs -mv /user/bip_txt/zyd_fd/test1/ttest1/aa.sh /user/bip_txt/zyd_fd/test1/
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/
Found 3 items
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 11:04 /user/bip_txt/zyd_fd/test1/aa.sh
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 11:09 /user/bip_txt/zyd_fd/test1/ttest1
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 10:28 /user/bip_txt/zyd_fd/test1/ttest2
[root@s133062 ~]#

hadoop fs -rm(-rm -r)

功能:在hdfs文件系统中删除文件,-rm删除前提示,-rm -r强制删除不提示
案例:

[root@s133062 ~]# hadoop fs -rm -r /user/bip_txt/zyd_fd/test1/aa.sh
2023-03-03 11:13:25,849 INFO fs.TrashPolicyDefault: Moved: 'hdfs://hacluster/user/bip_txt/zyd_fd/test1/aa.sh' to trash at: hdfs://hacluster/user/bip_txt/.Trash/Current/user/bip_txt/zyd_fd/test1/aa.sh
[root@s133062 ~]#
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/
Found 2 items
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 11:09 /user/bip_txt/zyd_fd/test1/ttest1
drwxr-xr-x   - bip_txt hadoop          0 2023-03-03 10:28 /user/bip_txt/zyd_fd/test1/ttest2

如果开启了回收站功能,被删除的文件会自动放到回收站内,可以通过 -skipTrash来跳过回收站

hadoop fs -rm -r -skipTrash

功能:在hdfs文件系统中删除文件,强制删除不提示并且跳过回收站
案例:

[root@s133062 ~]# hadoop fs -rm -r -skipTrash /user/bip_txt/zyd_fd/test1/ttest1/zyd.sh
Deleted /user/bip_txt/zyd_fd/test1/ttest1/zyd.sh
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1/
Found 2 items
-rwxrwxrwx   3 bip_txt hadoop        310 2023-03-03 12:23 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 11:08 /user/bip_txt/zyd_fd/test1/ttest1/bb.sh
[root@s133062 ~]#

hadoop fs -expunge

功能:清空hdfs文件系统的回收站
案例:这个我就不演示了。。。。。

hadoop fs -tail

功能:查看hdfs文件系统中文件尾部
案例:

[root@s133062 ~]# hadoop fs -tail /user/bip_txt/zyd_fd/test1/ttest1/bb.sh
#!/bin/bash
JAVA_OPTIONS="$JAVA_OPTIONS -Dweblogic.oif.serialFilterScope=weblogic  -Dfile.encoding=GBK -Duser.region=CN -Duser.language=zh -Djava.awt.headless=true -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError"
JAVA_OPTIONS="${JAVA_OPTIONS} -XX:+HeapDumpOnOutOfMemoryError"
echo $JAVA_OPTIONS

hadoop fs -du -h

功能:以友好的方式显示hdfs文件系统中文件目录的大小
案例:

[root@s133062 ~]# hadoop fs -du -h  /user/bip_txt/
410      1.2 K    /user/bip_txt/.Trash
0        0        /user/bip_txt/.sparkStaging
205.6 G  538.0 G  /user/bip_txt/data
473.6 K  947.3 K  /user/bip_txt/examples
168      504      /user/bip_txt/hive-query.hql
1.3 G    2.7 G    /user/bip_txt/user
0        0        /user/bip_txt/zhaoyd
542      1.4 K    /user/bip_txt/zyd_fd
[root@s133062 ~]#

hadoop fs -du -s

功能:显示hdfs文件系统中文件的大小
案例:

[root@s133062 ~]# hadoop fs -du -s /user/bip_txt/data
220763351221  577726108162  /user/bip_txt/data

hadoop fs -count

功能:统计hdfs文件系统中目录文件的个数
案例:

[root@s133062 ~]# hadoop fs -count /user/bip_txt/data538         1631       220763351221 /user/bip_txt/data

hadoop fs -df

功能:统计hdfs文件系统指定目录详细信息
案例:

[root@s133062 ~]# hadoop fs -df /user/bip_txt/
Filesystem                  Size            Used       Available  Use%
hdfs://hacluster  81478020022272  71154551288129  10321069467208   87%

hadoop fs -getmerge

功能:将hdfs文件系统指定目录内文件合并排序问一个文件输出到本地文件系统
案例:

[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1
Found 1 items
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 11:08 /user/bip_txt/zyd_fd/test1/ttest1/bb.sh
[root@s133062 ~]# hadoop fs -cp /user/bip_txt/zyd_fd/test1/ttest1/bb.sh /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1/
Found 2 items
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 12:23 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 11:08 /user/bip_txt/zyd_fd/test1/ttest1/bb.sh
[root@s133062 ~]# hadoop fs -getmerge /user/bip_txt/zyd_fd/test1/ttest1/ /hadoop/zydtest
[root@s133062 ~]# cat /hadoop/z
zookeeper/ zydstudy/  zydtest    zzq/
[root@s133062 ~]# cat /hadoop/zydtest
#!/bin/bash
JAVA_OPTIONS="$JAVA_OPTIONS -Dweblogic.oif.serialFilterScope=weblogic  -Dfile.encoding=GBK -Duser.region=CN -Duser.language=zh -Djava.awt.headless=true -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError"
JAVA_OPTIONS="${JAVA_OPTIONS} -XX:+HeapDumpOnOutOfMemoryError"
echo $JAVA_OPTIONS
#!/bin/bash
JAVA_OPTIONS="$JAVA_OPTIONS -Dweblogic.oif.serialFilterScope=weblogic  -Dfile.encoding=GBK -Duser.region=CN -Duser.language=zh -Djava.awt.headless=true -XX:-OmitStackTraceInFastThrow -XX:+HeapDumpOnOutOfMemoryError"
JAVA_OPTIONS="${JAVA_OPTIONS} -XX:+HeapDumpOnOutOfMemoryError"
echo $JAVA_OPTIONS
[root@s133062 ~]#

hadoop fs -chgrp

功能:修改hdfs文件系统指定目录文件所属组
案例:

[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 12:23 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]# hadoop fs -chgrp bip_txt /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rw-r--r--   3 bip_txt bip_txt        310 2023-03-03 12:23 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]#

hadoop fs -chmod

功能:修改hdfs文件系统指定目录文件权限
案例:

[root@s133062 ~]# hadoop fs -chmod 777 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rwxrwxrwx   3 bip_txt bip_txt        310 2023-03-03 12:23 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]#

hadoop fs -chown

功能:修改hdfs文件系统指定目录文件的属主和属组
案例:

[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rwxrwxrwx   3 bip_txt bip_txt        310 2023-03-03 12:23 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]# hadoop fs -chown bip_txt:hadoop /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rwxrwxrwx   3 bip_txt hadoop        310 2023-03-03 12:23 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh

hadoop fs -copyFromLocal

功能:限定源文件是从本地文件系统,然后上传文件到指定目录,类似 hadoop fs -put
案例:

[root@s133062 ~]# hadoop fs -copyFromLocal zyd.sh /user/bip_txt/zyd_fd/test1/ttest1/
[root@s133062 ~]# hadoop fs -ls /user/bip_txt/zyd_fd/test1/ttest1/
Found 3 items
-rwxrwxrwx   3 bip_txt hadoop        310 2023-03-03 12:23 /user/bip_txt/zyd_fd/test1/ttest1/aa.sh
-rw-r--r--   3 bip_txt hadoop        310 2023-03-03 11:08 /user/bip_txt/zyd_fd/test1/ttest1/bb.sh
-rw-r--r--   3 bip_txt hadoop         37 2023-03-03 14:17 /user/bip_txt/zyd_fd/test1/ttest1/zyd.sh
[root@s133062 ~]#

hadoop fs -copyToLocal

功能:限定目标路径是本地文件系统,然后从hdfs文件系统下载文件到本地,类似 hadoop fs -get
案例:

[root@s133062 ~]# hadoop fs -copyToLocal /user/bip_txt/zyd_fd/test1/ttest1/zyd.sh /hadoop/
[root@s133062 ~]# ll /hadoop/|grep zyd
-rw-r--r--  1 root  root          37 3月   3 14:25 zyd.sh

总结

好了以上就是常用hadoop fs命令测试介绍!


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部