/* * chombo: Hadoop Map Reduce utility * Author: Pranab Ghosh * * Licensed under the Apache License, Version 2.0 (the "License"); you * may not use this file except in compliance with the License. You may * obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or * implied. See the License for the specific language governing * permissions and limitations under the License. */ package org.chombo.mr; import java.io.IOException; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.conf.Configured; import org.apache.hadoop.fs.Path; import org.apache.hadoop.io.LongWritable; import org.apache.hadoop.io.NullWritable; import org.apache.hadoop.io.Text; import org.apache.hadoop.mapreduce.Job; import org.apache.hadoop.mapreduce.Mapper; import org.apache.hadoop.mapreduce.lib.input.FileInputFormat; import org.apache.hadoop.mapreduce.lib.output.FileOutputFormat; import org.apache.hadoop.util.Tool; import org.apache.hadoop.util.ToolRunner; import org.chombo.util.GenericAttributeSchema; import org.chombo.util.Tuple; import org.chombo.util.Utility; import org.chombo.util.Attribute; /** * Basic stats for length of string attributes * @author pranab * */ public class StringAttrStats extends Configured implements Tool { private static String configDelim = ","; @Override public int run(String[] args) throws Exception { Job job = new Job(getConf()); String jobName = "Basic stats for string attribute lengths"; job.setJobName(jobName); job.setJarByClass(StringAttrStats.class); FileInputFormat.addInputPath(job, new Path(args[0])); FileOutputFormat.setOutputPath(job, new Path(args[1])); Utility.setConfiguration(job.getConfiguration(), "chombo"); job.setMapperClass(StringAttrStats.StatsMapper.class); job.setReducerClass(NumericalAttrStats.StatsReducer.class); job.setCombinerClass(NumericalAttrStats.StatsCombiner.class); job.setMapOutputKeyClass(Tuple.class); job.setMapOutputValueClass(Tuple.class); job.setOutputKeyClass(NullWritable.class); job.setOutputValueClass(Text.class); int numReducer = job.getConfiguration().getInt("sas.num.reducer", -1); numReducer = -1 == numReducer ? job.getConfiguration().getInt("num.reducer", 1) : numReducer; job.setNumReduceTasks(numReducer); int status = job.waitForCompletion(true) ? 0 : 1; return status; } /** * @author pranab * */ public static class StatsMapper extends Mapper<LongWritable, Text, Tuple, Tuple> { private Tuple outKey = new Tuple(); private Tuple outVal = new Tuple(); private int[] attributes; private String fieldDelimRegex; private double val; private double sqVal; private int count = 1; private String[] items; private int[] idOrdinals; private GenericAttributeSchema schema; protected void setup(Context context) throws IOException, InterruptedException { Configuration config = context.getConfiguration(); fieldDelimRegex = config.get("field.delim.regex", ","); schema = Utility.getGenericAttributeSchema(config, "sas.schema.file.path"); attributes = Utility.getAttributes("sas.attr.list", configDelim, schema, config, Attribute.DATA_TYPE_STRING); idOrdinals = Utility.intArrayFromString(config.get("sas.id.field.ordinals"), configDelim); } @Override protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { items = value.toString().split(fieldDelimRegex, -1); for (int attr : attributes) { outKey.initialize(); outVal.initialize(); if (null != idOrdinals) { outKey.addFromArray(items, idOrdinals); } outKey.add(attr); val = items[attr].length(); sqVal = val * val; outVal.add(val, val, val, sqVal, count); context.write(outKey, outVal); } } } /** * @param args */ public static void main(String[] args) throws Exception { int exitCode = ToolRunner.run(new StringAttrStats(), args); System.exit(exitCode); } }