前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >大数据学习之路03——Hadoop常用命令详解

大数据学习之路03——Hadoop常用命令详解

原创
作者头像
汪志宾
修改2019-05-27 10:56:29
1.6K0
修改2019-05-27 10:56:29
举报
文章被收录于专栏:文斌的专栏

2019-05-17

Hadoop基本命令

version

查看Hadoop版本。

代码语言:txt
复制
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基础命令

命令格式

代码语言:txt
复制
hadoop fs -cmd < args >

ls

列出hdfs文件系统根目录下的目录和文件

代码语言:txt
复制
hadoop fs -ls  /

列出hdfs文件系统所有的目录和文件

代码语言:txt
复制
hadoop fs -ls -R /

mkdir

一级一级的建目录,父目录不存在的话使用这个命令会报错

代码语言:txt
复制
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

所创建的目录如果父目录不存在就创建该父目录

代码语言:txt
复制
hadoop fs -mkdir -p < hdfs path> 

put

上传文件。hdfs file的父目录一定要存在,否则命令不会执行

代码语言:txt
复制
command: hadoop fs -put < local file > < hdfs file >
eg: 
$ hadoop fs -put tmp/tmp.txt /test
$ hadoop fs -ls -R /test

上传目录。hdfs dir 一定要存在,否则命令不会执行

代码语言:txt
复制
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不能存在,否则命令不会执行

代码语言:txt
复制
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

cat

在标准输出中显示文件内容

代码语言:txt
复制
command: hadoop fs -cat < hdfs file> 
eg:
WZB-MacBook:50_bigdata wangzhibin$ hadoop fs -cat /test/20190517.tmp.txt
hello
world

tail

在标准输出中显示文件末尾的1KB数据

代码语言:txt
复制
command: hadoop fs -tail < hdfs file> 
eg:
WZB-MacBook:50_bigdata wangzhibin$ hadoop fs -tail /test/20190517.tmp.txt
hello
world

get

从hdfs中下载文件到本地。local file不能和 hdfs file名字不能相同,否则会提示文件已存在,没有重名的文件会复制到本地

代码语言:txt
复制
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

拷贝多个文件或目录到本地时,本地要为文件夹路径

代码语言: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 路径要为用户文件夹下的路径,否则会出现权限问题。

rm

每次可以删除多个文件或目录

代码语言:txt
复制
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.txt

cp

HDFS拷贝文件或文件夹。目标文件或者文件夹不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件还存在。

代码语言:txt
复制
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.txt

mv

HDFS移动文件或文件夹。目标文件不能存在,否则命令不能执行,相当于给文件重命名并保存,源文件不存在。源路径有多个时,目标路径必须为目录,且必须存在。

代码语言:txt
复制
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或者反过来)都是不允许的

count

统计hdfs对应路径下的目录个数,文件个数,文件总计大小

显示为目录个数,文件个数,文件总计大小,输入路径

代码语言:txt
复制
command: hadoop fs -count < hdfs path >
eg:
WZB-MacBook:tmp wangzhibin$ hadoop fs -count /test
           3            4              18648 /test

du

显示hdfs对应路径下每个文件夹和文件的大小

代码语言:txt
复制
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对应路径下所有文件和的大小

代码语言:txt
复制
command: hadoop fs -du -s < hdsf path> 
eg:
WZB-MacBook:tmp wangzhibin$ hadoop fs -du -s /test
18648  /test

显示hdfs对应路径下每个文件夹和文件的大小,文件的大小用方便阅读的形式表示,例如用64M代替67108864

代码语言:txt
复制
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

HDFS高级命令

以下命令参考:hadoop HDFS常用文件操作命令。没有实践。

moveFromLocal

代码语言:txt
复制
hadoop fs -moveFromLocal  < local src > ... < hdfs dst >

与put相类似,命令执行后源文件 local src 被删除,也可以从从键盘读取输入到hdfs file中

copyFromLocal

代码语言:txt
复制
hadoop fs -copyFromLocal  < local src > ... < hdfs dst >

与put相类似,也可以从从键盘读取输入到hdfs file中


moveToLocal

当前版本中还未实现此命令

copyToLocal

代码语言:txt
复制
hadoop fs -copyToLocal < local src > ... < hdfs dst >

与get相类似

getmerge

代码语言:txt
复制
hadoop fs -getmerge < hdfs dir >  < local file >

将hdfs指定目录下所有文件排序后合并到local指定的文件中,文件不存在时会自动创建,文件存在时会覆盖里面的内容

代码语言:txt
复制
hadoop fs -getmerge -nl  < hdfs dir >  < local file >

加上nl后,合并到local file中的hdfs文件之间会空出一行

text

