SHARE
TWEET

Untitled

a guest Oct 12th, 2017 47 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.io.IOException;
  2. import java.util.StringTokenizer;
  3. import java.util.TreeMap;
  4.  
  5. import org.apache.commons.lang3.StringUtils;
  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.LongWritable;
  10. import org.apache.hadoop.io.NullWritable;
  11. import org.apache.hadoop.io.Text;
  12. import org.apache.hadoop.mapreduce.Job;
  13. import org.apache.hadoop.mapreduce.Mapper;
  14. import org.apache.hadoop.mapreduce.Reducer;
  15. import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
  16. import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
  17.  
  18. public class Transpose {
  19.  
  20.     public static class TransposeMapper extends Mapper<LongWritable, Text, LongWritable, Text> {
  21.         @Override
  22.         protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException {
  23.             long column = 0;
  24.             long somethingLikeRow = key.get();
  25.             for (String num : value.toString().split(",")) {
  26.                 context.write(new LongWritable(column), new Text(somethingLikeRow + "\t" + num));
  27.                 ++column;
  28.             }
  29.         }
  30.     }
  31.  
  32.     public static class TransposeReducer extends Reducer<LongWritable, Text, Text, NullWritable> {
  33.         @Override
  34.         protected void reduce(LongWritable key, Iterable<Text> values, Context context) throws IOException, InterruptedException {
  35.             TreeMap<Long, String> row = new TreeMap<Long, String>(); // storing values sorted by positions in input file
  36.             for (Text text : values) {
  37.                 String[] parts = text.toString().split("\t"); // somethingLikeRow, value
  38.                 row.put(Long.valueOf(parts[0]), parts[1]);
  39.             }
  40.             String rowString = StringUtils.join(row.values(), ' '); // i'm using org.apache.commons library for concatenation
  41.             context.write(new Text(rowString), NullWritable.get());
  42.         }
  43.     }
  44.  
  45.     public static void main(String[] args) throws Exception {
  46.         Configuration conf = new Configuration();
  47.         Job job = Job.getInstance(conf, "Transpose");
  48.         job.setJarByClass(Transpose.class);
  49.         job.setMapperClass(TransposeMapper.class);
  50.         job.setCombinerClass(TransposeReducer.class);
  51.         job.setReducerClass(TransposeReducer.class);
  52.         job.setOutputKeyClass(LongWritable.class);
  53.         job.setOutputValueClass(Text.class);
  54.         FileInputFormat.addInputPath(job, new Path(args[0]));
  55.         FileOutputFormat.setOutputPath(job, new Path(args[1]));
  56.         System.exit(job.waitForCompletion(true) ? 0 : 1);
  57.     }
  58. }
RAW Paste Data
Top