/***********************************************************************************************************************
 *
 * Copyright (C) 2010-2013 by the Stratosphere project (http://stratosphere.eu)
 *
 * 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 eu.stratosphere.api.java.io;


import eu.stratosphere.api.common.io.FileOutputFormat;
import eu.stratosphere.core.fs.Path;
import org.apache.avro.Schema;
import org.apache.avro.file.DataFileWriter;
import org.apache.avro.io.DatumWriter;
import org.apache.avro.reflect.ReflectData;
import org.apache.avro.reflect.ReflectDatumWriter;
import org.apache.avro.specific.SpecificDatumWriter;
import java.io.IOException;

public class AvroOutputFormat<E> extends FileOutputFormat<E> {

	private static final long serialVersionUID = 1L;

	private final Class<E> avroValueType;

	private transient DataFileWriter<E> dataFileWriter;


	public AvroOutputFormat(Path filePath, Class<E> type) {
		super(filePath);
		this.avroValueType = type;
	}

	public AvroOutputFormat(Class<E> type) {
		this.avroValueType = type;
	}

	@Override
	public void writeRecord(E record) throws IOException {
		dataFileWriter.append(record);
	}

	@Override
	public void open(int taskNumber, int numTasks) throws IOException {
		super.open(taskNumber, numTasks);
		DatumWriter<E> datumWriter;
		Schema schema = null;
		if (org.apache.avro.specific.SpecificRecordBase.class.isAssignableFrom(avroValueType)) {
			datumWriter = new SpecificDatumWriter<E>(avroValueType);
			try {
				schema = ((org.apache.avro.specific.SpecificRecordBase)avroValueType.newInstance()).getSchema();
			} catch (InstantiationException e) {
				throw new RuntimeException(e.getMessage());
			} catch (IllegalAccessException e) {
				throw new RuntimeException(e.getMessage());
			}
		} else {
			datumWriter = new ReflectDatumWriter<E>(avroValueType);
			schema = ReflectData.get().getSchema(avroValueType);
		}
		dataFileWriter = new DataFileWriter<E>(datumWriter);
		dataFileWriter.create(schema, this.stream);
	}

	@Override
	public void close() throws IOException {
		dataFileWriter.flush();
		dataFileWriter.close();
		super.close();
	}
}

@@ -1448,16 +1448,59 @@
 eType;%0A%0A
+%09private Schema userDefinedSchema = null;%0A%0A
 %09private
@@ -1726,24 +1726,103 @@
 = type;%0A%09%7D%0A%0A
+%09public void setSchema(Schema schema) %7B%0A%09%09this.userDefinedSchema = schema;%0A%09%7D%0A%0A
 %09@Override%0A%09
@@ -2716,43 +2716,138 @@
 ;%0A%09%09
-dataFileWriter.create(schema, this.
+if (userDefinedSchema == null) %7B%0A%09%09%09dataFileWriter.create(schema, stream);%0A%09%09%7D else %7B%0A%09%09%09dataFileWriter.create(userDefinedSchema, 
 stre
@@ -2851,16 +2851,20 @@
 tream);%0A
+%09%09%7D%0A
 %09%7D%0A%0A%09@Ov

Add method to pass a schema for AvroOutputFormat