source: proiecte/HadoopJUnit/hadoop-0.20.1/src/test/testshell/ExternalMapReduce.java @ 141

Last change on this file since 141 was 120, checked in by (none), 14 years ago

Added the mail files for the Hadoop JUNit Project

  • Property svn:executable set to *
File size: 4.4 KB
Line 
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
19package testshell;
20
21import java.io.IOException;
22import java.util.Iterator;
23
24import org.apache.hadoop.conf.Configuration;
25import org.apache.hadoop.conf.Configured;
26import org.apache.hadoop.fs.Path;
27import org.apache.hadoop.io.IntWritable;
28import org.apache.hadoop.io.Writable;
29import org.apache.hadoop.io.WritableComparable;
30import org.apache.hadoop.mapred.FileInputFormat;
31import org.apache.hadoop.mapred.FileOutputFormat;
32import org.apache.hadoop.mapred.JobClient;
33import org.apache.hadoop.mapred.JobConf;
34import org.apache.hadoop.mapred.MapReduceBase;
35import org.apache.hadoop.mapred.Mapper;
36import org.apache.hadoop.mapred.OutputCollector;
37import org.apache.hadoop.mapred.Reducer;
38import org.apache.hadoop.mapred.Reporter;
39import org.apache.hadoop.util.Tool;
40import org.apache.hadoop.util.ToolRunner;
41
42/**
43 * will be in an external jar and used for
44 * test in TestJobShell.java.
45 */
46public 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}
Note: See TracBrowser for help on using the repository browser.