环境:
Ubuntu14.04
安装步骤:
1,准备工作
因为Hadoop的namenode将使用SSH协议启动namenode和datanode进程,但是伪分布式的namenode和datanode都是本身,所以需要配置SSH localhost的无密码验证。
1.1安装SSH Server
sudo apt-get install openssh-server
检查是否安装好:ps -e|grep ssh,如果出现ssh-agent和sshd说明server已经启动。默认服务端口是22,也可以自己在/etc/ssh/ssh_config文件进行修改自己定义服务端口
然后重启sudo /etc/init.d/ssh restart
1.2安装jdk
1.2.1下载JDK http://www.oracle.com/technetwork/java/javase/downloads/jdk8-downloads-2133151.html按自己的需要选择下载,例如我下的是jdk-8u20-linux-x64.tar.gz
1.2.2 解压jdk
tar -zvfx jdk-8u20-linux-x64.tar.gz /usr/local/java
1.2.3 配置环境变量
在/etc/profile后面添加:
export JAVA_HOME=/usr/local/java
export JRE_HOME=$JAVA_HOME/jre
export CLASSPATH=.$CLASSPATH:$JAVA_HOME/lib:$JRE_HOME/lib
export PATH=$PATH:$JAVA_HOME/bin:$JRE_HOME/bin
1.2.4 使更改生效source /etc/profile
1.2.5 如果java,javac都有相应的输出则表明安装成功
例如,输入java-version:
java version "1.7.0_67"
Java(TM) SE Runtime Environment (build 1.7.0_67-b01)
Java HotSpot(TM) 64-Bit Server VM (build 24.65-b04, mixed mode)
安装成功!!
1.3 增加一个hadoop用户组,便于hadoop和运行和管理(可选)
sudo addgroup hadoop
sudo adduer -ingroup hadoop hadoop
1.4 配置SSH
1.4.1 切换到hadoop用户 su hadoop
1.4.2 ssh -keygen -t rsa -P ""
cat $HOME/.ssh/id_rsa.pub>>$HOME/.ssh/authorized_keys
1.4.3 配置完成测试一下ssh localhost
hadoop@localhost's password:
Welcome to Ubuntu 14.04.1 LTS (GNU/Linux 3.13.0-36-generic x86_64)
* Documentation: https://help.ubuntu.com/
79 packages can be updated.
2 updates are security updates.
Last login: Thu Oct 9 15:28:24 2014 from localhost
2,安装hadoop
2.1 下载解压缩
下载地址: http://mirror.bit.edu.cn/apache/hadoop/common/hadoop-1.2.1/ 是hadoop-1.2.1的稳定版本。
解压tar -zvfx hadoop-1.2.1.tar.gz /opt/hadoop
修改所有者为hadoop用户:chown -R hadoop hadoop
2.2 配置环境变量
在/etc/profile后面添加
HADOOP_HOME=/opt/hadoop
PATH=$HADOOP_HOME/bin:$PATH
CLASSPATH=.:$JAVA_HOME/lib/dt.jar:$JAVA_HOME/lib/tools.jar:$HADOOP_HOME/lib:$CLASSPATH
export HADOOP_HOME
export PATH
export CLASSPATH
2.3 xml文件配置
vim /opt/hadoop/conf/core-site.xml
<configuration>
<property>
<name>hadoop.tmp.dir</name>
<!hdfs临时数据存放文件夹>
<value>/home/hadoop/hadoop-tmpdata/</value>
<description>A base for other temporary directories.</description>
</property>
<property>
<!--fs.default.name指定NameNode的IP地址和端口号-->
<name>fs.default.name</name>
<value>hdfs://localhost:54310</value>
<description>The name of the default file system. A URI whose
scheme and authority determine the FileSystem implementation. The
uri's scheme determines the config property (fs.SCHEME.impl) naming
the FileSystem implementation class. The uri's authority is used to
determine the host, port, etc. </description>
</property>
</configuration>
vim /opt/hadoop/conf/mapred-site.xml
<configuration>
<property>
<name>mapred.job.tracker</name>
<value>localhost:54311</value>
<description>The host and port that the MapReduce job tracker runs
at. If "local", then jobs are run in-process as a single map
and reduce task.
</description>
</property>
</configuration>
vim /opt/hadoop/conf/hdfs-site.xml
<property>
<!--block的副本数,默认为3;你可以设置为1 这样每个block只会存在一份。-->
<name>dfs.replication</name>
<value>1</value>
<description>Default block replication.
The actual number of replications can be specified when the file is created.
The default is used if replication is not specified in create time.
</description>
</property>
配置完成。
2.4格式化HDFS
/opt/hadoop/bin/hadoop namenode -format
2.5启动hadoop
/opt/hadoop/bin ./start-all.sh
如果启动后
1565 TaskTracker
1082 DataNode
1268 SecondaryNameNode
1680 Jps
1363 JobTracker
847 NameNode
这几个进程都在,则表明hadoop配置成功。
3,wordcount示例
3.1 在~/test下创建两个示例文件
echo ”hello hello cat“>file0
echo "hello hello kitty">file1
3.2 将本地文件夹test上传到hdfs作为输入
hadoop fs -put test input
3.3 运行wordcount示例程序
hadoop jar /opt/hadoop/hadoop-examples-1.2.1.jar wordcount input output
3.4 查看输出
hadoop fs -cat output/part-r-00000
cat 1
hello 4
kitty 1
需要注意的地方:
1,每次在运行hdfs格式化命令之前要把之前的hdfs的临时文件夹删除先,不然会出现Incompatible namespaceIDs的错误,这是因为每次格式化namenode都清除了自己的信息但是没有清除datanode的信息,这就造成了两个ID不一样的情况,但是还是要重新创建一个文件夹给hdfs存放临时数据。这个文件夹的地址也要在core-site.xml中配置。