第2章 HFDS的Shell操作

2.1 基本语法

hadoop fs 具体命令

hdfs dfs 具体命令

2.2 命令大全

具体命令如下

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] <localsrc> ... <dst>]
	[-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-count [-q] [-h] [-v] [-x] <path> ...]
	[-cp [-f] [-p | -p[topax]] <src> ... <dst>]
	[-createSnapshot <snapshotDir> [<snapshotName>]]
	[-deleteSnapshot <snapshotDir> <snapshotName>]
	[-df [-h] [<path> ...]]
	[-du [-s] [-h] [-x] <path> ...]
	[-expunge]
	[-find <path> ... <expression> ...]
	[-get [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
	[-getfacl [-R] <path>]
	[-getfattr [-R] {-n name | -d} [-e en] <path>]
	[-getmerge [-nl] <src> <localdst>]
	[-help [cmd ...]]
	[-ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...]]
	[-mkdir [-p] <path> ...]
	[-moveFromLocal <localsrc> ... <dst>]
	[-moveToLocal <src> <localdst>]
	[-mv <src> ... <dst>]
	[-put [-f] [-p] [-l] <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>]]
	[-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> ...]
	[-usage [cmd ...]]

Generic options supported are
-conf <configuration file>     specify an application configuration file
-D <property=value>            use value for given property
-fs <local|namenode:port>      specify a namenode
-jt <local|resourcemanager:port>    specify a ResourceManager
-files <comma separated list of files>    specify comma separated files to be copied to the map reduce cluster
-libjars <comma separated list of jars>    specify comma separated jar files to include in the classpath.
-archives <comma separated list of archives>    specify comma separated archives to be unarchived on the compute machines.

The general command line syntax is
bin/hadoop command [genericOptions] [commandOptions]

2.3 常用命令

注意:如果提示root权限不能进行写操作,请切换到hdfs用户操作

2.3.1 -help

帮助命令

hadoop fs -help rm

hdfs dfs -help rm

注意:hadoop脚本、hdfs脚本,在hadoop安装目录的bin目录下

2.3.2 -ls

显示目录信息

hadoop fs -ls /

hdfs dfs –ls /

2.3.3 -mkdir

在hdfs上创建目录

hadoop fs -mkdir -p /teaching/hdfs

hdfs dfs -mkdir -p /teaching/hdfs

2.3.4 -moveFromLocal

从本地剪切后粘贴到hdfs

touch test.txt
hadoop fs  -moveFromLocal  ./test.txt  /teaching/hdfs

hdfs dfs –moveFromLocal ./test.txt /teaching/hdfs

注意:执行过程为先复制,后删除,如果hdfs用户不具有删除本地文件权限则复制到hdfs能成功,删除本地文件不能成功

2.3.5 -appendToFile

追加一个文件到已经存在的文件末尾

touch test2.txt
vi test2.txt
# 输入
hello hdfs
hadoop fs -appendToFile test2.txt /teaching/hdfs/test.txt

hdfds dfs -appendToFile test2.txt /teaching/hdfs/test.txt

2.3.6 -cat

查看文件内容

hadoop fs -cat /teaching/hdfs/test.txt

hdfs dfs –cat /teaching/hdfs/test.txt

2.3.7 -tail

显示文件最后1kb内容

hadoop fs -tail /teaching/hdfs/test.txt

hdfs dfs -tail /teaching/hdfs/test.txt

2.3.8 -chgrp、-chmod、-chown

修改文件所属组、权限、用户

  1. 修改HDFS存储的文件/teaching/hdfs/tt.txt文件所属组,默认组:supergroup选项-R递归执行
$ hdfs dfs -ls /teaching/hdfs
-rw-r--r--   3 hdfs supergroup         11 2019-08-12 14:40 /teaching/hdfs/tt.txt
$ hdfs dfs -chgrp lubin-group /teaching/hdfs/tt.txt
$ hdfs dfs -ls /teaching/hdfs
-rw-r--r--   3 hdfs lubin-group         11 2019-08-12 14:40 /teaching/hdfs/tt.txt
  1. 修改HDFS存储的文件/teaching/hdfs/tt.txt文件的权限
