标签:style blog http java color 使用
引入YARN作为通用资源调度平台后,Hadoop得以支持多种计算框架,如MapReduce、Spark、Storm等。MRv1是Hadoop1中的MapReduce,MRv2是Hadoop2中的MapReduce。下面是MRv1和MRv2之间的一些基本变化:
MRv2上的参数可以参考官方文档进行配置,但是有一个参数需要注意:mapreduce.job.user.classpath.first
。如果不配置该参数的话,在执行jar程序的时候,系统会优先选择Hadoop框架中已经存在的java类而不是用户指定包中自己编写的java类
org.apache.hadoop.mapred
包(旧包)和org.apache.hadoop.mapreduce
包(新包)。MapReduce包wordcount事例
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 { //context.nextKeyValue() 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); } }
标签:style blog http java color 使用
原文地址:http://blog.csdn.net/jiewuyou/article/details/37742669