
datafu.hourglass.avro.AvroKeyWithMetadataOutputFormat Maven / Gradle / Ivy
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you 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 datafu.hourglass.avro;
import java.io.IOException;
import java.io.OutputStream;
import org.apache.avro.Schema;
import org.apache.avro.file.CodecFactory;
import org.apache.avro.mapred.AvroKey;
import org.apache.avro.mapreduce.AvroJob;
import org.apache.avro.mapreduce.AvroOutputFormatBase;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.io.NullWritable;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;
/**
* FileOutputFormat for writing Avro container files.
*
* Since Avro container files only contain records (not key/value pairs), this output
* format ignores the value.
*
* @param The (java) type of the Avro data to write.
*/
public class AvroKeyWithMetadataOutputFormat extends AvroOutputFormatBase, NullWritable> {
/** A factory for creating record writers. */
@SuppressWarnings("rawtypes")
private final RecordWriterFactory mRecordWriterFactory;
/**
* Constructor.
*/
@SuppressWarnings("rawtypes")
public AvroKeyWithMetadataOutputFormat() {
this(new RecordWriterFactory());
}
/**
* Constructor.
*
* @param recordWriterFactory A factory for creating record writers.
*/
protected AvroKeyWithMetadataOutputFormat(@SuppressWarnings("rawtypes") RecordWriterFactory recordWriterFactory) {
mRecordWriterFactory = recordWriterFactory;
}
/**
* A factory for creating record writers.
*
* @param The java type of the avro record to write.
*/
protected static class RecordWriterFactory {
/**
* Creates a new record writer instance.
*
* @param writerSchema The writer schema for the records to write.
* @param compressionCodec The compression type for the writer file.
* @param outputStream The target output stream for the records.
*/
protected RecordWriter, NullWritable> create(
Schema writerSchema, CodecFactory compressionCodec, OutputStream outputStream, Configuration conf)
throws IOException {
return new AvroKeyWithMetadataRecordWriter(writerSchema, compressionCodec, outputStream, conf);
}
}
/** {@inheritDoc} */
@Override
@SuppressWarnings("unchecked")
public RecordWriter, NullWritable> getRecordWriter(TaskAttemptContext context)
throws IOException {
// Get the writer schema.
Schema writerSchema = AvroJob.getOutputKeySchema(context.getConfiguration());
if (null == writerSchema) {
throw new IOException(
"AvroKeyOutputFormat requires an output schema. Use AvroJob.setOutputKeySchema().");
}
return mRecordWriterFactory.create(
writerSchema, getCompressionCodec(context), getAvroFileOutputStream(context), context.getConfiguration());
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy