| 1 | |
| 2 | |
| 3 | {{{ |
| 4 | #!java |
| 5 | |
| 6 | import java.io.IOException; |
| 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.LongWritable; |
| 12 | import org.apache.hadoop.io.Text; |
| 13 | import org.apache.hadoop.mapreduce.Job; |
| 14 | import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; |
| 15 | import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; |
| 16 | |
| 17 | public class ThreadGo extends Thread { |
| 18 | Configuration conf ; |
| 19 | Job job ; |
| 20 | long StartTime ; |
| 21 | public ThreadGo() { |
| 22 | conf = new Configuration(); |
| 23 | // conf.set("mapred.job.tracker", "local"); // for single |
| 24 | // conf.set("fs.default.name", "file:///"); // for single |
| 25 | StartTime = System.currentTimeMillis(); |
| 26 | } |
| 27 | |
| 28 | public void run() // throws Exception |
| 29 | { |
| 30 | try { |
| 31 | System.err.println(conf.get("fs.default.name")); |
| 32 | job.waitForCompletion(true); |
| 33 | System.out.println("[final] "+ job.getJobName() +" run" + (System.currentTimeMillis() - StartTime) / 1E+3 +" secs."); |
| 34 | |
| 35 | } catch (IOException e) { |
| 36 | System.err.println("[Error] " + job.getJobName() + " = IOException"); |
| 37 | System.err.println(e.getMessage()); |
| 38 | } catch (InterruptedException e) { |
| 39 | System.err.println("[Error] " + job.getJobName() + " = InterruptedException" ); |
| 40 | System.err.println(e.getMessage()); |
| 41 | } catch (ClassNotFoundException e) { |
| 42 | System.err.println("[Error] " + job.getJobName() + " ClassNotFoundException"); |
| 43 | System.err.println(e.getMessage()); |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | public void SetJob1(String input, String output) throws Exception { |
| 48 | job = new Job(conf, "Th1_HelloWorld"); |
| 49 | job.setJarByClass(HelloHadoopV2.class); |
| 50 | job.setMapperClass(HelloMapperV2.class); |
| 51 | |
| 52 | job.setCombinerClass(HelloReducerV2.class); |
| 53 | job.setReducerClass(HelloReducerV2.class); |
| 54 | job.setOutputKeyClass(LongWritable.class); |
| 55 | job.setOutputValueClass(Text.class); |
| 56 | FileInputFormat.setInputPaths(job, input); |
| 57 | FileOutputFormat.setOutputPath(job, new Path(output)); |
| 58 | } |
| 59 | |
| 60 | public void SetJob2(String input, String output) throws Exception { |
| 61 | // JobConf conf = new JobConf(DBWordCount.class); |
| 62 | job = new Job(conf, "Th2_WordCount"); |
| 63 | job.setJarByClass(WordCount.class); |
| 64 | job.setMapperClass(WordCount.TokenizerMapper.class); |
| 65 | job.setCombinerClass(WordCount.IntSumReducer.class); |
| 66 | job.setReducerClass(WordCount.IntSumReducer.class); |
| 67 | job.setOutputKeyClass(Text.class); |
| 68 | job.setOutputValueClass(IntWritable.class); |
| 69 | FileInputFormat.addInputPath(job, new Path(input)); |
| 70 | FileOutputFormat.setOutputPath(job, new Path(output)); |
| 71 | } |
| 72 | |
| 73 | public static void main(String[] args) throws Exception { |
| 74 | |
| 75 | long StartTime = System.currentTimeMillis(); |
| 76 | ThreadGo thread1 = new ThreadGo(); |
| 77 | thread1.SetJob1("./input1","./output1"); |
| 78 | |
| 79 | ThreadGo thread2 = new ThreadGo(); |
| 80 | thread2.SetJob2("./input2","./output2"); |
| 81 | |
| 82 | thread1.start(); |
| 83 | thread2.start(); |
| 84 | System.out.println("[main] "+ (System.currentTimeMillis() - StartTime) / 1E+3 +" secs."); |
| 85 | |
| 86 | } |
| 87 | } |
| 88 | |
| 89 | |
| 90 | }}} |