All Downloads are FREE. Search and download functionalities are using the official Maven repository.

datafu.hourglass.avro.AvroKeyValueWithMetadataRecordWriter Maven / Gradle / Ivy

Go to download

Librares that make easier to solve data problems using Hadoop and higher level languages based on it.

There is a newer version: 1.3.3
Show newest version
/**
 * 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 java.util.Map.Entry;

import org.apache.avro.Schema;
import org.apache.avro.hadoop.io.AvroKeyValue;
import org.apache.avro.hadoop.io.AvroDatumConverter;
import org.apache.avro.file.CodecFactory;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.generic.GenericData;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.reflect.ReflectDatumWriter;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.mapreduce.RecordWriter;
import org.apache.hadoop.mapreduce.TaskAttemptContext;

/**
 * Writes key/value pairs to an Avro container file.
 *
 * 

Each entry in the Avro container file will be a generic record with two fields, * named 'key' and 'value'. The input types may be basic Writable objects like Text or * IntWritable, or they may be AvroWrapper subclasses (AvroKey or AvroValue). Writable * objects will be converted to their corresponding Avro types when written to the generic * record key/value pair.

* * @param The type of key to write. * @param The type of value to write. */ public class AvroKeyValueWithMetadataRecordWriter extends RecordWriter { /** A writer for the Avro container file. */ private final DataFileWriter mAvroFileWriter; /** The writer schema for the generic record entries of the Avro container file. */ private final Schema mKeyValuePairSchema; /** A reusable Avro generic record for writing key/value pairs to the file. */ private final AvroKeyValue mOutputRecord; /** A helper object that converts the input key to an Avro datum. */ private final AvroDatumConverter mKeyConverter; /** A helper object that converts the input value to an Avro datum. */ private final AvroDatumConverter mValueConverter; /** The configuration key prefix for a text output metadata. */ public static final String TEXT_PREFIX = "avro.meta.text."; public AvroKeyValueWithMetadataRecordWriter(AvroDatumConverter keyConverter, AvroDatumConverter valueConverter, CodecFactory compressionCodec, OutputStream outputStream, Configuration conf) throws IOException { // Create the generic record schema for the key/value pair. mKeyValuePairSchema = AvroKeyValue.getSchema( keyConverter.getWriterSchema(), valueConverter.getWriterSchema()); // Create an Avro container file and a writer to it. mAvroFileWriter = new DataFileWriter( new ReflectDatumWriter(mKeyValuePairSchema)); mAvroFileWriter.setCodec(compressionCodec); for (Entry e : conf) { if (e.getKey().startsWith(TEXT_PREFIX)) mAvroFileWriter.setMeta(e.getKey().substring(TEXT_PREFIX.length()), e.getValue()); } mAvroFileWriter.create(mKeyValuePairSchema, outputStream); // Keep a reference to the converters. mKeyConverter = keyConverter; mValueConverter = valueConverter; // Create a reusable output record. mOutputRecord = new AvroKeyValue(new GenericData.Record(mKeyValuePairSchema)); } /** * Gets the writer schema for the key/value pair generic record. * * @return The writer schema used for entries of the Avro container file. */ public Schema getWriterSchema() { return mKeyValuePairSchema; } /** {@inheritDoc} */ @Override public void write(K key, V value) throws IOException { mOutputRecord.setKey(mKeyConverter.convert(key)); mOutputRecord.setValue(mValueConverter.convert(value)); mAvroFileWriter.append(mOutputRecord.get()); } /** {@inheritDoc} */ @Override public void close(TaskAttemptContext context) throws IOException { mAvroFileWriter.close(); } }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy