TT22

*.java

Sep 20th, 2017
245
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.67 KB | None | 0 0
  1. package tdd.tp03;
  2.  
  3. import java.io.IOException;
  4. import java.util.Collection;
  5.  
  6. import org.apache.hadoop.conf.Configuration;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.IntWritable;
  9. import org.apache.hadoop.io.NullWritable;
  10. import org.apache.hadoop.io.Text;
  11. import org.apache.hadoop.mapreduce.Mapper;
  12. import org.apache.hadoop.mapreduce.Reducer;
  13. import org.apache.hadoop.mapreduce.Job;
  14. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  15. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  17.  
  18. class SubstringCountMapper
  19. extends Mapper<Object, Text, Text, NullWritable> {
  20.  
  21.     @Override
  22.     public void map(Object o, Text in, Context ctx)
  23.             throws IOException, InterruptedException
  24.     {
  25.         for (String word : in.toString().toLowerCase().split("[ \n\t.,;:!?]+")) {
  26.             ctx.write(new Text(word), NullWritable.get());
  27.         }      
  28.     }
  29.    
  30. }
  31.  
  32. class SubstringCountReducer
  33. extends Reducer<Text, NullWritable, Text, IntWritable> {
  34.  
  35.     @Override
  36.     public void reduce(Text str, Iterable<NullWritable> nulls,
  37.                         Context ctx)
  38.     throws IOException, InterruptedException {
  39.        
  40.         if (nulls instanceof Collection) {
  41.             ctx.write(str, new IntWritable(((Collection<?>)nulls).size()));
  42.         } else {
  43.             int total = 0;
  44.             for (Object o : nulls) {
  45.                 total++;
  46.             }
  47.             ctx.write(str, new IntWritable(total));
  48.         }
  49.     }
  50.    
  51. }
  52.    
  53. public class DriverWordCount {
  54.  
  55.    
  56.    
  57.     public static void main(String[] args)
  58.    
  59.     {
  60.         if (args.length < 2) System.exit(2);
  61.        
  62.         try {
  63.            
  64.             //Création d'une nouvelle configuration
  65.             Configuration conf = new Configuration();
  66.  
  67.             Job job = Job.getInstance(conf, "tdd.tp03");
  68.            
  69.                    
  70.            
  71.             //Le nom de la classe 'main'
  72.             job.setJarByClass(DriverWordCount.class);
  73.  
  74.             //Le format d'entrée du Map
  75.             //TextInputFormat est un alias pour
  76.             //InputFormat<LongWritable, Text>;
  77.            
  78.             job.setInputFormatClass(TextInputFormat.class);
  79.            
  80.             //Initialisation du Map
  81.             job.setMapperClass(SubstringCountMapper.class);
  82.            
  83.             //Initialisation du Reduce
  84.             job.setReducerClass(SubstringCountReducer.class);
  85.  
  86.             //Sortie du Map
  87.             job.setMapOutputKeyClass(Text.class);
  88.             job.setMapOutputValueClass(NullWritable.class);
  89.            
  90.             //Sortie (du Reduce)
  91.             job.setOutputKeyClass(Text.class);
  92.             job.setOutputValueClass(IntWritable.class);
  93.                        
  94.             FileOutputFormat.setOutputPath(job, new Path(args[0]));
  95.            
  96.             for(int i = 1; i < args.length; i++)
  97.                 FileInputFormat.addInputPath(job, new Path(args[i]));
  98.  
  99.            
  100.                        
  101.            
  102.             System.exit(job.waitForCompletion(true) ?  0 : 1);
  103.            
  104.         } catch (Exception e) {
  105.             System.err.println("Erreur : " + e);
  106.         }
  107.        
  108.     }
  109.    
  110. }
Advertisement
Add Comment
Please, Sign In to add comment