代码语言:txt
复制
hadoop fs -text < hdsf file>

将文本文件或某些格式的非文本文件通过文本格式输出


setrep

代码语言:txt
复制
hadoop fs -setrep -R 3 < hdfs path >

改变一个文件在hdfs中的副本个数,上述命令中数字3为所设置的副本个数,-R选项可以对一个人目录下的所有目录+文件递归执行改变副本个数的操作


stat

代码语言:txt
复制
hdoop fs -stat [format] < hdfs path >

返回对应路径的状态信息

format可选参数有:%b(文件大小),%o(Block大小),%n(文件名),%r(副本个数),%y(最后一次修改日期和时间)

可以这样书写hadoop fs -stat %b%o%n < hdfs path >,不过不建议,这样每个字符输出的结果不是太容易分清楚

archive

代码语言:txt
复制
hadoop archive -archiveName name.har -p < hdfs parent dir > < src >* < hdfs dst >

命令中参数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的内容可以用如下命令:

代码语言:txt
复制
hadoop fs -ls /des/hadoop.jar

显示har压缩的是那些文件可以用如下命令

代码语言:txt
复制
hadoop fs -ls -R har:///des/hadoop.har

注意:har文件不能进行二次压缩。如果想给.har加文件,只能找到原来的文件,重新创建一个。har文件中原来文件的数据并没有变化,har文件真正的作用是减少NameNode和DataNode过多的空间浪费。

balancer

代码语言:txt
复制
hdfs balancer

如果管理员发现某些DataNode保存数据过多,某些DataNode保存数据相对较少,可以使用上述命令手动启动内部的均衡过程

dfsadmin

代码语言:txt
复制
hdfs dfsadmin -help

管理员可以通过dfsadmin管理HDFS,用法可以通过上述命令查看

hdfs dfsadmin -report

显示文件系统的基本数据

代码语言:txt
复制
hdfs dfsadmin -safemode < enter | leave | get | wait >

enter:进入安全模式;leave:离开安全模式;get:获知是否开启安全模式;

wait:等待离开安全模式

distcp

用来在两个HDFS之间拷贝数据

MapReduce命令

命令帮助

代码语言:txt
复制
WZB-MacBook:target wangzhibin$ mapred -help
Usage: mapred [--config confdir] [--loglevel loglevel] COMMAND
       where COMMAND is one of:
  pipes                run a Pipes job
  job                  manipulate MapReduce jobs
  queue                get information regarding JobQueues
  classpath            prints the class path needed for running
                       mapreduce subcommands
  historyserver        run job history servers as a standalone daemon
  distcp <srcurl> <desturl> copy file or directories recursively
  archive -archiveName NAME -p <parent path> <src>* <dest> create a hadoop archive
  archive-logs         combine aggregated logs into hadoop archives
  hsadmin              job history server admin interface

Most commands print help when invoked w/o parameters.

列出所有任务

代码语言:txt
复制
WZB-MacBook:target wangzhibin$ mapred job -list all
19/05/20 10:22:55 INFO client.RMProxy: Connecting to ResourceManager at /0.0.0.0:8032
Total jobs:1
                  JobId	     State	     StartTime	    UserName	       Queue  Priority	 UsedContainers	 RsvdContainers	 UsedMem	 RsvdMem	 NeededMem	   AM info
 job_1558104288185_0001	 SUCCEEDED	 1558104322342	  wangzhibin	     default   DEFAULT	            N/A	            N/A	     N/A	     N/A	       N/A	http://WZB-MacBook.local:8088/proxy/application_1558104288185_0001/

强制停止任务

代码语言:txt
复制
mapred job -kill <job-id>

参考资料

  1. 1.0.4版本官方文档-Hadoop Shell命令
  2. hadoop HDFS常用文件操作命令
  3. 大数据基本组件(Hadoop、HDFS、MapRed、YARN)入门命令

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • Hadoop基本命令
    • version
    • HDFS基础命令
      • 命令格式
        • ls
          • mkdir
            • put
              • cat
                • tail
                  • get
                    • rm
                      • cp
                        • mv
                          • count
                            • du
                            • HDFS高级命令
                              • moveFromLocal
                                • copyFromLocal
                                  • moveToLocal
                                    • copyToLocal
                                      • getmerge
                                        • text
                                          • setrep
                                            • stat
                                              • archive
                                                • balancer
                                                  • dfsadmin
                                                    • distcp
                                                    • MapReduce命令
                                                      • 命令帮助
                                                        • 列出所有任务
                                                          • 强制停止任务
                                                          相关产品与服务
                                                          大数据
                                                          全栈大数据产品,面向海量数据场景,帮助您 “智理无数,心中有数”!
                                                          领券
                                                          问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档