摘要:本文記錄了如何在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) 執行時注意加入包名,或者打包時加入包名