$ hdfs dfs -chmod 666 /teaching/hdfs/tt.txt
$ hdfs dfs -ls /teaching/hdfs
-rw-rw-rw-   3 hdfs lubin-group         11 2019-08-12 14:40 /teaching/hdfs/tt.txt
  1. 修改HDFS存储的文件/teaching/hdfs/tt.txt文件所属的用户和组,lubin1为用户,lubin2为组
$ hdfs dfs -chown lubin1:lubin2 /teaching/hdfs/tt.txt
$ hdfs dfs -ls /teaching/hdfs/tt.txt
-rw-rw-rw-   3 lubin1 lubin2         11 2019-08-12 14:40 /teaching/hdfs/tt.txt

2.3.9 -copyFromLocal

从本地文件系统拷贝文件到hdfs路径

hadoop fs -copyFromLocal README.txt /teaching/hdfs

hdfs dfs -copyFromLocal test3.txt /teaching/hdfs

2.3.10 -copyToLocal

从hdfs拷贝到本地

hadoop fs -copyToLocal /teaching/hdfs/test.txt /tmp

hdfs dfs –copyToLocal /teaching/hdfs/test.txt /tmp

2.3.11 -cp

从hdfs的一个路径拷贝到hdfs的另一个路径

hadoop fs -cp /teaching/hdfs/test3.txt /teaching/hdfs2

hdfs dfs -cp /teaching/hdfs/test3.txt /teaching/hdfs2

2.3.12 -mv

在hdfs目录中移动文件

hadoop fs -mv /teaching/hdfs/tt.txt /teaching/hdfs2

hdfs dfs -mv /teaching/hdfs/tt.txt /teaching/hdfs2

2.3.13 -get

等同于copyToLocal,从hdfs下载文件到本地

hadoop fs -get /teaching/hdfs/test3.txt /tmp

hdfs dfs -get /teaching/hdfs/test3.txt /tmp

2.3.14 -getmerge

合并下载多个文件(多个文件的内容合并到一个文件中),比如hdfs的目录/aaa/下有多个文件:log.1, log.2,log.3,…

hadoop fs -getmerge  /teaching/hdfs/log*.txt /tmp/log.txt

hdfs dfs -getmerge /teaching/hdfs/log*.txt /tmp/log.txt

2.3.15 -put

等同于copyFromLocal

hadoop fs -put log1.txt /teaching/hdfs

hdfs dfs –put log1.txt /teaching/hdfs

2.3.16 -rm

删除文件或文件夹

hadoop fs -rm /teaching/hdfs2/log1.txt

hdfs dfs –rm /teaching/hdfs2/log2.txt

注意:如果用户没有删除的权限,可以使用如下方式删除:su hdfs -c “hadoop fs -rm /test/test.txt”

2.3.17 -rmdir

删除空目录

hadoop fs -rmdir /test

hdfs dfs –rmdir /test

注意:上述命令只能删除空目录,删除非空目录用rm命令

2.3.18 -du

统计文件夹的大小信息

hadoop fs -du -s -h /teaching/hdfs

hdfs dfs -du -s -h /teaching/hdfs
58  174  /teaching/hdfs

注意:-s显示总(摘要)大小,去掉-s则显示每个匹配的文件大小;
-h以人类可读的方式格式化大小,不是用字节去展示;
第一列显示目录下总文件大小,第二列标示该目录下所有文件在集群上的总存储大小(与副本数有关),第三列是查询的目录名称

2.3.19 -setrep

设置hdfs中文件的副本数量

hadoop fs -setrep 10 /teaching/hdfs/test.txt

hdfs dfs –setrep 2 /teaching/hdfs /test.txt

在这里插入图片描述

设置的副本数只是记录在NameNode的元数据中,是否真的会有这么多副本,还得看DataNode的数量。因为目前只有3台设备,最多也就3个副本,如果设置为10,只有节点数的增加到10台时,副本数才能达到10。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值