package examples.avro.mapreduce;
import examples.avro.simple.User;
import org.apache.avro.Schema;
import org.apache.avro.mapred.AvroKey;
import org.apache.avro.mapred.AvroValue;
import org.apache.avro.mapreduce.AvroJob;
import org.apache.avro.mapreduce.AvroKeyInputFormat;
import org.apache.avro.mapreduce.AvroKeyValueOutputFormat;
import org.apache.hadoop.conf.Configured;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.io.IntWritable;
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.Reducer;
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 java.io.IOException;
public class MapReduceColorCount extends Configured implements Tool {
///Mapper定义:
///输入Key类型是AvroKey<User>,输入Value类型是NullWritable
///输出Key类型是Text,输出Value类型是IntWritable
public static class ColorCountMapper extends
Mapper<AvroKey<User>, NullWritable, Text, IntWritable> {
@Override
public void map(AvroKey<User> key, NullWritable value, Context context)
throws IOException, InterruptedException {
CharSequence color = key.datum().getFavoriteColor();
if (color == null) {
color = "none";
}
context.write(new Text(color.toString()), new IntWritable(1));
}
}
///Reducer定义:
///输入Key类型是Text,输入Value类型是IntWritable(跟Key的输出Key/Value类型一致)
///输出Key类型是AvroKey<CharSequence>,输出Value类型是AvroValue<Integer>
public static class ColorCountReducer extends
Reducer<Text, IntWritable, AvroKey<CharSequence>, AvroValue<Integer>> {
@Override
public void reduce(Text key, Iterable<IntWritable> values,
Context context) throws IOException, InterruptedException {
int sum = 0;
for (IntWritable value : values) {
sum += value.get();
}
context.write(new AvroKey<CharSequence>(key.toString()), new AvroValue<Integer>(sum));
}
}
public int run(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: MapReduceColorCount <input path> <output path>");
return -1;
}
Job job = new Job(getConf());
job.setJarByClass(MapReduceColorCount.class);
job.setJobName("Color Count");
///指定输入路径,输入文件是Avro格式
FileInputFormat.setInputPaths(job, new Path(args[0]));
///指定输出路径,输出文件格式是Key/Value组成的Avro文件,见AvroKeyValueOutputFormat
FileOutputFormat.setOutputPath(job, new Path(args[1]));
//AvroKeyInputFormat: A MapReduce InputFormat that can handle Avro container files.
job.setInputFormatClass(AvroKeyInputFormat.class);
job.setMapperClass(ColorCountMapper.class);
AvroJob.setInputKeySchema(job, User.getClassSchema());
job.setMapOutputKeyClass(Text.class);
job.setMapOutputValueClass(IntWritable.class);
//AvroKeyValueOutputFormat: FileOutputFormat for writing Avro container files of key/value pairs
job.setOutputFormatClass(AvroKeyValueOutputFormat.class);
job.setReducerClass(ColorCountReducer.class);
AvroJob.setOutputKeySchema(job, Schema.create(Schema.Type.STRING));
AvroJob.setOutputValueSchema(job, Schema.create(Schema.Type.INT));
return (job.waitForCompletion(true) ? 0 : 1);
}
public static void main(String[] args) throws Exception {
int res = ToolRunner.run(new MapReduceColorCount(), args);
System.exit(res);
}
}
3. 主要类注释
3.1 AvroKey
/** The wrapper of keys for jobs configured with {@link AvroJob} . */
3.2 AvroValue
/** The wrapper of values for jobs configured with {@link AvroJob} . */
3.3 AvroJob
/** Setters to configure jobs for Avro data. */
3.4 AvroKeyInputFormat
/**
* A MapReduce InputFormat that can handle Avro container files.
*
* <p>Keys are AvroKey wrapper objects that contain the Avro data. Since Avro
* container files store only records (not key/value pairs), the value from
* this InputFormat is a NullWritable.</p>
*/
3.5 AvroKeyValueOutputFormat
/**
* FileOutputFormat for writing Avro container files of key/value pairs.
*
* <p>Since Avro container files can only contain records (not key/value pairs), this
* output format puts the key and value into an Avro generic record with two fields, named
* 'key' and 'value'.</p>
*
* <p>The keys and values given to this output format may be Avro objects wrapped in
* <code>AvroKey</code> or <code>AvroValue</code> objects. The basic Writable types are
* also supported (e.g., IntWritable, Text); they will be converted to their corresponding
* Avro types.</p>
*
* @param <K> The type of key. If an Avro type, it must be wrapped in an <code>AvroKey</code>.
* @param <V> The type of value. If an Avro type, it must be wrapped in an <code>AvroValue</code>.
*/
3.6
/**
* Sets the job input key schema.
*
* @param job The job to configure.
* @param schema The input key schema.
*/
public static void setInputKeySchema(Job job, Schema schema) {
job.getConfiguration().set(CONF_INPUT_KEY_SCHEMA, schema.toString());
}
/**
* Sets the job input value schema.
*
* @param job The job to configure.
* @param schema The input value schema.
*/
public static void setInputValueSchema(Job job, Schema schema) {
job.getConfiguration().set(CONF_INPUT_VALUE_SCHEMA, schema.toString());
}
3.7
/**
* Sets the map output key schema.
*
* @param job The job to configure.
* @param schema The map output key schema.
*/
public static void setMapOutputKeySchema(Job job, Schema schema) {
job.setMapOutputKeyClass(AvroKey.class);
job.setGroupingComparatorClass(AvroKeyComparator.class);
job.setSortComparatorClass(AvroKeyComparator.class);
AvroSerialization.setKeyWriterSchema(job.getConfiguration(), schema);
AvroSerialization.setKeyReaderSchema(job.getConfiguration(), schema);
AvroSerialization.addToConfiguration(job.getConfiguration());
}
/**
* Sets the map output value schema.
*
* @param job The job to configure.
* @param schema The map output value schema.
*/
public static void setMapOutputValueSchema(Job job, Schema schema) {
job.setMapOutputValueClass(AvroValue.class);
AvroSerialization.setValueWriterSchema(job.getConfiguration(), schema);
AvroSerialization.setValueReaderSchema(job.getConfiguration(), schema);
AvroSerialization.addToConfiguration(job.getConfiguration());
}