2019-05-17
查看Hadoop版本。
WZB-MacBook:target wangzhibin$ hadoop version Hadoop 2.8.4 Subversion https://git-wip-us.apache.org/repos/asf/hadoop.git -r 17e75c2a11685af3e043aa5e604dc831e5b14674 Compiled by jdu on 2018-05-08T02:50Z Compiled with protoc 2.5.0 From source with checksum b02a59bb17646783210e979bea443b0 This command was run using /Users/wangzhibin/00_dev_suite/50_bigdata/hadoop-2.8.4/share/hadoop/common/hadoop-common-2.8.4.jar列出hdfs文件系统根目录下的目录和文件
hadoop fs -ls /列出hdfs文件系统所有的目录和文件
hadoop fs -ls -R /一级一级的建目录,父目录不存在的话使用这个命令会报错
command: hadoop fs -mkdir < hdfs path> eg: WZB-MacBook:~ wangzhibin$ hadoop fs -mkdir /test/20190517 WZB-MacBook:~ wangzhibin$ hadoop fs -ls -R /test drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 10:49 /test/20190517所创建的目录如果父目录不存在就创建该父目录
hadoop fs -mkdir -p < hdfs path>上传文件。hdfs file的父目录一定要存在,否则命令不会执行
command: hadoop fs -put < local file > < hdfs file > eg: $ hadoop fs -put tmp/tmp.txt /test $ hadoop fs -ls -R /test上传目录。hdfs dir 一定要存在,否则命令不会执行
command: hadoop fs -put < local file or dir >...< hdfs dir > eg: WZB-MacBook:50_bigdata wangzhibin$ hadoop fs -put tmp/ /test WZB-MacBook:50_bigdata wangzhibin$ hadoop fs -ls -R /test drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 10:42 /test/tmp -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:42 /test/tmp/tmp.txt -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:40 /test/tmp.txt从键盘读取输入到hdfs file中,按Ctrl+D(Control+D)结束输入。hdfs file不能存在,否则命令不会执行
command: hadoop fs -put - < hdsf file> eg: WZB-MacBook:50_bigdata wangzhibin$ hadoop fs -put - /test/20190517.tmp.txt hello world WZB-MacBook:50_bigdata wangzhibin$ hadoop fs -ls -R /test -rw-r--r-- 1 wangzhibin supergroup 12 2019-05-17 10:44 /test/20190517.tmp.txt drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 10:42 /test/tmp -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:42 /test/tmp/tmp.txt -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:40 /test/tmp.txt在标准输出中显示文件内容
command: hadoop fs -cat < hdfs file> eg: WZB-MacBook:50_bigdata wangzhibin$ hadoop fs -cat /test/20190517.tmp.txt hello world在标准输出中显示文件末尾的1KB数据
command: hadoop fs -tail < hdfs file> eg: WZB-MacBook:50_bigdata wangzhibin$ hadoop fs -tail /test/20190517.tmp.txt hello world从hdfs中下载文件到本地。local file不能和 hdfs file名字不能相同,否则会提示文件已存在,没有重名的文件会复制到本地
command: hadoop fs -get < hdfs file > < local file or dir> eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -get /test/20190517.tmp.txt . WZB-MacBook:tmp wangzhibin$ ls 20190517.tmp.txt tmp.txt拷贝多个文件或目录到本地时,本地要为文件夹路径
command: hadoop fs -get < hdfs file or dir > ... < local dir > eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -get /test . WZB-MacBook:tmp wangzhibin$ ls -l test/ total 24 drwxr-xr-x 2 wangzhibin staff 64 5 17 10:56 20190517 -rw-r--r-- 1 wangzhibin staff 12 5 17 10:56 20190517.tmp.txt drwxr-xr-x 3 wangzhibin staff 96 5 17 10:56 tmp -rw-r--r-- 1 wangzhibin staff 4662 5 17 10:56 tmp.txt**注意:**如果用户不是root, local 路径要为用户文件夹下的路径,否则会出现权限问题。
每次可以删除多个文件或目录
command: hadoop fs -rm < hdfs file > ... hadoop fs -rm -r < hdfs dir>... eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -rm /test/20190517.tmp.txt Deleted /test/20190517.tmp.txt WZB-MacBook:tmp wangzhibin$ hadoop fs -ls -R /test drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 10:49 /test/20190517 drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 10:42 /test/tmp -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:42 /test/tmp/tmp.txt -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:40 /test/tmp.txtHDFS拷贝文件或文件夹。目标文件或者文件夹不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件还存在。
command: hadoop fs -cp < hdfs file > < hdfs file > hadoop fs -cp < hdfs file or dir >... < hdfs dir > eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -cp /test/tmp.txt /test/20190517/ WZB-MacBook:tmp wangzhibin$ hadoop fs -ls -R /test drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 11:03 /test/20190517 -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 11:03 /test/20190517/tmp.txt drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 10:42 /test/tmp -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:42 /test/tmp/tmp.txt -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:40 /test/tmp.txtHDFS移动文件或文件夹。目标文件不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件不存在。源路径有多个时,目标路径必须为目录,且必须存在。
command: hadoop fs -mv < hdfs file > < hdfs file > hadoop fs -mv < hdfs file or dir >... < hdfs dir > eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -mv /test/20190517/tmp.20190519.txt /test/tmp WZB-MacBook:tmp wangzhibin$ hadoop fs -ls -R /test drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 11:06 /test/20190517 -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 11:03 /test/20190517/tmp.txt drwxr-xr-x - wangzhibin supergroup 0 2019-05-17 11:06 /test/tmp -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 11:04 /test/tmp/tmp.20190519.txt -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:42 /test/tmp/tmp.txt -rw-r--r-- 1 wangzhibin supergroup 4662 2019-05-17 10:40 /test/tmp.txt**注意:**跨文件系统的移动(local到hdfs或者反过来)都是不允许的
统计hdfs对应路径下的目录个数,文件个数,文件总计大小 显示为目录个数,文件个数,文件总计大小,输入路径
command: hadoop fs -count < hdfs path > eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -count /test 3 4 18648 /test显示hdfs对应路径下每个文件夹和文件的大小
command: hadoop fs -du < hdsf path> eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -du /test 4662 /test/20190517 9324 /test/tmp 4662 /test/tmp.txt显示hdfs对应路径下所有文件和的大小
command: hadoop fs -du -s < hdsf path> eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -du -s /test 18648 /test显示hdfs对应路径下每个文件夹和文件的大小,文件的大小用方便阅读的形式表示,例如用64M代替67108864
command: hadoop fs -du -h < hdsf path> eg: WZB-MacBook:tmp wangzhibin$ hadoop fs -du -h /test 4.6 K /test/20190517 9.1 K /test/tmp 4.6 K /test/tmp.txt以下命令参考:hadoop HDFS常用文件操作命令。没有实践。
与put相类似,命令执行后源文件 local src 被删除,也可以从从键盘读取输入到hdfs file中
与put相类似,也可以从从键盘读取输入到hdfs file中
当前版本中还未实现此命令
与get相类似
将hdfs指定目录下所有文件排序后合并到local指定的文件中,文件不存在时会自动创建,文件存在时会覆盖里面的内容
hadoop fs -getmerge -nl < hdfs dir > < local file >加上nl后,合并到local file中的hdfs文件之间会空出一行
将文本文件或某些格式的非文本文件通过文本格式输出
改变一个文件在hdfs中的副本个数,上述命令中数字3为所设置的副本个数,-R选项可以对一个人目录下的所有目录+文件递归执行改变副本个数的操作
返回对应路径的状态信息 [format]可选参数有:%b(文件大小),%o(Block大小),%n(文件名),%r(副本个数),%y(最后一次修改日期和时间) 可以这样书写hadoop fs -stat %b%o%n < hdfs path >,不过不建议,这样每个字符输出的结果不是太容易分清楚
命令中参数name:压缩文件名,自己任意取;< hdfs parent dir > :压缩文件所在的父目录;< src >:要压缩的文件名;< hdfs dst >:压缩文件存放路径*示例:hadoop archive -archiveName hadoop.har -p /user 1.txt 2.txt /des 示例中将hdfs中/user目录下的文件1.txt,2.txt压缩成一个名叫hadoop.har的文件存放在hdfs中/des目录下,如果1.txt,2.txt不写就是将/user目录下所有的目录和文件压缩成一个名叫hadoop.har的文件存放在hdfs中/des目录下 显示har的内容可以用如下命令:
hadoop fs -ls /des/hadoop.jar显示har压缩的是那些文件可以用如下命令
hadoop fs -ls -R har:///des/hadoop.har**注意:**har文件不能进行二次压缩。如果想给.har加文件,只能找到原来的文件,重新创建一个。har文件中原来文件的数据并没有变化,har文件真正的作用是减少NameNode和DataNode过多的空间浪费。
如果管理员发现某些DataNode保存数据过多,某些DataNode保存数据相对较少,可以使用上述命令手动启动内部的均衡过程
管理员可以通过dfsadmin管理HDFS,用法可以通过上述命令查看 hdfs dfsadmin -report
显示文件系统的基本数据
hdfs dfsadmin -safemode < enter | leave | get | wait >enter:进入安全模式;leave:离开安全模式;get:获知是否开启安全模式; wait:等待离开安全模式
用来在两个HDFS之间拷贝数据
参考资料
1.0.4版本官方文档-Hadoop Shell命令hadoop HDFS常用文件操作命令大数据基本组件(Hadoop、HDFS、MapRed、YARN)入门命令