001/**
002 * Licensed to the Apache Software Foundation (ASF) under one
003 * or more contributor license agreements.  See the NOTICE file
004 * distributed with this work for additional information
005 * regarding copyright ownership.  The ASF licenses this file
006 * to you under the Apache License, Version 2.0 (the
007 * "License"); you may not use this file except in compliance
008 * with the License.  You may obtain a copy of the License at
009 *
010 *     http://www.apache.org/licenses/LICENSE-2.0
011 *
012 * Unless required by applicable law or agreed to in writing, software
013 * distributed under the License is distributed on an "AS IS" BASIS,
014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015 * See the License for the specific language governing permissions and
016 * limitations under the License.
017 */
018
019package org.apache.hadoop.mapred.lib.aggregate;
020
021import java.io.IOException;
022import java.util.ArrayList;
023
024import org.apache.hadoop.classification.InterfaceAudience;
025import org.apache.hadoop.classification.InterfaceStability;
026import org.apache.hadoop.conf.Configuration;
027import org.apache.hadoop.fs.Path;
028import org.apache.hadoop.io.Text;
029import org.apache.hadoop.mapred.FileInputFormat;
030import org.apache.hadoop.mapred.FileOutputFormat;
031import org.apache.hadoop.mapred.InputFormat;
032import org.apache.hadoop.mapred.JobClient;
033import org.apache.hadoop.mapred.JobConf;
034import org.apache.hadoop.mapred.SequenceFileInputFormat;
035import org.apache.hadoop.mapred.TextInputFormat;
036import org.apache.hadoop.mapred.TextOutputFormat;
037import org.apache.hadoop.mapred.jobcontrol.Job;
038import org.apache.hadoop.mapred.jobcontrol.JobControl;
039import org.apache.hadoop.util.GenericOptionsParser;
040
041/**
042 * This is the main class for creating a map/reduce job using Aggregate
043 * framework. The Aggregate is a specialization of map/reduce framework,
044 * specilizing for performing various simple aggregations.
045 * 
046 * Generally speaking, in order to implement an application using Map/Reduce
047 * model, the developer is to implement Map and Reduce functions (and possibly
048 * combine function). However, a lot of applications related to counting and
049 * statistics computing have very similar characteristics. Aggregate abstracts
050 * out the general patterns of these functions and implementing those patterns.
051 * In particular, the package provides generic mapper/redducer/combiner classes,
052 * and a set of built-in value aggregators, and a generic utility class that
053 * helps user create map/reduce jobs using the generic class. The built-in
054 * aggregators include:
055 * 
056 * sum over numeric values count the number of distinct values compute the
057 * histogram of values compute the minimum, maximum, media,average, standard
058 * deviation of numeric values
059 * 
060 * The developer using Aggregate will need only to provide a plugin class
061 * conforming to the following interface:
062 * 
063 * public interface ValueAggregatorDescriptor { public ArrayList<Entry>
064 * generateKeyValPairs(Object key, Object value); public void
065 * configure(JobConfjob); }
066 * 
067 * The package also provides a base class, ValueAggregatorBaseDescriptor,
068 * implementing the above interface. The user can extend the base class and
069 * implement generateKeyValPairs accordingly.
070 * 
071 * The primary work of generateKeyValPairs is to emit one or more key/value
072 * pairs based on the input key/value pair. The key in an output key/value pair
073 * encode two pieces of information: aggregation type and aggregation id. The
074 * value will be aggregated onto the aggregation id according the aggregation
075 * type.
076 * 
077 * This class offers a function to generate a map/reduce job using Aggregate
078 * framework. The function takes the following parameters: input directory spec
079 * input format (text or sequence file) output directory a file specifying the
080 * user plugin class
081 */
082@InterfaceAudience.Public
083@InterfaceStability.Stable
084public class ValueAggregatorJob {
085
086  public static JobControl createValueAggregatorJobs(String args[]
087    , Class<? extends ValueAggregatorDescriptor>[] descriptors) throws IOException {
088    
089    JobControl theControl = new JobControl("ValueAggregatorJobs");
090    ArrayList<Job> dependingJobs = new ArrayList<Job>();
091    JobConf aJobConf = createValueAggregatorJob(args);
092    if(descriptors != null)
093      setAggregatorDescriptors(aJobConf, descriptors);
094    Job aJob = new Job(aJobConf, dependingJobs);
095    theControl.addJob(aJob);
096    return theControl;
097  }
098
099  public static JobControl createValueAggregatorJobs(String args[]) throws IOException {
100    return createValueAggregatorJobs(args, null);
101  }
102  
103  /**
104   * Create an Aggregate based map/reduce job.
105   * 
106   * @param args the arguments used for job creation. Generic hadoop
107   * arguments are accepted.
108   * @return a JobConf object ready for submission.
109   * 
110   * @throws IOException
111   * @see GenericOptionsParser
112   */
113  public static JobConf createValueAggregatorJob(String args[])
114    throws IOException {
115
116    Configuration conf = new Configuration();
117    
118    GenericOptionsParser genericParser 
119      = new GenericOptionsParser(conf, args);
120    args = genericParser.getRemainingArgs();
121    
122    if (args.length < 2) {
123      System.out.println("usage: inputDirs outDir "
124          + "[numOfReducer [textinputformat|seq [specfile [jobName]]]]");
125      GenericOptionsParser.printGenericCommandUsage(System.out);
126      System.exit(1);
127    }
128    String inputDir = args[0];
129    String outputDir = args[1];
130    int numOfReducers = 1;
131    if (args.length > 2) {
132      numOfReducers = Integer.parseInt(args[2]);
133    }
134
135    Class<? extends InputFormat> theInputFormat =
136      TextInputFormat.class;
137    if (args.length > 3 && 
138        args[3].compareToIgnoreCase("textinputformat") == 0) {
139      theInputFormat = TextInputFormat.class;
140    } else {
141      theInputFormat = SequenceFileInputFormat.class;
142    }
143
144    Path specFile = null;
145
146    if (args.length > 4) {
147      specFile = new Path(args[4]);
148    }
149
150    String jobName = "";
151    
152    if (args.length > 5) {
153      jobName = args[5];
154    }
155    
156    JobConf theJob = new JobConf(conf);
157    if (specFile != null) {
158      theJob.addResource(specFile);
159    }
160    String userJarFile = theJob.get("user.jar.file");
161    if (userJarFile == null) {
162      theJob.setJarByClass(ValueAggregator.class);
163    } else {
164      theJob.setJar(userJarFile);
165    }
166    theJob.setJobName("ValueAggregatorJob: " + jobName);
167
168    FileInputFormat.addInputPaths(theJob, inputDir);
169
170    theJob.setInputFormat(theInputFormat);
171    
172    theJob.setMapperClass(ValueAggregatorMapper.class);
173    FileOutputFormat.setOutputPath(theJob, new Path(outputDir));
174    theJob.setOutputFormat(TextOutputFormat.class);
175    theJob.setMapOutputKeyClass(Text.class);
176    theJob.setMapOutputValueClass(Text.class);
177    theJob.setOutputKeyClass(Text.class);
178    theJob.setOutputValueClass(Text.class);
179    theJob.setReducerClass(ValueAggregatorReducer.class);
180    theJob.setCombinerClass(ValueAggregatorCombiner.class);
181    theJob.setNumMapTasks(1);
182    theJob.setNumReduceTasks(numOfReducers);
183    return theJob;
184  }
185
186  public static JobConf createValueAggregatorJob(String args[]
187    , Class<? extends ValueAggregatorDescriptor>[] descriptors)
188  throws IOException {
189    JobConf job = createValueAggregatorJob(args);
190    setAggregatorDescriptors(job, descriptors);
191    return job;
192  }
193  
194  public static void setAggregatorDescriptors(JobConf job
195      , Class<? extends ValueAggregatorDescriptor>[] descriptors) {
196    job.setInt("aggregator.descriptor.num", descriptors.length);
197    //specify the aggregator descriptors
198    for(int i=0; i< descriptors.length; i++) {
199      job.set("aggregator.descriptor." + i, "UserDefined," + descriptors[i].getName());
200    }    
201  }
202  
203  /**
204   * create and run an Aggregate based map/reduce job.
205   * 
206   * @param args the arguments used for job creation
207   * @throws IOException
208   */
209  public static void main(String args[]) throws IOException {
210    JobConf job = ValueAggregatorJob.createValueAggregatorJob(args);
211    JobClient.runJob(job);
212  }
213}