Advertisement
Guest User

DomainGenderCount.java

a guest
Jul 12th, 2018
337
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.55 KB | None | 0 0
  1. package peoplemail;
  2. import java.io.IOException;
  3. import java.util.Iterator;
  4.  
  5. import org.apache.hadoop.conf.Configuration;
  6. import org.apache.hadoop.conf.Configured;
  7. import org.apache.hadoop.fs.Path;
  8. import org.apache.hadoop.io.IntWritable;
  9. import org.apache.hadoop.io.LongWritable;
  10. import org.apache.hadoop.io.Text;
  11. import org.apache.hadoop.mapred.FileInputFormat;
  12. import org.apache.hadoop.mapred.FileOutputFormat;
  13. import org.apache.hadoop.mapred.JobClient;
  14. import org.apache.hadoop.mapred.JobConf;
  15. import org.apache.hadoop.mapred.MapReduceBase;
  16. import org.apache.hadoop.mapred.Mapper;
  17. import org.apache.hadoop.mapred.OutputCollector;
  18. import org.apache.hadoop.mapred.Reducer;
  19. import org.apache.hadoop.mapred.Reporter;
  20. import org.apache.hadoop.util.Tool;
  21. import org.apache.hadoop.util.ToolRunner;
  22.  
  23.  
  24.  
  25. public class DomainGenderCount extends Configured implements Tool {
  26.    
  27.     public static class MapClass
  28.     extends MapReduceBase implements Mapper<LongWritable, Text, Text, Text>{
  29.         @Override
  30.         public void map(LongWritable  key,Text value,
  31.                 OutputCollector<Text,Text> output, Reporter r)throws IOException{
  32.            
  33.             String fields[] = value.toString().split(",");
  34.             String gender = fields[5];
  35.             String domain = fields[3].split("@")[1];
  36.             output.collect(new Text(domain), new Text(gender));
  37.         }
  38.        
  39.     }
  40.    
  41.     public static class ReduceClass
  42.     extends MapReduceBase implements Reducer<Text, Text, Text, IntWritable>{
  43.         @Override
  44.         public void reduce(Text key, Iterator<Text> value,
  45.                 OutputCollector<Text,IntWritable> output, Reporter r)throws IOException{
  46.            
  47.             int count=0;
  48.             while(value.hasNext()){
  49.                 value.next();
  50.                 count++;
  51.             }
  52.             output.collect(key, new IntWritable(count));
  53.            
  54.         }
  55.        
  56.     }
  57.    
  58.    
  59.     public static void main(String[] args) throws Exception{
  60.        
  61.         System.exit(ToolRunner.run(new Configuration(), new DomainGenderCount(), args));
  62.     }
  63.     public int run(String[] paths) throws Exception {
  64.        
  65.         JobConf jobConf = new JobConf(getConf(), DomainGenderCount.class);
  66.         jobConf.setMapOutputKeyClass(Text.class);
  67.         jobConf.setMapOutputValueClass(Text.class);
  68.        
  69.         jobConf.setJobName("Number of Users in each domain:");
  70.        
  71.         jobConf.setOutputKeyClass(Text.class);
  72.         jobConf.setOutputValueClass(IntWritable.class);
  73.        
  74.         jobConf.setMapperClass(MapClass.class);
  75.         jobConf.setReducerClass(ReduceClass.class);
  76.         jobConf.setCombinerClass(ReduceClass.class);
  77.        
  78.         FileInputFormat.setInputPaths(jobConf, new Path(paths[0]));
  79.         FileOutputFormat.setOutputPath(jobConf, new Path(paths[1]));
  80.        
  81.         JobClient.runJob(jobConf);
  82.         return 0;
  83. }
  84.  
  85.  
  86.  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement