上一篇文章,博主为大家分享了hadoop的安装以及集群的启动,本篇博客将带领小伙伴们一起来感受下hadoop命令和Linux命令的使用有什么不同。

    一、首先,启动hadoop集群,执行脚本sh start-dfs.sh;sh start-yarn.sh

大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

    二、浏览器中查看dfs中的文件目录(此时文件为空),对应的到hadoop中数据节点中的路径/home/hadoop/hdpdata/dfs/data/current/BP-302498708-192.168.29.144-1540943832361/current/finalized,其中/home/hadoop/hdpdata为配置文件中配dfs路径。

大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

    三、命令行使用体验

#查看hdfs根目录
hadoop fs -ls hdfs://centos-aaron-h1:9000/ 或 hadoop fs -ls /

#将test.avi这个文件上传到hdfs跟目录中,默认为128m才切开成不同的块来存放.
hadoop fs -put test.avi /
hadoop fs -put test.avi test1.avi /
#如果文件被切成了不同的块,我们知道是哪几个块的话,可以用块直接还原文件
cat bloc_2034655 >>hadoop.file
cat bloc_2034656 >>hadoop.file
tar -zxvf hadoop.file -C /opt

#浏览test.avi文件内容
hadoop fs -cat /test.avi

#从hdfs中下载test.avi这个文件
hadoop fs -get  /test.avi

#删除文件或文件夹
hadoop fs -rmr /wordcount/output 或 hadoop fs -rm -r /wordcount/output

#递归新建文件夹
hadoop fs -mkdir -p /wordcount/input

#运行jar包,wordcount为主类,/wordcount/input为主类运行需要的自己的参数输入目录,/wordcount/output/为输出目录;如果"/wordcount/output/"已经存在会报错
hadoop jar hadoop-mapreduce-examples-2.9.1.jar wordcount  /wordcount/input/ /wordcount/output/

    效果图:大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

    上传的文件在web页面可以下载

大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

    四、运行第一个mapreduce程序

           首先,hadoop官网jar包本身自动有一些用于测试的mapreduce程序jar包,它们在/home/hadoop/apps/hadoop-2.9.1/share/hadoop/mapreduce目录下:    

大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

           本次测试使用hadoop-mapreduce-examples-2.9.1.jar(官网的文档中有介绍)这个jar包,该jar包是内有一个单词统计的mapreduce程序,主类是:wordcount;

           测试步骤:

           a.准备用于测试单词统计使用的输入目录和结果输出目录

#递归新建输入目录
hadoop fs -mkdir -p /wordcount/input

           b. 准备用于测试单词统计的保存着单词的文件

#新建文件test.txt,并且输入些单词报错
vi test.txt
ctrl+z+z
#复制一份
cp test.txt test1.txt
#上传到上一步用于做单词统计的输入目录
hadoop fs -put test.txt test1.txt /wordcount/input

            c.运行hadoop-mapreduce-examples-2.9.1.jar

#运行jar包,wordcount为主类,/wordcount/input为主类运行需要的自己的参数输入目录,/wordcount/output/为输出目录;如果"/wordcount/output/"已经存在会报错
hadoop jar hadoop-mapreduce-examples-2.9.1.jar wordcount  /wordcount/input/ /wordcount/output/

大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

