Guest User

Untitled

a guest
Oct 19th, 2017
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.64 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.ArrayList;
  3. import java.io.IOException;
  4. import java.util.StringTokenizer;
  5. import java.util.regex.Matcher;
  6. import java.util.regex.Pattern;
  7.  
  8. import org.apache.hadoop.conf.Configuration;
  9. import org.apache.hadoop.fs.Path;
  10. import org.apache.hadoop.io.IntWritable;
  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. import org.apache.hadoop.mapreduce.lib.input.FileSplit;
  19. import org.apache.log4j.Logger;
  20.  
  21. public class ExecutionTimeTracker {
  22.  
  23. public static class TokenizerMapper
  24. extends Mapper<Object, Text, Text, IntWritable>{
  25.  
  26. private final static IntWritable one = new IntWritable(1);
  27. private Text word = new Text();
  28. private final Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
  29.  
  30. public void map(Object key, Text value, Context context
  31. ) throws IOException, InterruptedException {
  32. StringTokenizer itr = new StringTokenizer(value.toString());
  33.  
  34. FileSplit fileSplit = (FileSplit)context.getInputSplit();
  35. String filename = fileSplit.getPath().getName();
  36.  
  37. //to write the file name as key
  38. Text text = new Text();
  39. text.set(filename);
  40. LOG.warn("fileName: " + filename);
  41.  
  42. try {
  43. // command execution
  44. Runtime rt = Runtime.getRuntime();
  45. String evmDir = "evm";
  46. String command = evmDir + " --debug --code " + value.toString() + " run";
  47. Process proc = Runtime.getRuntime().exec(command);
  48. BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
  49.  
  50. // output data struct
  51. ArrayList<String> consoleOutput = new ArrayList<String>();
  52. String s = null;
  53. while ((s = stdInput.readLine()) != null) {
  54. consoleOutput.add(s);
  55. }
  56. for (String p : consoleOutput) {
  57. Pattern pattern = Pattern.compile("([A-Za-z]+)([ \t]+)(\\d+)");
  58. Matcher matcher = pattern.matcher(p);
  59. while (matcher.find()) {
  60. String groupThree = matcher.group(3);
  61. IntWritable writeValue = new IntWritable(Integer.parseInt(groupThree));
  62. context.write(text, writeValue);
  63. }
  64. }
  65. } catch (IOException e) {
  66. LOG.warn("Exception Encountered!");
  67. LOG.warn(e);
  68. }
  69. }
  70. }
  71.  
  72. public static class IntSumReducer
  73. extends Reducer<Text,IntWritable,Text,IntWritable> {
  74. private IntWritable result = new IntWritable();
  75.  
  76. public void reduce(Text key, Iterable<IntWritable> values,
  77. Context context
  78. ) throws IOException, InterruptedException {
  79. int sum = 0;
  80. for (IntWritable val : values) {
  81. sum += val.get();
  82. }
  83. result.set(sum);
  84. context.write(key, result);
  85. }
  86. }
  87.  
  88. public static void main(String[] args) throws Exception {
  89. Configuration conf = new Configuration();
  90. Job job = Job.getInstance(conf, "execution time tracker");
  91. job.setJarByClass(ExecutionTimeTracker.class);
  92. job.setMapperClass(TokenizerMapper.class);
  93. job.setCombinerClass(IntSumReducer.class);
  94. job.setReducerClass(IntSumReducer.class);
  95. job.setOutputKeyClass(Text.class);
  96. job.setOutputValueClass(IntWritable.class);
  97. FileInputFormat.addInputPath(job, new Path(args[0]));
  98. FileOutputFormat.setOutputPath(job, new Path(args[1] + "/" + System.currentTimeMillis()));
  99. System.exit(job.waitForCompletion(true) ? 0 : 1);
  100. }
  101. }
Add Comment
Please, Sign In to add comment