Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.82 KB | None | 0 0
  1. package org.myorg;
  2.  
  3. import java.io.IOException;
  4. import java.util.*;
  5.  
  6. import org.apache.hadoop.fs.Path;
  7. import org.apache.hadoop.conf.*;
  8. import org.apache.hadoop.io.*;
  9. import org.apache.hadoop.mapred.*;
  10. import org.apache.hadoop.util.*;
  11.  
  12. public class FindMax {
  13.  
  14. public static class Map extends MapReduceBase implements Mapper<LongWritable, Text, Text, IntWritable> {
  15. private IntWritable val = new IntWritable();
  16. private Text department = new Text();
  17.  
  18. public void map(LongWritable key, Text value, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
  19. String line = value.toString();
  20. for (int i=0; i<line.length(); i=i+2){
  21. department.set(line[i]);
  22. val.set(line[i+1].parseInt());
  23. }
  24. output.collect(department, val);
  25. }
  26. }
  27.  
  28. public static class Reduce extends MapReduceBase implements Reducer<Text, FloatWritable, Text, FloatWritable> {
  29.  
  30. public void reduce(Text key, Iterator<IntWritable> values, OutputCollector<Text, IntWritable> output, Reporter reporter) throws IOException {
  31. int maxx = 0;
  32. while (values.hasNext()) {
  33. int temp = values.next().get();
  34. if (maxx < temp){
  35. maxx = temp;
  36. }
  37. }
  38. output.collect(key, new IntWritable(maxx));
  39. }
  40. }
  41.  
  42. public static void main(String[] args) throws Exception {
  43. JobConf conf = new JobConf(FindMax.class);
  44. conf.setJobName("findmax");
  45.  
  46. conf.setOutputKeyClass(Text.class);
  47. conf.setOutputValueClass(IntWritable.class);
  48.  
  49. conf.setMapperClass(Map.class);
  50. conf.setCombinerClass(Reduce.class);
  51. conf.setReducerClass(Reduce.class);
  52.  
  53. conf.setInputFormat(TextInputFormat.class);
  54. conf.setOutputFormat(TextOutputFormat.class);
  55.  
  56. FileInputFormat.setInputPaths(conf, new Path(args[0]));
  57. FileOutputFormat.setOutputPath(conf, new Path(args[1]));
  58.  
  59. JobClient.runJob(conf);
  60. }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement