hadoop2.2.0 編譯運行wordcount,由於hadoop2.2.0不支持eclipse的插件,因此運行wordcount,須要手動編譯並將wordcount打包成jar包來運行,下面記錄一下編譯運行的過程,但願能給你們有些幫助。html
一、首先介紹下hadoop的版本問題,當前Hadoop版本比較混亂,讓不少用戶不知所措。實際上,當前Hadoop只有兩個版本:Hadoop 1.0和Hadoop 2.0,其中,Hadoop 1.0由一個分佈式文件系統HDFS和一個離線計算框架MapReduce組成,而Hadoop 2.0則包含一個支持NameNode橫向擴展的HDFS,一個資源管理系統YARN和一個運行在YARN上的離線計算框架MapReduce。相比於Hadoop 1.0,Hadoop 2.0功能更增強大,且具備更好的擴展性、性能,並支持多種計算框架。因爲hadoop 2.0不用於hadoop 1.0的API,因此,從hadoop 1.0升級到hadoop 2.0須要重寫mapreduce程序,關於從Hadoop 1.0升級到2.0(1)參考連接: http://dongxicheng.org/mapreduce-nextgen/hadoop-upgrade-to-version-2/ hadoop 2.2.0新功能介紹 參考連接http://docs.aws.amazon.com/zh_cn/ElasticMapReduce/latest/DeveloperGuide/emr-hadoop-2.2.0-features.html java
二、而後就是準備程序WordCount.java在/root/test/下:shell
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(); // value已是文件內容的一行 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); } }
三、新建bin文件夾在/root/test/下,將WordCount編譯成class文件,命令以下:apache
root@ubuntupc:/home/ubuntu/software/cdh5-hadoop/share/hadoop# javac -classpath common/hadoop-common-2.2.0-cdh5.0.0-beta-2.jar:common/lib/commons-cli-1.2.jar:common/lib/hadoop-annotations-2.2.0-cdh5.0.0-beta-2.jar:mapreduce/hadoop-mapreduce-client-core-2.2.0-cdh5.0.0-beta-2.jar -d /root/test/bin/ /root/test/WordCount.java
四、將class文件打包成jar包,命令以下:ubuntu
root@ubuntupc:~/test# jar -cvf WordCount.jar com/du/simple/*.class
五、運行jar文件app
root@ubuntupc:~/test# hadoop jar WordCount.jar com/du/simple/WordCount /user/root/input /user/root/output
六、查看運行結果框架
root@ubuntupc:~/hadoop/WordCount# hadoop fs -cat output/part-r-00000
好的,到此打完收功!eclipse