Advertisement
Guest User

modified_wordcount_test

a guest
Aug 8th, 2016
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.06 KB | None | 0 0
  1. /**
  2.  * Licensed to the Apache Software Foundation (ASF) under one
  3.  * or more contributor license agreements.  See the NOTICE file
  4.  * distributed with this work for additional information
  5.  * regarding copyright ownership.  The ASF licenses this file
  6.  * to you under the Apache License, Version 2.0 (the
  7.  * "License"); you may not use this file except in compliance
  8.  * with the License.  You may obtain a copy of the License at
  9.  *
  10.  *     http://www.apache.org/licenses/LICENSE-2.0
  11.  *
  12.  * Unless required by applicable law or agreed to in writing, software
  13.  * distributed under the License is distributed on an "AS IS" BASIS,
  14.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15.  * See the License for the specific language governing permissions and
  16.  * limitations under the License.
  17.  */
  18. package abd.count;
  19.  
  20. import java.io.IOException;
  21. import java.util.StringTokenizer;
  22.  
  23. import org.apache.hadoop.conf.Configuration;
  24. import org.apache.hadoop.fs.Path;
  25. import org.apache.hadoop.io.IntWritable;
  26. import org.apache.hadoop.io.Text;
  27. import org.apache.hadoop.mapreduce.Job;
  28. import org.apache.hadoop.mapreduce.Mapper;
  29. import org.apache.hadoop.mapreduce.Reducer;
  30. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  31. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  32. import org.apache.hadoop.util.GenericOptionsParser;
  33.  
  34. public class WordCount {
  35.  
  36.   public static class TokenizerMapper
  37.        extends Mapper<Object, Text, Text, IntWritable>{
  38.    
  39.     private final static IntWritable one = new IntWritable(1);
  40.      
  41.     public void map(Object key, Text value, Context context
  42.                     ) throws IOException, InterruptedException {
  43.         context.write(value, one);
  44.      
  45.     }
  46.   }
  47.  
  48.   public static class IntSumReducer
  49.        extends Reducer<Text,IntWritable,Text,IntWritable> {
  50.     private IntWritable result = new IntWritable();
  51.  
  52.     public void reduce(Text key, Iterable<IntWritable> values,
  53.                        Context context
  54.                        ) throws IOException, InterruptedException {
  55.       int sum = 0;
  56.       for (IntWritable val : values) {
  57.         sum += val.get();
  58.       }
  59.       result.set(sum);
  60.       context.write(key, result);
  61.     }
  62.   }
  63.  
  64.   public static void main(String[] args) throws Exception {
  65.     Configuration conf = new Configuration();
  66.     String[] otherArgs = new GenericOptionsParser(conf, args).getRemainingArgs();
  67.     if (otherArgs.length < 2) {
  68.       System.err.println("Usage: wordcount <in> [<in>...] <out>");
  69.       System.exit(2);
  70.     }
  71.     Job job = Job.getInstance(conf, "word count");
  72.     job.setJarByClass(WordCount.class);
  73.     job.setMapperClass(TokenizerMapper.class);
  74.     job.setCombinerClass(IntSumReducer.class);
  75.     job.setReducerClass(IntSumReducer.class);
  76.     job.setOutputKeyClass(Text.class);
  77.     job.setOutputValueClass(IntWritable.class);
  78.     FileInputFormat.addInputPath(job, new Path("anypath"));
  79.    
  80.     FileOutputFormat.setOutputPath(job,
  81.       new Path(otherArgs[otherArgs.length - 1]));
  82.     System.exit(job.waitForCompletion(true) ? 0 : 1);
  83.   }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement