[120] | 1 | /** |
---|
| 2 | * Licensed to the Apache Software Foundation (ASF) under one |
---|
| 3 | * or more contributor license agreements. See the NOTICE file |
---|
| 4 | * distributed with this work for additional information |
---|
| 5 | * regarding copyright ownership. The ASF licenses this file |
---|
| 6 | * to you under the Apache License, Version 2.0 (the |
---|
| 7 | * "License"); you may not use this file except in compliance |
---|
| 8 | * with the License. You may obtain a copy of the License at |
---|
| 9 | * |
---|
| 10 | * http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 11 | * |
---|
| 12 | * Unless required by applicable law or agreed to in writing, software |
---|
| 13 | * distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 14 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 15 | * See the License for the specific language governing permissions and |
---|
| 16 | * limitations under the License. |
---|
| 17 | */ |
---|
| 18 | |
---|
| 19 | package testshell; |
---|
| 20 | |
---|
| 21 | import java.io.IOException; |
---|
| 22 | import java.util.Iterator; |
---|
| 23 | |
---|
| 24 | import org.apache.hadoop.conf.Configuration; |
---|
| 25 | import org.apache.hadoop.conf.Configured; |
---|
| 26 | import org.apache.hadoop.fs.Path; |
---|
| 27 | import org.apache.hadoop.io.IntWritable; |
---|
| 28 | import org.apache.hadoop.io.Writable; |
---|
| 29 | import org.apache.hadoop.io.WritableComparable; |
---|
| 30 | import org.apache.hadoop.mapred.FileInputFormat; |
---|
| 31 | import org.apache.hadoop.mapred.FileOutputFormat; |
---|
| 32 | import org.apache.hadoop.mapred.JobClient; |
---|
| 33 | import org.apache.hadoop.mapred.JobConf; |
---|
| 34 | import org.apache.hadoop.mapred.MapReduceBase; |
---|
| 35 | import org.apache.hadoop.mapred.Mapper; |
---|
| 36 | import org.apache.hadoop.mapred.OutputCollector; |
---|
| 37 | import org.apache.hadoop.mapred.Reducer; |
---|
| 38 | import org.apache.hadoop.mapred.Reporter; |
---|
| 39 | import org.apache.hadoop.util.Tool; |
---|
| 40 | import org.apache.hadoop.util.ToolRunner; |
---|
| 41 | |
---|
| 42 | /** |
---|
| 43 | * will be in an external jar and used for |
---|
| 44 | * test in TestJobShell.java. |
---|
| 45 | */ |
---|
| 46 | public class ExternalMapReduce extends Configured implements Tool { |
---|
| 47 | |
---|
| 48 | public void configure(JobConf job) { |
---|
| 49 | // do nothing |
---|
| 50 | } |
---|
| 51 | |
---|
| 52 | public void close() |
---|
| 53 | throws IOException { |
---|
| 54 | |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | public static class MapClass extends MapReduceBase |
---|
| 58 | implements Mapper<WritableComparable, Writable, |
---|
| 59 | WritableComparable, IntWritable> { |
---|
| 60 | public void map(WritableComparable key, Writable value, |
---|
| 61 | OutputCollector<WritableComparable, IntWritable> output, |
---|
| 62 | Reporter reporter) |
---|
| 63 | throws IOException { |
---|
| 64 | //check for classpath |
---|
| 65 | String classpath = System.getProperty("java.class.path"); |
---|
| 66 | if (classpath.indexOf("testjob.jar") == -1) { |
---|
| 67 | throw new IOException("failed to find in the library " + classpath); |
---|
| 68 | } |
---|
| 69 | //fork off ls to see if the file exists. |
---|
| 70 | // java file.exists() will not work on |
---|
| 71 | // cygwin since it is a symlink |
---|
| 72 | String[] argv = new String[2]; |
---|
| 73 | argv[0] = "ls"; |
---|
| 74 | argv[1] = "files_tmp"; |
---|
| 75 | Process p = Runtime.getRuntime().exec(argv); |
---|
| 76 | int ret = -1; |
---|
| 77 | try { |
---|
| 78 | ret = p.waitFor(); |
---|
| 79 | } catch(InterruptedException ie) { |
---|
| 80 | //do nothing here. |
---|
| 81 | } |
---|
| 82 | if (ret != 0) { |
---|
| 83 | throw new IOException("files_tmp does not exist"); |
---|
| 84 | } |
---|
| 85 | } |
---|
| 86 | } |
---|
| 87 | |
---|
| 88 | public static class Reduce extends MapReduceBase |
---|
| 89 | implements Reducer<WritableComparable, Writable, |
---|
| 90 | WritableComparable, IntWritable> { |
---|
| 91 | public void reduce(WritableComparable key, Iterator<Writable> values, |
---|
| 92 | OutputCollector<WritableComparable, IntWritable> output, |
---|
| 93 | Reporter reporter) |
---|
| 94 | throws IOException { |
---|
| 95 | //do nothing |
---|
| 96 | } |
---|
| 97 | } |
---|
| 98 | |
---|
| 99 | public int run(String[] argv) throws IOException { |
---|
| 100 | if (argv.length < 2) { |
---|
| 101 | System.out.println("ExternalMapReduce <input> <output>"); |
---|
| 102 | return -1; |
---|
| 103 | } |
---|
| 104 | Path outDir = new Path(argv[1]); |
---|
| 105 | Path input = new Path(argv[0]); |
---|
| 106 | JobConf testConf = new JobConf(getConf(), ExternalMapReduce.class); |
---|
| 107 | |
---|
| 108 | //try to load a class from libjar |
---|
| 109 | try { |
---|
| 110 | testConf.getClassByName("testjar.ClassWordCount"); |
---|
| 111 | } catch (ClassNotFoundException e) { |
---|
| 112 | System.out.println("Could not find class from libjar"); |
---|
| 113 | return -1; |
---|
| 114 | } |
---|
| 115 | |
---|
| 116 | |
---|
| 117 | testConf.setJobName("external job"); |
---|
| 118 | FileInputFormat.setInputPaths(testConf, input); |
---|
| 119 | FileOutputFormat.setOutputPath(testConf, outDir); |
---|
| 120 | testConf.setMapperClass(MapClass.class); |
---|
| 121 | testConf.setReducerClass(Reduce.class); |
---|
| 122 | testConf.setNumReduceTasks(1); |
---|
| 123 | JobClient.runJob(testConf); |
---|
| 124 | return 0; |
---|
| 125 | } |
---|
| 126 | |
---|
| 127 | public static void main(String[] args) throws Exception { |
---|
| 128 | int res = ToolRunner.run(new Configuration(), |
---|
| 129 | new ExternalMapReduce(), args); |
---|
| 130 | System.exit(res); |
---|
| 131 | } |
---|
| 132 | } |
---|