一:环境准备:基于Windows下的VM9的ubuntu12.04-64server.
- 下载免费的VMware Player并安装好;
- 下载 免费的Ubuntu 12.04 server版并在VMware中安装好;
二:基础安装:
- 执行如下命令升级部分软件和把ssh安装好:
(1) sudo apt-get update;
(2) sudo apt-get upgrade;
(3) sudo apt-get install openssh-server;
- 手动安装JDK。
(1) 下载jdk1.6。我用的是32位的
(2) 安装JDK并设置环境变量地址:http://www.linuxidc.com/Linux/2012-06/62078.htm
- 创建hadoop用户。
(1) sudo addgroup hadoop
(2) sudo adduser --ingroup hadoop hduser
- 建立SSH信任关系,登录localhost就不需要密码
$ cd /home/hduser
$ ssh-keygen -t rsa -P "" #直接回车
$cat .ssh/id_rsa.pub >>.ssh/authorized_keys
注:可通过ssh localhost命令验证。
三:正式安装:
注:以下操作以hduser登录进行操作。
- 下载hadoop2.2版本。地址:http://apache.dataguru.cn/hadoop/common/hadoop-2.2.0/hadoop-2.2.0.tar.gz。
- 执行tar zxf hadoop-2.2.0.tar.gz解压至当前目录/home/hduser目录下。
- mv hadoop-2.2.0 hadoop
四:配置hadoop:
- 编辑/home/hduser/hadoop/etc/hadoop/hadoop-env.sh
替换exportJAVA_HOME=${JAVA_HOME}为如下:
export JAVA_HOME=/usr/local/java/jdk1.6.0_45/
export JAVA_HOME=$JAVA_HOME这样设置会报错,只有使用绝对路径才不会报错
- 编辑/home/hduser/hadoop/etc/hadoop/core-site.xml,在<configuration>中添加如下:
备注:配置了/home/hduser/hadoop/tmp/这个目录,必须执行mkdir /home/hduser/hadoop/tmp/创建它,否则后面运行会报错。
- 编辑/home/hduser/hadoop/etc/hadoop/mapred-site.xml:
(1) mv /home/hduser/hadoop/etc/hadoop/mapred-site.xml.template/home/hduser/hadoop/etc/hadoop/mapred-site.xml
(2) 在<configuration>中添加如下:
五:运行Hadoop
在初次运行Hadoop的时候需要初始化Hadoop文件系统,命令如下:
cd /home/hduser/hadoop/bin
$./hdfs namenode -format //格式化namenode
如果执行成功,你会在日志中(倒数几行)找到如下成功的提示信息:
common.Storage: Storage directory/home/hduser/hadoop/tmp/hadoop-hduser/dfs/name has been successfully formatted.
运行命令如下:
$cd /home/hduser/hadoop/sbin/
$./start-dfs.sh //开始hadoop守护进程
注:该过程需要多次输入密码, 如果不想多次输入密码,可先用ssh建立信任。
hduser@ubuntu:~/hadoop/sbin$ jps //查看守护进程
4266 SecondaryNameNode
4116 DataNode
4002 NameNode
注:用jps查看启动了三个进程。
$./start-yarn.sh
hduser@ubuntu:~/hadoop/sbin$ jps
4688 NodeManager
4266 SecondaryNameNode
4116 DataNode
4002 NameNode
4413 ResourceManager
六:查看Hadoop资源管理器
http://192.168.128.129:8088/,将其中的192.168.128.129替换为你的实际IP地址。
七:测试Hadoop
运行hadoop自带的词频统计的例子
(执行以下步骤的前提是先启动hadoop,namenode和datanode进程必须启动)
首先在hdfs虚拟文件上创建文件夹input 命令:hdfs dfs -mkdir /input
上传文件到hdfs上命令:hdfs dfs -put /home/hduser/hadoop/etc/hadoop/ /input
(把/home/hduser/hadoop/etc/hadoop/下所有的文件复制到input下)
执行例子,命令
hadoop jar /home/hduser/hadoop/share/hadoop/mapreduce/hadoop-mapreduce-examples- 2.2.0.jar wordcount /input/hadoop /output
注意:output是自动创建的,执行前不能存在
执行成功后,可以通过浏览器访问HDFS文件下的output:
八:停止Hadoop
若停止hadoop,依次运行如下命令:
$./stop-yarn.sh
$./stop-dfs.sh
九:参考资料
- http://wenku.baidu.com/view/1681511a52d380eb62946df6.html
- DFSShell
HDFS允许用户数据由文件和文件夹式的管理,它提供一个接口叫DFSShell,让用户和HDFS中的数据交互 命令集的语法跟其他的shells(bash,csh)相似
创建目录foodir : hadoop fs -mkdir /foodir
查看文件 /foodir/myfile.txt : hadoop dfs -cat /foodir/myfile.txt
删除文件/foodir/myfile.txt : hadoop dfs -rm /foodir myfile.txt
DFSAdmin
DFSAdmin命令集是用于管理dfs集群的,这些命令只由HDFS管理员使用
将集群设置成安全模式 : hadoop dfsadmin-safemode enter
产生一个数据节点的列表 : hadoop dfsadmin-report或者hdfsdfsadmin -report去掉一个数据节点: hadoop dfsadmin-decommission datanodename