Hadoop的三种模式:
本地模式:本地模拟实现,不使用分布式文件系统
伪分布式模式:5个进程在一台主机上启动,一般开发人员调试hadoop程序使用
完全分布式模式:至少3个结点,JobTracker和NameNode在同一台主机上,secondaryNameNode一台主机,DataNode和Tasktracker一台主机
本次试验环境:
CentOS2.6.32-358.el6.x86_64
jdk-7u21-linux-x64.rpm
hadoop-0.20.2-cdh3u6.tar.gz
一、hadoop伪分布式模式的配置
[root@localhost ~]# rpm -ivh jdk-7u21-linux-x64.rpm
[root@localhost ~]# vim /etc/profile.d/Java.sh
JAVA_HOME=/usr/java/latest
PATH=$JAVA_HOME/bin:$PATH
export JAVA_HOME PATH
[root@localhost ~]# tar xf hadoop-0.20.2-cdh3u6.tar.gz -C /usr/local/
[root@localhost ~]# cd /usr/local/
[root@localhost local]# ln -sv hadoop-0.20.2-cdh3u6/ hadoop
[root@localhost ~]# vim /etc/profile.d/hadoop.sh
HADOOP_HOME=/usr/local/hadoop
PATH=$HADOOP_HOME/bin:$PATH
export HADOOP_HOME PATH
测试jdk和hadoop是否正确安装
[root@localhost ~]# java -version
[root@localhost ~]# hadoop version
创建用户并修改hadoop文件权限
[root@localhost ~]# useradd hduser
[root@localhost ~]# passwd hduser
[root@localhost ~]# chown -R hduser.hduser /usr/local/hadoop/
创建hadoop临时数据保存目录
[root@localhost ~]# mkdir /hadoop/temp -pv
[root@localhost ~]# chown -R hduser.hduser /hadoop/
主要脚本功能:
/usr/local/hadoop/bin/start-dfs.sh 启动namenode datanode secondarynamenode进程
/usr/local/hadoop/bin/start-mapred.sh 启动jobtracker tasktracker
/usr/local/hadoop/bin/hadoop-daemon.sh 单独启动某个进程
/usr/local/hadoop/bin/start-all.sh 启动全部进程
/usr/local/hadoop/bin/stop-all.sh 停止全部进程
主要配置文件:
/usr/local/hadoop/conf/masters 保存第二名称节点的位置(secondaryNameNode)
/usr/local/hadoop/conf/slaves 保存从节点的位置(所有运行tasktracker和datanode的结点)
/usr/local/hadoop/conf/core-site.xml 用于定义系统级别的参数
/usr/local/hadoop/conf/hdfs-site.xml HDFS的相关设定
/usr/local/hadoop/conf/mapred-site.xml HDFS的相关设定,如reduce任务的默认个数、任务所能够使用内存的默认上下限等
/usr/local/hadoop/conf/hadoop-env.sh 定义hadoop运行环境相关的配置信息
让hadoop启动起来,只需修改一下配置文件即可
[root@localhost conf]# vim core-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href=https://www.linuxidc.com/Linux/2013-05/\'#\'" Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<value>/hadoop/temp</value>
</property>
<property>
<name>fs.default.name</name>
<value>hdfs://localhost:8020</value>
</property>
</configuration>
[root@localhost conf]# vim mapred-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href=https://www.linuxidc.com/Linux/2013-05/\'#\'" Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>localhost:8021</value>
</property>
</configuration>
[root@localhost conf]# vim hdfs-site.xml
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href=https://www.linuxidc.com/Linux/2013-05/\'#\'" Put site-specific property overrides in this file. -->
<configuration>
<property>
<name>dfs.replication</name>
<value>1</value>
</property>
</configuration>
配置hduser通过ssh不需要密码访问本机
[hduser@localhost ~]$ ssh-keygen -t rsa -P ''
[hduser@localhost .ssh]$ ssh-copy-id -i id_rsa.pub hduser@localhost
[hduser@localhost ~]$ hadoop namenode -format 格式化名称结点[hduser@localhost ~]$ start-all.sh 启动服务
[hduser@localhost ~]$ jps 查看进程
NameNode
DataNode
JobTracker
TaskTracker
secondaryNameNode
如果以上5个进程启动起来,说明hadoop配置成功
hadoop常用命令:
[hduser@localhost ~]$ hadoop 查看帮助
[hduser@localhost ~]$ hadoop fs
[hduser@localhost ~]$ hadoop fs -mkdir test 在HDFS上创建目录
[hduser@localhost ~]$ hadoop fs -ls 查看文件或目录
[hduser@localhost ~]$ hadoop fs -put test.txt test 上传本地文件到HDFS