首页 > 代码库 > hadoop 2.2.0 编译运行wordcount
hadoop 2.2.0 编译运行wordcount
hadoop2.2.0 编译运行wordcount,因为hadoop2.2.0不支持eclipse的插件,所以运行wordcount,需要手动编译并将wordcount打包成jar包来运行,下面记录一下编译运行的过程,希望能给大家有些帮助。
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/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
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