您好,登錄后才能下訂單哦!
這篇文章主要介紹hadoop 2.2.0如何編譯運行wordcount,文中介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們一定要看完!
1、首先介紹下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
2、然后就是準備程序WordCount.java在/root/test/下:
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); } }
3、新建bin文件夾在/root/test/下,將WordCount編譯成class文件,命令如下:
root@ubuntupc:/home/ubuntu/software/cdh6-hadoop/share/hadoop# javac -classpath common/hadoop-common-2.2.0-cdh6.0.0-beta-2.jar:common/lib/commons-cli-1.2.jar:common/lib/hadoop-annotations-2.2.0-cdh6.0.0-beta-2.jar:mapreduce/hadoop-mapreduce-client-core-2.2.0-cdh6.0.0-beta-2.jar -d /root/test/bin/ /root/test/WordCount.java
4、將class文件打包成jar包,命令如下:
root@ubuntupc:~/test# jar -cvf WordCount.jar com/du/simple/*.class
5、運行jar文件
root@ubuntupc:~/test# hadoop jar WordCount.jar com/du/simple/WordCount /user/root/input /user/root/output
6、查看運行結果
root@ubuntupc:~/hadoop/WordCount# hadoop fs -cat output/part-r-00000
好的,到此打完收功!
以上是“hadoop 2.2.0如何編譯運行wordcount”這篇文章的所有內容,感謝各位的閱讀!希望分享的內容對大家有幫助,更多相關知識,歡迎關注億速云行業資訊頻道!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。