执行效果
[hadoop@centos-aaron-h3 mapreduce]$ hadoop jar hadoop-mapreduce-examples-2.9.1.jar wordcount  /wordcount/input/ /wordcount/output/
18/11/04 04:59:13 INFO client.RMProxy: Connecting to ResourceManager at centos-aaron-h1/192.168.29.144:8032
18/11/04 04:59:14 INFO input.FileInputFormat: Total input files to process : 2
18/11/04 04:59:15 INFO mapreduce.JobSubmitter: number of splits:2
18/11/04 04:59:15 INFO Configuration.deprecation: yarn.resourcemanager.system-metrics-publisher.enabled is deprecated. Instead, use yarn.system-metrics-publisher.enabled
18/11/04 04:59:16 INFO mapreduce.JobSubmitter: Submitting tokens for job: job_1541270195271_0001
18/11/04 04:59:17 INFO impl.YarnClientImpl: Submitted application application_1541270195271_0001
18/11/04 04:59:17 INFO mapreduce.Job: The url to track the job: http://centos-aaron-h1:8088/proxy/application_1541270195271_0001/
18/11/04 04:59:17 INFO mapreduce.Job: Running job: job_1541270195271_0001
18/11/04 04:59:29 INFO mapreduce.Job: Job job_1541270195271_0001 running in uber mode : false
18/11/04 04:59:29 INFO mapreduce.Job:  map 0% reduce 0%
18/11/04 04:59:46 INFO mapreduce.Job:  map 100% reduce 0%
18/11/04 04:59:56 INFO mapreduce.Job:  map 100% reduce 100%
18/11/04 04:59:57 INFO mapreduce.Job: Job job_1541270195271_0001 completed successfully
18/11/04 04:59:57 INFO mapreduce.Job: Counters: 49
        File System Counters
                FILE: Number of bytes read=40
                FILE: Number of bytes written=592624
                FILE: Number of read operations=0
                FILE: Number of large read operations=0
                FILE: Number of write operations=0
                HDFS: Number of bytes read=257
                HDFS: Number of bytes written=13
                HDFS: Number of read operations=9
                HDFS: Number of large read operations=0
                HDFS: Number of write operations=2
        Job Counters
                Launched map tasks=2
                Launched reduce tasks=1
                Data-local map tasks=2
                Total time spent by all maps in occupied slots (ms)=29268
                Total time spent by all reduces in occupied slots (ms)=7337
                Total time spent by all map tasks (ms)=29268
                Total time spent by all reduce tasks (ms)=7337
                Total vcore-milliseconds taken by all map tasks=29268
                Total vcore-milliseconds taken by all reduce tasks=7337
                Total megabyte-milliseconds taken by all map tasks=29970432
                Total megabyte-milliseconds taken by all reduce tasks=7513088
        Map-Reduce Framework
                Map input records=2
                Map output records=2
                Map output bytes=30
                Map output materialized bytes=46
                Input split bytes=235
                Combine input records=2
                Combine output records=2
                Reduce input groups=1
                Reduce shuffle bytes=46
                Reduce input records=2
                Reduce output records=1
                Spilled Records=4
                Shuffled Maps =2
                Failed Shuffles=0
                Merged Map outputs=2
                GC time elapsed (ms)=798
                CPU time spent (ms)=4970
                Physical memory (bytes) snapshot=547995648
                Virtual memory (bytes) snapshot=2537877504
                Total committed heap usage (bytes)=259543040
        Shuffle Errors
                BAD_ID=0
                CONNECTION=0
                IO_ERROR=0
                WRONG_LENGTH=0
                WRONG_MAP=0
                WRONG_REDUCE=0
        File Input Format Counters
                Bytes Read=22
        File Output Format Counters
                Bytes Written=13

             d.查看执行结果,_SUCCESS代表执行成功的文件,part-r-00000是输出结果文件

hadoop fs -ls /wordcount/output
hadoop fs -cat /wordcount/output/part-r-00000

大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

大数据教程(6.5)hadoop shell命令操作&mapreduce程序运行初体验-LMLPHP

        五、hadoop命令详解

#1.获取命令提示
hadoop fs

#2.查看hadoop fs命令帮助,功能:输出这个命令参数手册
hadoop fs -help

#3.管道命令分页查看
hadoop fs -cat  /wordcount/output/part-r-00000 | more

#4.功能:显示目录信息
hadoop fs -ls                  
示例:
hadoop fs -ls hdfs://hadoop-server01:9000/
备注:这些参数中,hadoop-server01为namenode的主机名或域名,所有的hdfs路径都可以简写-->
hadoop fs -ls /   等同于上一条命令的效果

#5.功能:在hdfs上创建目录
hadoop fs -mkdir              
示例:
hadoop fs -mkdir  -p  /aaa/bbb/cc/dd

#6.功能:从本地剪切粘贴到hdfs
hadoop fs -moveFromLocal            
示例:
hadoop fs -moveFromLocal  /home/hadoop/a.txt  /aaa/bbb/cc/dd

#7.功能:从hdfs剪切粘贴到本地
hadoop fs -moveToLocal              
示例:
hadoop fs -moveToLocal   /aaa/bbb/cc/dd  /home/hadoop/a.txt

#8.功能:追加一个文件到已经存在的文件末尾
hadoop fs -appendToFile  
示例:
hadoop fs -appendToFile  ./hello.txt  hdfs://hadoop-server01:9000/hello.txt
可以简写为:
Hadoop fs -appendToFile  ./hello.txt  /hello.txt

