Advertisement
Guest User

MapRed v2

a guest
Dec 19th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.69 KB | None | 0 0
  1. // https://www.edureka.co/blog/mapreduce-example-reduce-side-join/
  2. import java.io.IOException;
  3.  
  4. import org.apache.hadoop.conf.Configuration;
  5. import org.apache.hadoop.fs.Path;
  6. import org.apache.hadoop.io.Text;
  7. import org.apache.hadoop.mapreduce.Job;
  8. import org.apache.hadoop.mapreduce.Mapper;
  9. import org.apache.hadoop.mapreduce.Reducer;
  10. import org.apache.hadoop.mapreduce.lib.input.MultipleInputs;
  11. import org.apache.hadoop.mapreduce.lib.input.TextInputFormat;
  12. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  13.  
  14. public class custTrans {
  15.    
  16.     public static class mapCustDetails extends Mapper<Object, Text, Text, Text> {
  17.        
  18.         public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  19.             String record = value.toString();
  20.             String[] parts = record.split(",");
  21.             context.write(new Text(parts[0]), new Text("cust\t" + parts[1]));
  22.         }
  23.     }
  24.    
  25.     public static class mapTransDetails extends Mapper<Object, Text, Text, Text> {
  26.        
  27.         public void map(Object key, Text value, Context context) throws IOException, InterruptedException {
  28.             String record = value.toString();
  29.             String[] parts = record.split(",");
  30.             context.write(new Text(parts[2]), new Text("trans\t" + parts[3]));
  31.         }
  32.     }
  33.    
  34.     public static class transReducer extends Reducer<Text, Text, Text, Text> {
  35.        
  36.         public void reduce(Text key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
  37.             String name = "";
  38.             int count = 0;
  39.             double amount = 0.0;
  40.            
  41.             for(Text value : values) {
  42.                 String parts[] = value.toString().split("\t");
  43.                
  44.                 if(parts[0].equals("trans")) {
  45.                     count++;
  46.                     amount += Float.parseFloat(parts[1]);
  47.                 } else if(parts[0].equals("cust")) {
  48.                     name = parts[1];
  49.                 }
  50.                
  51.                 String str = String.format("%d\t%f", count, amount);
  52.                 context.write(new Text(name), new Text(str));
  53.             }
  54.         }
  55.     }
  56.    
  57.     @SuppressWarnings("deprecation")
  58.     public static void main(String[] args) throws IOException, ClassNotFoundException, InterruptedException {
  59.        
  60.         Configuration con = new Configuration();
  61.        
  62.         Job job = new Job(con, "Sports Transactions");
  63.        
  64.         job.setJarByClass(custTrans.class);
  65.         job.setReducerClass(transReducer.class);
  66.         job.setOutputKeyClass(Text.class);
  67.         job.setOutputValueClass(Text.class);
  68.        
  69.         MultipleInputs.addInputPath(job, new Path(args[0]), TextInputFormat.class, mapCustDetails.class);
  70.         MultipleInputs.addInputPath(job, new Path(args[1]), TextInputFormat.class, mapTransDetails.class);
  71.          
  72.         Path outputPath = new Path(args[2]);
  73.          
  74.         FileOutputFormat.setOutputPath(job, outputPath);
  75.         outputPath.getFileSystem(con).delete(outputPath);
  76.         System.exit(job.waitForCompletion(true) ? 0 : 1);
  77.        
  78.     }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement