date: 2021-01-02title: HDFS的shell操作 #标题
tags: hadoop #标签
categories: Hadoop # 分类
记录下HDFS的常用命令。hadoop fs 和 hdfs dfs 命令功能一样。dfs是fs的实现类。
命令大全
$ 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] <localsrc> ... <dst>]
[-copyToLocal [-f] [-p] [-ignoreCrc] [-crc] <src> ... <localdst>]
[-count [-q] [-h] [-v] [-t [<storage type>]] [-u] [-x] <path> ...]
[-cp [-f] [-p | -p[topax]] [-d] <src> ... <dst>]
[-createSnapshot <snapshotDir> [<snapshotName>]]
[-deleteSnapshot <snapshotDir> <snapshotName>]
[-df [-h] [<path> ...]]
[-du [-s] [-h] [-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>]
[-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] [-d] <localsrc> ... <dst>]
[-renameSnapshot <snapshotDir> <oldName> <newName>]
[-rm [-f] [-r|-R] [-skipTrash] [-safely] <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> ...]
[-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 machines
不难发现,多数选项和linux系统命令一致。
-ls
$ hadoop fs -ls -R / # 递归查看hdfs的根目录
-mkdir
$ hadoop fs -mkdir -p /zhongguo/beijing # 创建目录,-p是递归创建
$ hadoop fs -ls -R /zhongguo # 查看目录
drwxr-xr-x - root supergroup 0 2020-11-15 19:17 /zhongguo/beijing
-moveFromLocal
将本地文件移动到hdfs。
$ echo "fengtaikejiyuan" > a.txt
$ hadoop fs -moveFromLocal a.txt /zhongguo/beijing/ # 上传到hdfs目录
$ hadoop fs -cat /zhongguo/beijing/a.txt # 查看移动到hdfs上的文件
fengtaikejiyuan
$ cat a.txt # 本地文件已被剪切走
cat: a.txt: 没有那个文件或目录
-appendToFile
追加本地文件内容到hdfs上的指定文件中。
$ echo "zhuowangxinxi" > b.txt
$ hadoop fs -appendToFile b.txt /zhongguo/beijing/a.txt # 将本地b.txt中的内容追加到hdfs上的a.txt。
$ hadoop fs -cat /zhongguo/beijing/a.txt # 验证是否追加成功
fengtaikejiyuan
zhuowangxinxi
-chgrp、-chmod、-chown
和linux上用法一样,功能分别是修改组、修改权限、修改属主属组。
$ hadoop fs -ls /zhongguo/beijing/a.txt # 查看文件当前权限
-rw-r--r-- 3 root supergroup 30 2020-11-15 19:24 /zhongguo/beijing/a.txt
$ hadoop fs -chgrp lvjianzhao /zhongguo/beijing/a.txt # 修改属组
$ hadoop fs -chown lvjianzhao /zhongguo/beijing/a.txt # 修改属主
$ hadoop fs -chmod 777 /zhongguo/beijing/a.txt # 修改权限
$ hadoop fs -ls /zhongguo/beijing/a.txt # 确认修改成功
-rwxrwxrwx 3 lvjianzhao lvjianzhao 30 2020-11-15 19:24 /zhongguo/beijing/a.txt
-copyFromLocal
将本地文件复制到hdfs上。
$ hadoop fs -copyFromLocal b.txt /zhongguo/beijing/
$ hadoop fs -ls /zhongguo/beijing/
Found 2 items
-rwxrwxrwx 3 lvjianzhao lvjianzhao 30 2020-11-15 19:24 /zhongguo/beijing/a.txt
-rw-r--r-- 3 root supergroup 14 2020-11-15 19:30 /zhongguo/beijing/b.txt
-copyTolocal
将hdfs上的文件copy到本地。
$ cat a.txt
cat: a.txt: 没有那个文件或目录
$ hadoop fs -copyToLocal /zhongguo/beijing/a.txt # copy到本地
$ cat a.txt # 查看文件内容
fengtaikejiyuan
zhuowangxinxi
-cp
从HDFS的一个路径复制到另一个路径。
$ hadoop fs -cp /zhongguo/beijing/a.txt /user/lvjianzhao/ # cp
$ hadoop fs -ls /user/lvjianzhao/a.txt # 查看
-rw-r--r-- 3 root supergroup 30 2020-11-15 19:44 /user/lvjianzhao/a.txt
$ hadoop fs -ls /zhongguo/beijing/a.txt # 源文件还在
-rwxrwxrwx 3 lvjianzhao lvjianzhao 30 2020-11-15 19:24 /zhongguo/beijing/a.txt
-mv
在HDFS目录中移动文件。
$ hadoop fs -mv /zhongguo/beijing/a.txt /zhongguo/ # 移动a.txt到上层目录
$ hadoop fs -ls -R /zhongguo/
-rwxrwxrwx 3 lvjianzhao lvjianzhao 30 2020-11-15 19:24 /zhongguo/a.txt
drwxr-xr-x - root supergroup 0 2020-11-15 19:47 /zhongguo/beijing
-rw-r--r-- 3 root supergroup 14 2020-11-15 19:30 /zhongguo/beijing/b.txt
-get
等同于copyToLocal,就是从hdfs下载文件到本地。
-getmerge
# 将hdfs上的a.txt和b.txt 合并未本地文件ab.txt
$ hadoop fs -getmerge /zhongguo/a.txt /zhongguo/beijing/b.txt ./ab.txt
# 亦可以在目录后使用通配符 “ * ”,表示合并目录下的所有文件。
$ cat ab.txt # 查看合并后的文件
fengtaikejiyuan
zhuowangxinxi
zhuowangxinxi
-put
等同于copyFromLocal,将本地文件copy到hdfs。
-tail
查看hdfs上的文件内容后面几行。
$ hadoop fs -put anaconda-ks.cfg /zhongguo/beijing/ # 上传文件到hdfs
$ hadoop fs -tail -f /zhongguo/beijing/anaconda-ks.cfg # 增加 “-f” 选项,相当于linux上的tailf。
-rm
删除文件或目录。
$ hadoop fs -rm /zhongguo/beijing/anaconda-ks.cfg # 删除文件
$ hadoop fs -rm -r -f /zhongguo # 删除一个目录
-rmdir
删除空目录。
$ hadoop fs -mkdir /test # 创建空目录
$ hadoop fs -rmdir /test # 删除空目录
-du
统计文件或目录大小。
$ hadoop fs -du -s -h /user # -s -h 表示以人性化的容量单位输出
349.5 M /user
$ hadoop fs -du /user # 不加 -s -h 输出如下
366447519 /user/lvjianzhao
-setrep
设置文件副本数。
先查看一个文件的副本数量:
$ hadoop fs -setrep 2 /user/lvjianzhao/test/a.txt
Replication 2 set: /user/lvjianzhao/test/a.txt
再次查看副本数量:
上传文件到指定的hdfs服务器
$ hdfs dfs -put hadoop-root-datanode-lv.log hdfs://192.168.20.2:9000/ha/test/