由于Hadoop是基于Java开发,所以首选在Linux上安装好Java环境。
1、Hadoop的下载安装
下载包到/usr/local
解压:tar -zxvf /usr/local/hadoop-3.1.2.tar.gz -C /usr/local
添加环境变量vim /etc/profile
1 | export HADOOP_HOME=/usr/local/hadoop-3.1.2 |
source /etc/profile
执行测试:hadoop version
2、目录结构
1 | bin目录:存放对Hadoop相关服务(HDFS,YARN)进行操作的脚本 |
Hadoop运行模式包括:本地模式、伪分布式模式以及完全分布式模式。
3、本地运行模式(单机模式)
3.1、官方Grep案例
1 | 创建在hadoop-2.7.2文件下面创建一个input文件夹 |
3.2、官方WordCount案例 *
统计单词数
1 | 建立输入目录和文件,并且写入测试内容 |
3、伪分布式运行模式
3.1、启动HDFS并运行MapReduce程序
** 配置集群 **
配置:hadoop-env.sh
1 | vim etc/hadoop/hadoop-env.sh |
配置:core-site.xml
1 | [root@VM_0_3_centos hadoop-3.1.2]# vim etc/hadoop/core-site.xml |
HDFS core-site.xml 参数配置
1 | – fs.default.name |
配置:hdfs-site.xml
1 | vim etc/hadoop/hdfs-site.xml |
HDFS hdfs-site.xml 参数配置
1 | – dfs.name.dir |
** 启动集群 **
1 | 格式化NameNode(第一次启动时格式化) |
web端查看HDFS文件系统:http://152.*****:9870/dfshealth.html#tab-overview 老版本端口是50070
查看Log日志 /usr/local/hadoop-3.1.2/logs
** 操作集群 **
在HDFS文件系统上创建一个input文件夹
1 | [root@VM_0_3_centos hadoop-3.1.2]# bin/hdfs dfs -mkdir -p /user/root/input |
将测试文件内容上传到文件系统上
1 | [root@VM_0_3_centos hadoop-3.1.2]# bin/hdfs dfs -put wcinput/wc.input /user/root/input/ |
查看上传的文件是否正确
1 | [root@VM_0_3_centos hadoop-3.1.2]# bin/hdfs dfs -ls /user/root/input/ |
1 | [root@VM_0_3_centos hadoop-3.1.2]# bin/hdfs dfs -cat /user/root/ input/wc.input |
运行MapReduce程序
1 | [root@VM_0_3_centos hadoop-3.1.2]# bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.2.jar wordcount /user/root/input/ /user/root/output |
查看输出结果
1 | [root@VM_0_3_centos hadoop-3.1.2]# bin/hdfs dfs -cat /user/root/output/* |
将测试文件内容下载到本地
1 | [root@VM_0_3_centos hadoop-3.1.2]# hdfs dfs -get /user/root/output/* ./wcoutput/ |
本地文件存在,先删除在下载
1 | [root@VM_0_3_centos hadoop-3.1.2]# rm -rf ./wcoutput/* |
查看本地结果
1 | [root@VM_0_3_centos hadoop-3.1.2]# cat ./wcoutput/* |
删除输出结果
1 | hdfs dfs -rm -r /user/root/output |
3.2、启动YARN并运行MapReduce程序
配置集群
配置环境
1 | [root@VM_0_3_centos hadoop-3.1.2]# vim etc/hadoop/yarn-env.sh |
配置yarn-site.xml
1 | <!-- Reducer获取数据的方式 --> |
配置:mapred-site.xml
1 | [root@VM_0_3_centos hadoop]# vim mapred-site.xml |
启动集群
启动前必须保证NameNode和DataNode已经启动
1 | [root@VM_0_3_centos hadoop-3.1.2]# sbin/yarn-daemon.sh start resourcemanager |
集群操作
YARN的浏览器查看:http://152.....56:8088/cluster
删除文件系统上的output文件
1 | [root@VM_0_3_centos hadoop-3.1.2]# bin/hdfs dfs -rm -R /user/root/output |
执行MapReduce程序
1 | bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.2.jar wordcount /user/root/input/ /user/root/output |
查看运行结果可以通过浏览器查看。
1 |
3.3、配置历史服务器
为了查看程序的历史运行情况,需要配置一下历史服务器。
配置mapred-site.xml
1 | vim mapred-site.xml |
启动历史服务器
sbin/mr-jobhistory-daemon.sh start historyserver
查看JobHistory
3.4、配置日志的聚集
应用运行完成以后,将程序运行日志信息上传到HDFS系统上。方便的查看到程序运行详情,方便开发调试。
注意:开启日志聚集功能,需要重新启动NodeManager 、ResourceManager和HistoryManager。
配置yarn-site.xml
1 | [atguigu@hadoop101 hadoop]$ vi yarn-site.xml |
重启NodeManager 、ResourceManager和HistoryManager
1 | [root@VM_0_3_centos hadoop-3.1.2]# sbin/yarn-daemon.sh stop resourcemanager |
删除HDFS上已经存在的输出文件
1 | [root@VM_0_3_centos hadoop-3.1.2]# bin/hdfs dfs -rm -R /user/root/output |
执行WordCount程序
1 | [root@VM_0_3_centos hadoop-3.1.2]# hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-3.1.2.jar wordcount /user/root/input /user/root/output |
查看日志
3.5、配置文件说明
Hadoop配置文件分两类:默认配置文件和自定义配置文件,只有用户想修改某一默认配置值时,才需要修改自定义配置文件,更改相应属性值。
默认配置文件:
1 | 默认文件 在Hadoop的jar包中的位置 |
自定义配置文件:core-site.xml、hdfs-site.xml、yarn-site.xml、mapred-site.xml