Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.ArrayList;
- import java.io.IOException;
- import java.util.StringTokenizer;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import org.apache.hadoop.conf.Configuration;
- import org.apache.hadoop.fs.Path;
- import org.apache.hadoop.io.IntWritable;
- import org.apache.hadoop.io.Text;
- import org.apache.hadoop.mapreduce.Job;
- import org.apache.hadoop.mapreduce.Mapper;
- import org.apache.hadoop.mapreduce.Reducer;
- import org.apache.hadoop.mapreduce.lib.input.FileInputFormat;
- import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat;
- import org.apache.hadoop.mapreduce.lib.input.FileSplit;
- import org.apache.log4j.Logger;
- public class ExecutionTimeTracker {
- public static class TokenizerMapper
- extends Mapper<Object, Text, Text, IntWritable>{
- private final static IntWritable one = new IntWritable(1);
- private Text word = new Text();
- private final Logger LOG = org.apache.log4j.Logger.getLogger(this.getClass());
- public void map(Object key, Text value, Context context
- ) throws IOException, InterruptedException {
- StringTokenizer itr = new StringTokenizer(value.toString());
- FileSplit fileSplit = (FileSplit)context.getInputSplit();
- String filename = fileSplit.getPath().getName();
- //to write the file name as key
- Text text = new Text();
- text.set(filename);
- LOG.warn("fileName: " + filename);
- try {
- // command execution
- Runtime rt = Runtime.getRuntime();
- String evmDir = "evm";
- String command = evmDir + " --debug --code " + value.toString() + " run";
- Process proc = Runtime.getRuntime().exec(command);
- BufferedReader stdInput = new BufferedReader(new InputStreamReader(proc.getInputStream()));
- // output data struct
- ArrayList<String> consoleOutput = new ArrayList<String>();
- String s = null;
- while ((s = stdInput.readLine()) != null) {
- consoleOutput.add(s);
- }
- for (String p : consoleOutput) {
- Pattern pattern = Pattern.compile("([A-Za-z]+)([ \t]+)(\\d+)");
- Matcher matcher = pattern.matcher(p);
- while (matcher.find()) {
- String groupThree = matcher.group(3);
- IntWritable writeValue = new IntWritable(Integer.parseInt(groupThree));
- context.write(text, writeValue);
- }
- }
- } catch (IOException e) {
- LOG.warn("Exception Encountered!");
- LOG.warn(e);
- }
- }
- }
- public static class IntSumReducer
- extends Reducer<Text,IntWritable,Text,IntWritable> {
- private IntWritable result = new IntWritable();
- public void reduce(Text key, Iterable<IntWritable> values,
- Context context
- ) throws IOException, InterruptedException {
- int sum = 0;
- for (IntWritable val : values) {
- sum += val.get();
- }
- result.set(sum);
- context.write(key, result);
- }
- }
- public static void main(String[] args) throws Exception {
- Configuration conf = new Configuration();
- Job job = Job.getInstance(conf, "execution time tracker");
- job.setJarByClass(ExecutionTimeTracker.class);
- job.setMapperClass(TokenizerMapper.class);
- job.setCombinerClass(IntSumReducer.class);
- job.setReducerClass(IntSumReducer.class);
- job.setOutputKeyClass(Text.class);
- job.setOutputValueClass(IntWritable.class);
- FileInputFormat.addInputPath(job, new Path(args[0]));
- FileOutputFormat.setOutputPath(job, new Path(args[1] + "/" + System.currentTimeMillis()));
- System.exit(job.waitForCompletion(true) ? 0 : 1);
- }
- }
Add Comment
Please, Sign In to add comment