Advertisement
Guest User

Untitled

a guest
May 26th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.46 KB | None | 0 0
  1. package kr.ac.kookmin.cs.bigdata;
  2.  
  3. import java.io.IOException;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.io.IntWritable;
  8. import org.apache.hadoop.io.LongWritable;
  9. import org.apache.hadoop.io.Text;
  10. import org.apache.hadoop.mapreduce.Job;
  11. import org.apache.hadoop.mapreduce.Mapper;
  12. import org.apache.hadoop.mapreduce.Reducer;
  13. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  14. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  15. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  16. import org.apache.hadoop.mapreduce.lib.output.TextOutputFormat;
  17.  
  18. public class WordCount {
  19.     public static void main(String[] args) throws Exception {
  20.         Configuration conf = new Configuration();
  21.  
  22.         Job job = Job.getInstance(conf, "wordcount");
  23.         job.setJarByClass(WordCount.class);
  24.  
  25.         job.setOutputKeyClass(Text.class);
  26.         job.setOutputValueClass(IntWritable.class);
  27.  
  28.         job.setMapperClass(Map.class);
  29.         job.setReducerClass(Reduce.class);
  30.  
  31.         job.setInputFormatClass(TextInputFormat.class);
  32.         job.setOutputFormatClass(TextOutputFormat.class);
  33.  
  34.         FileInputFormat.addInputPath(job, new Path(args[0]));
  35.         FileOutputFormat.setOutputPath(job, new Path(args[1]));
  36.  
  37.         job.waitForCompletion(true);
  38.     }
  39.  
  40.     public static class Map extends Mapper<LongWritable, Text, Text, IntWritable> {
  41.         private final static IntWritable one = new IntWritable(1);
  42.         private Text word = new Text();
  43.  
  44.         public void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  45.             String line = value.toString();
  46.             String[] lineArray = line.split(",");
  47.             String lastWord = lineArray[lineArray.length - 1];
  48.             String[] genreArray = lastWord.split("\\|");
  49.             for (String genre_value : genreArray) {
  50.                 word.set(genre_value);
  51.                 context.write(word, one);
  52.             }
  53.         }
  54.     }
  55.  
  56.     public static class Reduce extends Reducer<Text, IntWritable, Text, IntWritable> {
  57.         public void reduce(Text key, Iterable<IntWritable> values, Context context)
  58.                 throws IOException, InterruptedException {
  59.             int sum = 0;
  60.             for (IntWritable val : values) {
  61.                 sum += val.get();
  62.             }
  63.             context.write(key, new IntWritable(sum));
  64.         }
  65.     }
  66. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement