摘要:本文记录了如何在Hadoop 2.2.0环境下编译、启动运行和测试自己的程序。
关键词:hadoop2.2.0 编译例子 WordCount.java 打包 wordcount.jar
一、前提条件:部署好hadoop 2.2.0环境
部署hadoop 2.2.0 环境可以参考:Hadoop2.2.0单节点安装及测试
环境如下:
用户:hduser
hadoop 2.2.0 部署在目录: /home/hduser/hadoop-2.2.0
WordCount.java 通过winrar解压文件 /home/hduser/hadoop-2.2.0/share/hadoop/mapreduce/sources/hadoop-mapreduce-examples-2.2.0-sources.jar 可以得到。
设置环境变量PATH:
vim /home/hduser/.bash_profile ,执行命令 i ,并填入如下内容
export HADOOP_HOME=/home/hduser/hadoop-2.2.0
export HADOOP_LIB_HOME=/home/hduser/hadoop-2.2.0/share/hadoop
PATH=$HADOOP_HOME/bin:$PATH:$HOME/bin
export PATH
顺序执行命令 ESC , : , wq , enter
然后 source /home/hduser/.bash_profile
二、单词统计例子(WordCount.java)是什么样子的
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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.
*/
package org.apache.hadoop.examples;
import java.io.IOException;
import java.util.StringTokenizer;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Job;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.Reducer;
import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
import org.apache.hadoop.util.GenericOptionsParser;
public class WordCount {
public static class TokenizerMapper
extends Mapper<Object, Text, Text, IntWritable>{
private final static IntWritable one = new IntWritable(1);
private Text word = new Text();
public void map(Object key, Text value, Context context
) throws IOException, InterruptedException {
StringTokenizer itr = new StringTokenizer(value.toString());
while (itr.hasMoreTokens()) {
word.set(itr.nextToken());
context.write(word, one);
}
}
}
public static class IntSumReducer
extends Reducer<Text,IntWritable,Text,IntWritable> {
private IntWritable result = new IntWritable();
public void reduce(Text key, Iterable<IntWritable> values,
Context context
) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable val : values) {
sum += val.get();
}
result.set(sum);
context.write(key, result);
}
}
public static void main(String[] args) throws Exception {
Configuration conf = new Configuration();
String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
if (otherArgs.length != 2) {
System.err.println("Usage: wordcount <in> <out>");
System.exit(2);
}
Job job = new Job(conf, "word count");
job.setJarByClass(WordCount.class);
job.setMapperClass(TokenizerMapper.class);
job.setCombinerClass(IntSumReducer.class);
job.setReducerClass(IntSumReducer.class);
job.setOutputKeyClass(Text.class);
job.setOutputValueClass(IntWritable.class);
FileInputFormat.addInputPath(job, new Path(otherArgs[0]));
FileOutputFormat.setOutputPath(job, new Path(otherArgs[1]));
System.exit(job.waitForCompletion(true) ? 0 : 1);
}
}
三、准备数据
cd /home/hduser
wget http://www.gutenberg.org/cache/epub/20417/pg20417.txt
cd hadoop-2.2.0
bin/hdfs dfs -mkdir /tmp
bin/hdfs dfs -copyFromLocal /home/hduser/pg20417.txt /tmp
bin/hdfs dfs -ls /tmp
四、编译hadoop2.2 例子程序WordCount.java
4.1 编译需要hadoop2.2的库文件:
/home/hduser/hadoop-2.2.0/share/hadoop/common/lib/commons-cli-1.2.jar
/home/hduser/hadoop-2.2.0/share/hadoop/common/hadoop-common-2.2.0.jar
/home/hduser/hadoop-2.2.0/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.2.0.jar
4.2 编译工具:
1) 在redhat或者ubuntu下开启Eclipse进行编译
step1:
创建工程,创建包org.apache.hadoop.examples,将WordCount.java加入包中,这时会看到许多错误,那是因为缺少hadoop的jar包。
step2:
然后加入库文件commons-cli-1.2.jar, hadoop-common-2.2.0.jar, hadoop-mapreduce-client-core-2.2.0.jar。这时,之前的错误就消失了。
step3:
在Eclipse自动编译后,进入工程bin目录org.apache.hadoop.examples下可以看到三个class文件:WordCount.class,WordCount$IntSumReducer.class,WordCount$TokenizerMapper.class
step4:
cd /home/hduser/workspace/hdpHelloWord/bin/org/apache/hadoop/examples
step5:
用命令打包 jar -cvf wordcount.jar -C ./ . 可以得到 wordcount.jar
step6:
执行命令 hadoop jar wordcount.jar org.apache.hadoop.examples.WordCount /tmp /tmp-output ,注意加上包名org.apache.hadoop.examples.WordCount才能正常运行。运行需要一点点时间,屏幕会输出中间结果。
step7:
查看结果,执行命令 hdfs dfs -ls /tmp-output/tmp-output/_SUCCESS/tmp-output/part-r-00000
2) 用命令行编译
step1:
javac -classpath /home/hduser/hadoop-2.2.0/share/hadoop/common/lib/commons-cli-1.2.jar:/home/hduser/hadoop-2.2.0/share/hadoop/common/hadoop-common-2.2.0.jar:/home/hduser/hadoop-2.2.0/share/hadoop/mapreduce/hadoop-mapreduce-client-core-2.2.0.jar WordCount.java
step2:
用命令打包 jar -cvf wordcount.jar -C ./ . 可以得到 wordcount.jar
step3:
执行命令 hadoop jar wordcount.jar org.apache.hadoop.examples.WordCount /tmp /tmp-output ,注意加上包名org.apache.hadoop.examples.WordCount才能正常运行。运行需要一点点时间,屏幕会输出中间结果。
step4:
查看结果,执行命令 hdfs dfs -ls /tmp-output/tmp-output/_SUCCESS/tmp-output/part-r-00000
执行编译
执行打包
查看打包结果
五、总结
利用hadoop2.2.0编译自己工程或者代码时,注意一下几点:
1)搭建好hadoop2.2.0环境
2) 知道需要的hadoop库文件
3) 编译时引用hadoop的库文件
4) 执行时注意加入包名,或者打包时加入包名