#9.功能:显示文件内容
Hadoop fs -cat  
示例:
hadoop fs -cat  /hello.txt


#10.功能:显示一个文件的末尾
hadoop fs -tail                 
示例:
hadoop fs -tail  /weblog/access_log.1

#11.功能:以字符形式打印一个文件的内容
hadoop fs -text                  
示例:
hadoop fs -text  /weblog/access_log.1

#13.功能:linux文件系统中的用法一样,对文件所属权限
-chgrp
-chmod
-chown
示例:
hadoop fs -chmod  666  /hello.txt
hadoop fs -chown  someuser:somegrp   /hello.txt

#14.功能:从本地文件系统中拷贝文件到hdfs路径去
hadoop fs -copyFromLocal    
示例:
hadoop fs -copyFromLocal  ./jdk.tar.gz  /aaa/

#15.功能:从hdfs拷贝到本地
hadoop fs -copyToLocal      
示例:
hadoop fs -copyToLocal /aaa/jdk.tar.gz

#16.功能:从hdfs的一个路径拷贝hdfs的另一个路径
hadoop fs -cp              
示例:
hadoop fs -cp  /aaa/jdk.tar.gz  /bbb/jdk.tar.gz.2

#17.功能:在hdfs目录中移动文件
hadoop fs -mv                     
示例:
hadoop fs -mv  /aaa/jdk.tar.gz  /

#18.功能:等同于copyToLocal,就是从hdfs下载文件到本地
hadoop fs -get              
示例:
hadoop fs -get  /aaa/jdk.tar.gz

#19.功能:合并下载多个文件
hadoop fs -getmerge             
示例:比如hdfs的目录 /aaa/下有多个文件:log.1, log.2,log.3,...
hadoop fs -getmerge /aaa/log.* ./log.sum

#20.功能:等同于copyFromLocal
hadoop fs -put                
示例:
hadoop fs -put   /aaa/jdk.tar.gz  /bbb/jdk.tar.gz.2

#21.功能:删除文件或文件夹
hadoop fs -rm               
示例:
hadoop fs -rm -r /aaa/bbb/

#22.功能:删除空目录
hadoop fs -rmdir                
示例:
hadoop fs -rmdir   /aaa/bbb/ccc

#23.功能:统计文件系统的可用空间信息
hadoop fs -df            
示例:
hadoop fs -df -h /

#24.功能:统计文件夹的大小信息
hadoop fs -du
示例:
hadoop fs -du -s -h /aaa/*
根目录统计需写全路径centos-aaron-h1为namenode域名
hadoop fs -du -s -h hdfs://centos-aaron-h1:9000/

#25.功能:统计一个指定目录下的文件节点数量
hadoop fs -count         
示例:
hadoop fs -count  /aaa/

#26.功能:设置hdfs中文件的副本数量
hadoop fs -setrep                
示例:
hadoop fs -setrep 3 /aaa/jdk.tar.gz
<这里设置的副本数只是记录在namenode的元数据中,是否真的会有这么多副本,还得看datanode的数量>

    最后补充:

    a、hadoop hdfs上传大于设置的限制大小值的文件时,会进行分块上传到hdfs文件系统,对外还是表现为一个文件,但在内部可能存在不同的节点,且可能包含很多个块;

    b、hadoop hdfs中没有用户和组的严格限定,你可以设置任何用户和组,即使设置的用户和组不存在也可以设置成功;

    c、分块保存原理,上传时用Input循环读,当读的值大于设置值(默认128m)时,从新new一个socket和output写到另外一个分块文件,这样同一个文件的不同块就可能存在不同的集群节点,且每个块都有和设置值(默认3份)相同的副本;

    d、mapreduce程序运行时,会在hdfs根目录产生一个临时文件夹tmp,是些临时数据等;

    e、设置的副本数的值大于datanode节点时,最多只会保存副本数为datanode的节点数量,多一个文件块不会存多分副本在一台datanode节点上;

    f、在namenode web控制台看到的文件副本数是元数据的副本值,真实值由于上面e说明的那个原因,可能存在偏差;

    g、namenode的hdfs默认访问端口为9000

    以上是博主本次文章的全部内容,如果大家觉得博主的文章还不错,请点赞;如果您对博主其它服务器技术或者博主本人感兴趣,请关注博主博客,并且欢迎随时跟博主沟通交流。

11-04 14:25