前面学习了hadoop的完成全分布模式的配置和搭建。现在我们需要用Eclipse来编译hadoop-1.2.1的插件,实现Eclipse上的hadoop程序的开发和调试。
现在我们用一个为分布式的hadoop来测试。关于为分布式的安装请看 “centos6.4安装hadoop-1.2.1,实现wordcount功能 ”博文
1、在windows上准备,Eclipse,hadoop-1.2.1.tar.gz 的安装包。
2、打开Eclipse , 导入 eclipse-plugin 项目以及依赖包。具体如下:
2.1 将hadoop-1.2.1解压,并将 hadoop-1.2.1\src\contrib\eclipse-plugin文件夹,以项目的形式导入到Eclipse中。 项目名称自动为 MapReduceTools。
这里缺必要的包,所以会有 红色感叹号 !
2.2 添加必要的jar包。在项目根目录下创建 lib 文件夹,将依赖包复制到该文件中【hadoop-core-1.2.1.jar,commons-cli-1.2.jar,commons-configuration-1.6.jar,commons-httpclient-3.0.1.jar,commons-lang-2.4.jar,jackson-core-asl-1.8.8.jar,jackson-mapper-asl-1.8.8.jar】。并将依赖包buildpath。
依赖包添加完成!!!!项目没有任何错误提示。
3、 将hadoop-1.2.1\src\contrib 文件夹下的 build-contrib.xml 复制到项目的根目录下。并对配置文件进行配置。
3.1 在 build.properties 配置文件中 添加如下信息
#add eclipse.home and hadoop version
eclipse.home=D:/eclipse/eclipse
version=1.2.1
全部代码如下:
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
output.. = bin/
bin.includes = META-INF/,\
plugin.xml,\
resources/,\
classes/,\
classes/,\
lib/
#add eclipse.home and hadoop version
eclipse.home=D:/eclipse/eclipse
version=1.2.1
3.2 对 build.xml 进行配置。
3.2.1 <import file="../build-contrib.xml"/> 修改为 : <import file="build-contrib.xml"/> 【因为在同级目录,所以不需要跳到上级目录上】
3.2.2 修改以下 2 个地方。
<path id="eclipse-sdk-jars">
<fileset dir="${eclipse.home}/plugins/">
<include name="org.eclipse.ui*.jar"/>
<include name="org.eclipse.jdt*.jar"/>
<include name="org.eclipse.core*.jar"/>
<include name="org.eclipse.equinox*.jar"/>
<include name="org.eclipse.debug*.jar"/>
<include name="org.eclipse.osgi*.jar"/>
<include name="org.eclipse.swt*.jar"/>
<include name="org.eclipse.jface*.jar"/>
<include name="org.eclipse.team.cvs.ssh2*.jar"/>
<include name="com.jcraft.jsch*.jar"/>
</fileset>
<!-- 增加 -->
<!--build need hadoop classes file.-->
<fileset dir="${hadoop.root}">
<include name="hadoop*.jar"/>
</fileset>
</path>
以及:
<target name="jar" depends="compile" unless="skip.contrib">
<mkdir dir="${build.dir}/lib"/>
<!-- 注释 -->
<!--
<copy file="${hadoop.root}/build/hadoop-core-${version}.jar" tofile="${build.dir}/lib/hadoop-core.jar" verbose="true"/>
<copy file="${hadoop.root}/build/ivy/lib/Hadoop/common/commons-cli-${commons-cli.version}.jar" todir="${build.dir}/lib" verbose="true"/>
-->
<!-- 增加 -->
<copy file="${hadoop.root}/hadoop-core-${version}.jar" tofile="${build.dir}/lib/hadoop-core.jar" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-cli-1.2.jar" todir="${build.dir}/lib" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-lang-2.4.jar" todir="${build.dir}/lib" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-configuration-1.6.jar" todir="${build.dir}/lib" verbose="true"/>
<copy file="${hadoop.root}/lib/jackson-mapper-asl-1.8.8.jar" todir="${build.dir}/lib" verbose="true"/>
<copy file="${hadoop.root}/lib/jackson-core-asl-1.8.8.jar" todir="${build.dir}/lib" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-httpclient-3.0.1.jar" todir="${build.dir}/lib" verbose="true"/>
<copy file="${hadoop.root}/lib/commons-cli-${commons-cli.version}.jar" todir="${build.dir}/lib" verbose="true"/>
<jar
jarfile="${build.dir}/hadoop-${name}-${version}.jar"
manifest="${root}/META-INF/MANIFEST.MF">
<fileset dir="${build.dir}" includes="classes/ lib/"/>
<fileset dir="${root}" includes="resources/ plugin.xml"/>
</jar>
</target>
3.3 对 build-contrib.xml 进行修改
<property name="hadoop.root" location="E:/study/hadoop/hadoop-1.2.1"/>
<property name="version" value="1.2.1"/>
<property name="ivy.version" value="2.1.0"/>
<property name="eclipse.home" location="D:/eclipse/eclipse"/>
部分代码如下:代码过长,是贴出前面部分:
<project name="hadoopbuildcontrib" xmlns:ivy="antlib:org.apache.ivy.ant">
<property name="name" value="${ant.project.name}"/>
<property name="root" value="${basedir}"/>
<property name="hadoop.root" location="E:/study/hadoop/hadoop-1.2.1"/>
<property name="version" value="1.2.1"/>
<property name="ivy.version" value="2.1.0"/>
<property name="eclipse.home" location="D:/eclipse/eclipse"/>
<!-- Load all the default properties, and any the user wants -->
<!-- to contribute (without having to type -D or edit this file -->
<property file="${user.home}/${name}.build.properties" />
<property file="${root}/build.properties" />
<property file="${hadoop.root}/build.properties" /
..............................
...............................
................................
3.4 对 MANIFEST.MF 中的 Bundle-ClassPath 进行修改。
Bundle-ClassPath: classes/,lib/hadoop-core.jar,lib/commons-cli-1.2.jar,lib/commons-configuration-1.6.jar,
lib/commons-httpclient-3.0.1.jar,lib/commons-lang-2.4.jar,lib/jackson-core-asl-1.8.8.jar,
lib/jackson-mapper-asl-1.8.8.jar
【注意这里需要把这些JAR写成一排,不要跳行。这里跳行是因为写成一排不能全部显示出来!!!】
4、配置完成,开始编译。
右键 build.xml---> run as ----> ant built 。 console出现 BUILD SUCCESSFUL 。 即编译成功。
5、在 路径 hadoop-1.2.1\build\contrib\eclipse-plugin 下找到刚刚编译好的插件:hadoop-eclipse-plugin-1.2.1.jar
6、将 hadoop-eclipse-plugin-1.2.1.jar copy 到 Eclipse 的 plugins 文件目录下。重启Eclipse。重启后的Eclipse 左边项目导航栏里面会出现 DFS location。说明成功!
6.1 Eclipse中 window -- >Perferences ---> Hadoop MapReduce ---->hadoop install directory 添加 hadoop 的解压路径
6.2 在 show view 中 添加 Map/Redeuce Locations。添加后 右键添加hadoop location。然后host: hadoop的环境的IP 地址,Port:9001。DFS Master:port:9000,Advanced prarmeters的 hadoop.tmp.dir 是core-site.xml中的路径!【在配置之前,请将 hadoop环境中的 core-site.xml 与 mapred-site.xml 配置文件中的 localhost 改为 自己的 IP地址 】
6.3 配置成功后,Eclipse左边的项目导航栏中 会出现到HDFS 文件结构【这里需要linux关闭防火墙规则:service iptables stop。如果需要,可以在iptables中开放9000和9001端口】。
愉快的完成!