以编译hadoop-mapred-examples-0.22.0.jar中的WordCount为例。
Version:
hadoop-0.22.0
Compile In Eclipse:
新建一个项目WordCount,新建package: demo.mapreduce,并将hadoop-0.22.0\mapreduce\src\examples\org\apache\hadoop\examples下的WordCount.java文件放到该package下。
为了能够编译成功,需要添加以下jar:
hadoop-0.22.0\hadoop-mapred-0.22.0.jar
hadoop-0.22.0\hadoop-hdfs-0.22.0.jar
hadoop-0.22.0\hadoop-common-0.22.0.jar
hadoop-0.22.0\lib\commons-cli-1.2.jar
添加方式:
项目右键->Build Path->Configure Build Path...->Add External Jars...
编译导出为WordCount.jar,上传到Linux环境,通过以下方式执行:
1 | hadoop jar WordCount.jar demo.mapreduce.WordCount /input_file /output_file |
Compile in Linux:
假设WordCount.java中的包结构为:
package demo.mapreduce;
使用如下命令编译:
2 | javac -classpath ${HADOOP_HOME}/hadoop-mapred-0.22.0.jar: |
3 | ${HADOOP_HOME}/hadoop-hdfs-0.22.0.jar: |
4 | ${HADOOP_HOME}/hadoop-common-0.22.0.jar: |
5 | ${HADOOP_HOME}/lib/commons-cli-1.2.jar |
6 | -d mapreduce_classes WordCount.java |
编译过后,在mapreduce_classes/demo/mapreduce目录下会看到编译后产生的class文件:
WordCount.class, WordCount$IntSumReducer.class, WordCount$TokenizerMapper.class。
使用如下命令打包:
1 | jar -cvf WordCount.jar -C mapreduce_classes/ . |
通过以下方式执行:
view source
print?
1 | hadoop jar WordCount.jar demo.mapreduce.WordCount /input_file /output_file |