Created
December 8, 2014 21:38
-
-
Save nagataka/3415b2d0cdda9b5bb6e2 to your computer and use it in GitHub Desktop.
Example of calculating the average object size from apache like log file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import java.io.IOException; | |
| import org.apache.hadoop.conf.Configured; | |
| import org.apache.hadoop.fs.Path; | |
| import org.apache.hadoop.io.IntWritable; | |
| import org.apache.hadoop.io.LongWritable; | |
| import org.apache.hadoop.io.Text; | |
| import org.apache.hadoop.mapreduce.Job; | |
| import org.apache.hadoop.mapreduce.Mapper; | |
| import org.apache.hadoop.mapreduce.Reducer; | |
| import org.apache.hadoop.mapreduce.Reducer.Context; | |
| 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; | |
| class KeyMapper extends Mapper<LongWritable, Text, Text, IntWritable> { | |
| @Override | |
| protected void map(LongWritable key, Text value, Context context) throws IOException, InterruptedException { | |
| String line = value.toString(); | |
| String[] records = line.split(" "); | |
| context.write(new Text(records[0]), new IntWritable(Integer.parseInt(records[3]))); | |
| } | |
| } | |
| class KeyReducer extends Reducer<Text, IntWritable, Text, IntWritable>{ | |
| @Override | |
| protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException { | |
| int count = 0; | |
| int sum = 0; | |
| int result = 0; | |
| for(IntWritable value : values){ | |
| sum += value.get(); | |
| count += 1; | |
| } | |
| result = sum / count; | |
| context.write(key, new IntWritable(result)); | |
| } | |
| } | |
| class Combine extends Reducer<Text, IntWritable, Text, IntWritable>{ | |
| @Override | |
| protected void reduce(Text key, Iterable<IntWritable> values, Context context) throws IOException, InterruptedException{ | |
| int count = 0; | |
| int sum = 0; | |
| int inter_result = 0; | |
| for (IntWritable value : values){ | |
| sum += value.get(); | |
| count += 1; | |
| } | |
| inter_result = sum / count; | |
| context.write(key, new IntWritable(inter_result)); | |
| } | |
| } | |
| public class KeyCountDriver extends Configured implements Tool { | |
| @Override | |
| public int run(String[] args) throws Exception{ | |
| if(args.length != 2){ | |
| System.out.printf("Usage: %s [generic options] <indir> <outdir>\n", getClass().getSimpleName()); | |
| return -1; | |
| } | |
| Job job = new Job(getConf(), "KeyCount"); | |
| job.setJarByClass(KeyCountDriver.class); | |
| job.setJarByClass(KeyMapper.class); | |
| job.setJarByClass(KeyReducer.class); | |
| FileInputFormat.addInputPath(job, new Path(args[0])); | |
| FileOutputFormat.setOutputPath(job, new Path(args[1])); | |
| job.setMapperClass(KeyMapper.class); | |
| job.setReducerClass(KeyReducer.class); | |
| job.setMapOutputKeyClass(Text.class); | |
| job.setMapOutputValueClass(IntWritable.class); | |
| job.setOutputKeyClass(Text.class); | |
| job.setOutputValueClass(IntWritable.class); | |
| return job.waitForCompletion(true) ? 0 : -1; | |
| } | |
| public static void main(String[] args) throws Exception { | |
| System.exit(ToolRunner.run(new KeyCountDriver(), args)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment