大数据笔记 | HDFS 常用操作命令
目錄
一、HDFS 命令前綴
二、ls 命令
三、put 命令
四、moveFromLocal 命令
五、get 命令
六、rm 命令
七、mkdir 命令
八、cp 命令
九、mv 命令
十、cat 命令
十一、appendToFile 命令
十二、總結(jié)
? ? ? ? HDFS 是 Hadoop Distributed File System 的簡寫,即 Hadoop 分布式文件系統(tǒng)。它是 Hadoop 項目的核心子項目,它為大數(shù)據(jù)分布式計算提供了海量數(shù)據(jù)的存儲與管理。
? ? ? ? 既然 HDFS 是文件系統(tǒng),那么它必然有一套對文件管理的命令,這里介紹一下 HDFS 常用的文件管理命令。
一、HDFS 命令前綴
? ? ? ? 所有操作 HDFS 的命令都需要前綴,它的前綴有兩種,分別是 hadoop fs 或 hdfs dfs 兩種。可以通過 hadoop fs -help 或 hdfs dfs -help 來查看其幫助文件。比如:
$ hadoop fs -help ls -ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...] :List the contents that match the specified file pattern. If path is notspecified, the contents of /user/<currentUser> will be listed. For a directory alist of its direct children is returned (unless -d option is specified).Directory entries are of the form:permissions - userId groupId sizeOfDirectory(in bytes)modificationDate(yyyy-MM-dd HH:mm) directoryNameand file entries are of the form:permissions numberOfReplicas userId groupId sizeOfFile(in bytes)modificationDate(yyyy-MM-dd HH:mm) fileName-C Display the paths of files and directories only.-d Directories are listed as plain files.-h Formats the sizes of files in a human-readable fashionrather than a number of bytes.-q Print ? instead of non-printable characters.-R Recursively list the contents of directories.-t Sort files by modification time (most recent first).-S Sort files by size.-r Reverse the order of the sort.-u Use time of last access instead of modification fordisplay and sorting.? ? ? ? 或者使用 hdfs dfs 來查看幫助,命令如下:
$ hdfs dfs -help ls -ls [-C] [-d] [-h] [-q] [-R] [-t] [-S] [-r] [-u] [<path> ...] :List the contents that match the specified file pattern. If path is notspecified, the contents of /user/<currentUser> will be listed. For a directory alist of its direct children is returned (unless -d option is specified).Directory entries are of the form:permissions - userId groupId sizeOfDirectory(in bytes)modificationDate(yyyy-MM-dd HH:mm) directoryNameand file entries are of the form:permissions numberOfReplicas userId groupId sizeOfFile(in bytes)modificationDate(yyyy-MM-dd HH:mm) fileName-C Display the paths of files and directories only.-d Directories are listed as plain files.-h Formats the sizes of files in a human-readable fashionrather than a number of bytes.-q Print ? instead of non-printable characters.-R Recursively list the contents of directories.-t Sort files by modification time (most recent first).-S Sort files by size.-r Reverse the order of the sort.-u Use time of last access instead of modification fordisplay and sorting.二、ls 命令
? ? ? ? ls 命令用來查看 HDFS 系統(tǒng)中的目錄和文件,命令如下:
$ hadoop fs -ls /? ? ? ? 也可以通過給 ls 添加 -R 參數(shù)來遞歸列出要查看目錄下的所有目錄和文件,命令如下:
$ hadoop fs -ls -R /?? ? ? ? 由于目前在 HDFS 中并沒有任何文件和目錄,因此這里沒有顯示任何的結(jié)果。
三、put 命令
? ? ? ? put 命令用于將本地文件上傳到 HDFS 系統(tǒng)中,命令如下:
$ hadoop fs -put test.txt / $ hadoop fs -ls / Found 1 items -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:22 /test.txt? ? ? ? 通過?-put 命令將本地當前目錄下的 test.txt 文件上傳到了 HDFS 的 / 目錄下,通過?-ls 命令可以看到文件已經(jīng)上傳到 HDFS 系統(tǒng)中了。
四、moveFromLocal 命令
? ? ? ? 將本地文件移動到 HDFS 文件系統(tǒng)中,并將本地的文件進行刪除,命令如下:
$ ll 總用量 84804 -rw-rw-r--. 1 hadoop hadoop 5 11月 7 13:27 abc.txt $ hadoop fs -moveFromLocal abc.txt / $ hadoop fs -ls / Found 2 items -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:27 /abc.txt -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:22 /test.txt? ? ? ??將本地的 abc.txt 文件上傳到 HDFS 的 / 目錄下,通過 -ls 命令查看?/ 目錄下已經(jīng)有了 abc.txt 文件,再來查看本地文件,本地的 abc.txt 文件已經(jīng)被移除。
五、get 命令
? ? ? ? get 命令用來將 HDFS 文件系統(tǒng)中的文件下載到本地,下載時的文件名不能與本地文件相同,否則會提示文件已存在。命令如下:
$ hadoop fs -get /abc.txt /home/hadoop/ $ ll 總用量 84804 -rw-r--r--. 1 hadoop hadoop 5 11月 7 13:42 abc.txt? ? ? ? 下載文件時確保文件不重名,否則提示文件已存在,命令如下:
$ hadoop fs -get / /home/hadoop/ get: `/home/hadoop/abc.txt': File exists get: `/home/hadoop/test.txt': File exists六、rm 命令
? ? ? ? rm 命令用來刪除 HDFS 系統(tǒng)中的文件或文件夾,每次可以刪除多個文件或目錄,命令如下:
$ hadoop fs -rm /test.txt Deleted /test.txt $ hadoop fs -ls / Found 1 items -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:27 /abc.txt七、mkdir 命令
? ? ? ? mkdir 命令用來在 HDFS 系統(tǒng)中創(chuàng)建目錄,可以使用 -p 參數(shù)創(chuàng)建多級目錄,即當父目錄不存在時,則自動創(chuàng)建,若不使用 -p 參數(shù),當父目錄不存在時則會提示文件或目錄不存在。命令如下:
$ hadoop fs -mkdir /test $ hadoop fs -mkdir /abc/abc mkdir: `/abc/abc': No such file or directory $ hadoop fs -mkdir -p /abc/abc八、cp 命令
? ? ? ? cp 命令在 HDFS 文件系統(tǒng)中用于文件的復制,命令如下:
$ hadoop fs -ls / Found 3 items drwxr-xr-x - hadoop supergroup 0 2021-11-07 13:47 /abc -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:27 /abc.txt drwxr-xr-x - hadoop supergroup 0 2021-11-07 13:47 /test $ hadoop fs -cp /abc.txt /abc/ $ hadoop fs -ls -R / drwxr-xr-x - hadoop supergroup 0 2021-11-07 13:49 /abc drwxr-xr-x - hadoop supergroup 0 2021-11-07 13:47 /abc/abc -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:49 /abc/abc.txt -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:27 /abc.txt drwxr-xr-x - hadoop supergroup 0 2021-11-07 13:47 /test? ? ? ? 通過 cp 命令將 /abc.txt 文件復制到了 /abc/ 目錄下,然后使用 ls -R 來遞歸查看目錄。
九、mv 命令
? ? ? ? mv 命令用來在 HDFS 文件系統(tǒng)下完成移動的功能,也可以用來進行重命名。命令如下:
hadoop fs -mv /abc/abc.txt /test/ [hadoop@centos01 ~]$ hadoop fs -ls -R / drwxr-xr-x - hadoop supergroup 0 2021-11-07 13:52 /abc drwxr-xr-x - hadoop supergroup 0 2021-11-07 13:47 /abc/abc -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:27 /abc.txt drwxr-xr-x - hadoop supergroup 0 2021-11-07 13:52 /test -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:49 /test/abc.txt? ? ? ??從上面的命令中可以看出,/abc/abc.txt 文件被移動到了 /test/ 目錄下。再來看下它的重命名功能:
$ hadoop fs -mv /test/abc.txt /test/abcabc.txt $ hadoop fs -ls /test/ Found 1 items -rw-r--r-- 2 hadoop supergroup 5 2021-11-07 13:49 /test/abcabc.txt? ? ? ? 通過 ls 命令可以看到,abc.txt 已經(jīng)被重命名為了 abcabc.txt。
十、cat 命令
? ? ? ? cat 命令用來輸出 HDFS 文件系統(tǒng)中某個文件的所有內(nèi)容,命令如下:
$ hadoop fs -cat /test/abcabc.txt 1234 $ hadoop fs -cat /abc.txt 1234十一、appendToFile 命令
? ? ? ??將單個或多個文件的內(nèi)容從本地系統(tǒng)追加到 HDFS 系統(tǒng)的文件中,命令如下:
$ hadoop fs -appendToFile abc.txt /abc.txt $ hadoop fs -cat /abc.txt 1234 1234? ? ? ??可以看到,/abc.txt 的內(nèi)容已經(jīng)發(fā)生了改變。
十二、總結(jié)
? ? ? ? HDFS 關(guān)于文件的基本操作與 Linux 系統(tǒng)命令的基本是一樣的,只是 HDFS 命令增加了 hadoop fs 這樣的前綴。如果對 Linux 系統(tǒng)命令有些了解,那么 HDFS 的基本操作命令也會非常容易的上手。
總結(jié)
以上是生活随笔為你收集整理的大数据笔记 | HDFS 常用操作命令的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 关于在asp.net中textbox文本
- 下一篇: setInterval和setTimeo