1. HDFS Shell
1.1. 查看所有命令的用法
1 | $ hdfs dfs |
1.2. help 查看某一命令的具体用法
1 | hdfs dfs -help get # 查看get的用法 |
1.3. ls 目录浏览
用法:-ls [-d] [-h] [-R] [<path> ...]
| 选项 | 描述 |
|---|---|
| -d | 查看目录本身信息 |
| -h | 显示文件大小单位便于阅读(byte单位省略) |
| -R | 递归显示 |
1 | $ hdfs dfs -ls / # 列出目录 |
1.4. mkdir 创建目录
用法:-mkdir [-p] <path> ...
| 选项 | 描述 |
|---|---|
| -p | 递归创建 |
1 | hdfs dfs -mkdir /d1 |
1.5. moveFromLocal 从本地移动到HDFS
用法:-moveFromLocal <localsrc> ... <dst>
说明:与put类似,区别在于moveFromLocal会删除本地文件
1 | hdfs dfs -moveFromLocal 1.txt /d1 # 移动文件 |
1.6. cat 查看HDFS的文件
用法:-cat [-ignoreCrc] <src> ...
1 | hdfs dfs -cat /input/1.txt # 查看文件 |
1.7. appendToFile 追加文件到已存在文件末尾
用法:-appendToFile <localsrc> ... <dst>
说明:<localsrc>的值如果是 -,表示从stdin读取输入
1 | $ hdfs dfs -appendToFile 2.txt /1.txt # 2.txt追加到1.txt |
1.8. chgrp 修改所有组
用法:-chgrp [-R] GROUP PATH...
1 | hdfs dfs -chgrp group1 /1.txt |
1.9. chown 修改所有者和所有组
用法:-chown [-R] [OWNER][:[GROUP]] PATH...
1 | hdfs dfs -chown user1 /1.txt |
1.10. chmod 修改权限
用法:-chmod [-R] <MODE[,MODE]... | OCTALMODE> PATH..
1 | hdfs dfs -chmod a+x /1.txt |
1.11. put / copyFromLocal 上传本地文件到HDFS
用法:-copyFromLocal [-f] [-p] [-l] <localsrc> ... <dst>、-put [-f] [-p] [-l] <localsrc> ... <dst>
| 选项 | 描述 |
|---|---|
| -p | 保留源文件的access时间戳、modification时间戳、所有者、所有组、和文件权限 |
| -f | 如果目标文件已存在,则覆盖 |
| -l | 允许DataNode懒惰持久化到磁盘(lazily persist),强制设置副本因子为1。该选项会导致可用性降低,请谨慎使用 |
1 | hdfs dfs -put 1.txt /d1 # 上传1.txt |
1.12. get / copyToLocal 下载文件到本地
用法:-put [-f] [-p] [-l] <localsrc> ... <dst>,-copyToLocal [-p] [-ignoreCrc] [-crc] <src> ... <localdst>
| 选项 | 描述 |
|---|---|
| -p | 保留源文件的access时间戳、modification时间戳、所有者、所有组、和文件权限 |
1 | hdfs dfs -get /1.txt ~ # 下载1.txt |
1.13. cp 从HDFS一个路径拷贝到HDFS的另一个路径
用法:-cp [-f] [-p | -p[topax]] <src> ... <dst>
| 选项 | 描述 |
|---|---|
| -p | all of the source and target pathnames are in the /.reserved/raw hierarchy. raw namespace xattr preservation is determined solely by the presence (or absence) of the /.reserved/raw prefix and not by the -p option |
| -f | 如果目标文件已存在,则覆盖 |
1 | $ hdfs dfs -ls -R / |
1.14. getmerge 合并下载多个文件到本地
用法:-getmerge [-nl] <src> <localdst>
| 选项 | 描述 |
|---|---|
| -nl | 合并时,在每个文件之后添加一个空行 |
| -f | 如果目标文件已存在,则覆盖 |
1 | hdfs dfs -getmerge /*.txt merge.txt # 下载合并txt到本地,命名为merge.txt |
1.15. tail 查看文件末尾1KB数据
用法:-tail [-f] <file>
注意:是查看末尾1KB数据,而不是最后几行
| 选项 | 描述 |
|---|---|
| -f | 监视文件末尾 |
1 | hdfs dfs -tail /1.txt |
1.16. rm 删除文件
用法:-rm [-f] [-r|-R] [-skipTrash] <src> ...
| 选项 | 描述 |
|---|---|
| -f | 若目标文件不存在,不显示提示信息 |
| -r 或 -R | 递归删除 |
1 | hadoop fs -rm /1.txt # 删除1.txt |
1.17. rmdir 删除空目录
用法:-rmdir [--ignore-fail-on-non-empty] <dir>
1 | hdfs dfs -rmdir /d1 |
1.18. rmr 递归删除
用法:rmr等价于 rm -r
1.19. mv 从HDFS一个路径移动到HDFS的另一个路径
用法:-mv <src> ... <dst>
1 | hadoop fs -mv /d1 /d2 # 更名 |
1.20. du 统计目录各文件大小
用法:-du [-s] [-h] <path> ...
| 选项 | 描述 |
|---|---|
| -s | 统计目录总大小 |
| -h | 显示文件大小单位便于阅读(byte单位省略) |
1 | $ hdfs dfs -du / # 统计目录下各文件大小 |
1.21. setrep 修改文件的数据块副本数
用法:-setrep [-R] [-w] <rep> <path> ...
| 选项 | 描述 |
|---|---|
| -w | It requests that the command waits for the replication to complete. This can potentially take a very long time. |
| -R | It is accepted for backwards compatibility. It has no effect. |
1 | $ hdfs dfs -ls /1.txt |
1.22. touchz 创建空文件
用法:-touchz <path> ...
1 | hdfs dfs -touchz /file1 |