一. Hadoop运行模式有三种:
-
本地模式:这是默认模式,不需要启用单独的进程,直接可以运行,测试和开发时使用。
-
伪分布模式:等同于完全分布式,但是只有一个节点
-
完全分布式模式:多个节点一起运行
二. 本地模式运行案例
-
官方
grep
案例-
在
hadoop-2.7.2
文件夹下创建input文件夹[starfish@hadoop100 hadoop-2.7.2]$mkdir input
-
将
hadoop
的xml配置文件复制到input(只是作为一个众多数据,也可以自己导自己写的文件)[starfish@hadoop100 hadoop-2.7.2]$cp etc/hadoop/*.xml input
-
执行share目录下的
mapreduce
程序[starfish@hadoop100 hadoop-2.7.2]bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar grep input output 'dfs[a-z.]+'
上面的命令的意思就是说,运行
bin/hadoop
,执行share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar
里面的grep
, 查找input文件夹下面符合'dfs[a-z.]+'
这个正则规则的字段,然后将结果输出到当前目录下的output文件夹下(output文件夹是自动生成的) -
查看输出结果
[starfish@hadoop100 hadoop-2.7.2]cat output/*
-
-
官方
wordcount
案例-
在
hadoop-2.7.2
文件夹下创建一个wcinput
文件夹[starfish@hadoop100 hadoop-2.7.2]$mkdir wcinput
-
在
wcinput
文件夹下创建一个wc.input
文件[starfish@hadoop100 hadoop-2.7.2]$cd wcinput [starfish@hadoop100 wcinput]$touch wc.input
-
编辑
wc.input
[starfish@hadoop100 wcinput]$vim wc.input 在文件中输入如下内容 hadoop yarn hadoop mapreduce hello you you 保存退出::wq
-
回到
/opt/module/hadoop-2.7.2
文件夹下执行程序[starfish@hadoop100 hadoop-2.7.2]$hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar wordcount wcinput wcoutput
上面命令的意思就是: 运行
hadoop
, 执行share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar
里面的wordcount
, 需要做wordcount
的数据在wcinput
文件夹下,最后将结果输出到当前目录的wcoutput
里面。
-
三. 伪分布式运行案例
伪分布式分为在HDFS
上运行MapReduce
程序和在YARN上运行MapReduce
程序
-
在
HDFS
上运行MapReduce
程序(1) 首先要保证这台虚拟机安装了Hadoop和jdk,并且为hadoop和jdk配置了环境变量
(2)配置一些文件
hadoop-env.sh里面修改JAVA_HOME路径:
export JAVA_HOME=/opt/module/jdk1.7.0_79
core-site.xml
<!-- 指定HDFS中NameNode的地址 --> <property> <name>fs.defaultFS</name> <value>hdfs://hadoop100:8020</value> </property> <!-- 指定hadoop运行时产生文件的存储目录 --> <property> <name>hadoop.tmp.dir</name> <value>/opt/module/hadoop-2.7.2/data/tmp</value> </property>
hdfs-site.xml
<!-- 指定HDFS副本的数量 --> <property> <name>dfs.replication</name> <value>1</value> </property>
(3) 格式化Namenode
[starfish@hadoop100 hadoop-2.7.2]$bin/hdfs namenode -format
注意:第一次启动时格式化,以后不要总是都格式化。
(4)启动namenode和datanode
#启动namenode [starfish@hadoop100 hadoop-2.7.2]$sbin/hadoop-daemon.sh start namenode #用jps查看是否启动,出现namenode就表示已启动 [starfish@hadoop100 hadoop-2.7.2]$jps #启动datanode [starfish@hadoop100 hadoop-2.7.2]$sbin/hadoop-daemon.sh start datanode [starfish@hadoop100 hadoop-2.7.2]$jps
(5)在web增删查(不能修改)HDFS文件系统
在浏览器输入URL: http://192.168.10.100:50070/dfshealth.html#tab-overview
就可以看到HDFS文件系统。
(6)在hdfs上运行mapreduce程序步骤
-
在hdfs文件系统上创建一个input文件
[starfish@hadoop100 hadoop-2.7.2]$bin/hdfs dfs -mkdir -p /user/atguigu/mapreduce/wordcount/input
-
将本地的文件夹上传到hdfs文件系统上
[starfish@hadoop100 hadoop-2.7.2]$bin/hdfs dfs -put wcinput/wc.input /user/atguigu/mapreduce/wordcount/input/
-
在hdfs上运行mapreduce程序
[starfish@hadoop100 hadoop-2.7.2]$bin/hadoop jar share/hadoop/mapreduce/hadoop-mapreduce-examples-2.7.2.jar wordcount /user/atguigu/mapreduce/wordcount/input/ /user/atguigu/mapreduce/wordcount/output
-
查看结果
[starfish@hadoop100 hadoop-2.7.2]$bin/hdfs dfs -cat /user/atguigu/mapreduce/wordcount/output/*
或者可以直接选择在网页上面查看
web端查看HDFS文件系统:
http://192.168.1.101:50070/dfshealth.html#tab-overview
- 删除结果的命令
[starfish@hadoop100 hadoop-2.7.2]$hdfs dfs -rmr /user/atguigu/mapreduce/wordcount/output
-
-
在
YARN上
运行MapReduce
程序
L3O-1587981692481)]
- 删除结果的命令
[starfish@hadoop100 hadoop-2.7.2]$hdfs dfs -rmr /user/atguigu/mapreduce/wordcount/output
- 在
YARN上
运行MapReduce
程序
未完待续,Hia~