Basic operations of HDFS (create directories or files, view directories or files, upload and copy files to HDFS, append data to HDFS, download files from HDFS to Linux local, merge HDFS files)


Preface

Mainly involves HDFS shell operation commands, including creating directories or files, viewing directories or files, uploading and copying files to HDFS, appending data to HDFS, downloading files from HDFS to Linux locally, merging multiple files on HDFS and downloading, Delete files on HDFS, view the usage of various commands, etc.


1. HDFS related commands

1. Create a directory in HDFS

#hdfs dfs -mkdir /要创建的目录
hdfs dfs -mkdir /data
# -p表示创建多级目录,父目录和子目录一起
hdfs dfs -mkdir -p /usr/opt/data

2. View the current directory

#ls 目录名xxx:列出目录名下的可见文件
#查看data目录下的所有可见内容
hdfs dfs -ls /usr/opt/data
#查看hdfs根目录下的内容
hdfs dfs -ls /
#查看本地Linux系统根目录下的内容
hdfs dfs -ls file:/// #相当于 ls /
#查看hdfs上目录
hdfs dfs -ls hdfs://hadoop100:8020/
#显示文件size
hdfs dfs -ls -h /data

3. View directories and subdirectories

#查看根目录下的目录以及目录下的子目录
hdfs dfs -ls -R/

4. View the contents of the file

#-cat命令:将文件内容显示出来(适合小文件)
hdfs dfs -cat /usr/opt/data/a.txt
#-haed命令:查看文件前1KB的内容
hdfs dfs -head /usr/opt/data/a.txt
#-tail命令:查看文件最后1KB的内容
hdfs dfs -tail /usr/opt/data/a.txt
#-tail -f命令:可以动态显示文件中追加的内容
#在Linux系统中
tail -f 1.txt 
#hdfs
hdfs dfs -tail -f /usr/opt/data/a.txt

5. Create files

#本地创建文件 touch
touch a.txt b.txt c.txt
#创建不存在的文件,连续创建多个空文件,用空格隔开

6. Upload and copy files

#-put命令:从本地文件系统拷贝到HDFS,其中/xxx/xxx/为hdfs中的路径
hdfs dfs -put a.txt /usr/opt/data
#-copyFromLocal命令:从本地文件系统拷贝到HDFS,效果与-put命令等同
hdfs dfs -copyFromLocal b.txt /usr/opt/data
#-moveFromLocal命令:从本地文件系统剪切到HDFS,命令执行完后本地文件就没有了
hdfs dfs -moveFromLocal c.txt /usr/opt/data

7. Append data to HDFS file

#-appendToFile命令:将所有给定本地文件的内容追加到hdfs文件,hdf文件不存在,则创建该文件
hdfs dfs -appendToFile d.txt /usr/opt/data/a.txt

8. Download the file to the Linux local system

#-get命令:获取文件
#将hdfs上的a.txt文件,下载到本地Linux系统的root目录下
hdfs dfs -get /usr/opt/data/a.txt /root/

9. Merge multiple small files on HDFS and download them locally

#-getmerge命令:将hdfs中的文件合并到本地文件系统的单个文件
#将hdfs上data目录下的文件,合并,并下载到Linux系统的指定目录下
hdfs dfs -getmerge /usr/opt/data/ /opt/merge
#-getmerge -nl命令:在每个文件末尾添加换行符
#-getmerge -skip-empty-file命令:跳过空文件进行合并

10. Delete files in the specified directory on HDFS

#删除HDFS上usr/opt/data/目录下的a.txt文件
#-rm命令:删除文件,-r:目录/子目录下所有内容
hdfs dfs -rm -r /usr/opt/data/a.txt

11. Delete the specified directory on HDFS

#删除HDFS上/usr/opt/data目录的data目录
hdfs dfs -rm -r /usr/opt/data

12. View the detailed usage of the command

#-help命令:输出rm命令参数
hdfs dfs -help rm

13. Check HDFS disk space

#-df -h命令:显示文件系统的容量,可用空间和已用空间
hdfs dfs -df -h /

14. View the amount of space used by HDFS files

#-du -s命令:表示显示指定路径文件长度的汇总摘要,不是单个文件的摘要
hdfs dfs -du -s 
#-du -h命令:人性化的显示文件大小
hdfs dfs -du -s -h /usr/opt/data

15. HDFS data movement operation

#-mv命令:移动文件到指定文件夹下,并且重命名文件的名称
hdfs dfs -mv /usr/opt/data/a.txt /tmp/aaa.txt

16. Modify the number of HDFS file copies

#-setrep -R:修改文件夹下及其所有
#-setrep -w:客户端是否等待副本修改完毕
hdfs dfs -settrep 2 /usr/opt/data/a.txt

Guess you like

Origin blog.csdn.net/weixin_61472217/article/details/132812280