Skip to content

Commit

Permalink
minor clean up wrt #16
Browse files Browse the repository at this point in the history
  • Loading branch information
cowtowncoder committed Feb 22, 2017
1 parent 8465329 commit b66ab0f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,13 @@ public enum Feature

/**
* Feature that tells Avro to write data in file format (i.e. including the schema with the data)
* rather than the RPC format
* rather than the RPC format which is otherwise default
*<p>
* NOTE: reader-side will have to be aware of distinction as well, since possible inclusion
* of this header is not 100% reliably auto-detectable (while header has distinct marker,
* "raw" Avro content has no limitations and could theoretically have same pre-amble from data).
*
* @since 2.9
*/
AVRO_FILE_OUTPUT(false)
;
Expand Down Expand Up @@ -606,6 +612,8 @@ protected void _complete() throws IOException
// do not want to hide the original problem...
// First one sanity check, for a (relatively?) common case
if (_rootContext != null) {
// 21-Feb-2017, tatu: As per [dataformats-binary#15], need to ensure schema gets
// written, if using "File" format (not raw "rpc" one)
if (isEnabled(Feature.AVRO_FILE_OUTPUT)) {
_rootContext.complete(_output);
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.fasterxml.jackson.dataformat.avro;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.SeekableByteArrayInput;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;

import com.fasterxml.jackson.databind.ObjectMapper;

// for [dataformats-binary#16]
public class FileFormatTest extends AvroTestBase
{
public void testFileFormatOutput() throws Exception
{
Employee empl = new Employee();
empl.name = "Bobbee";
empl.age = 39;
empl.emails = new String[] { "[email protected]", "[email protected]" };
empl.boss = null;

AvroFactory af = new AvroFactory();
ObjectMapper mapper = new ObjectMapper(af);

af.enable(AvroGenerator.Feature.AVRO_FILE_OUTPUT);

AvroSchema schema = getEmployeeSchema();
byte[] bytes = mapper.writer(schema).writeValueAsBytes(empl);

assertNotNull(bytes);
assertEquals(301, bytes.length);

DatumReader<GenericRecord> datumReader = new GenericDatumReader<GenericRecord>(schema.getAvroSchema());
@SuppressWarnings("resource")
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<GenericRecord>(new SeekableByteArrayInput(bytes),
datumReader);
GenericRecord output = dataFileReader.next();

assertNotNull(output);
assertEquals(output.get("name").toString(), empl.name);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,6 @@
import java.io.ByteArrayOutputStream;
import java.io.IOException;

import org.apache.avro.file.DataFileReader;
import org.apache.avro.file.SeekableByteArrayInput;
import org.apache.avro.generic.GenericDatumReader;
import org.apache.avro.generic.GenericRecord;
import org.apache.avro.io.DatumReader;
import org.junit.Assert;

import com.fasterxml.jackson.annotation.JsonPropertyOrder;
Expand Down Expand Up @@ -165,31 +160,4 @@ public void testIgnoringOfUnknownObject() throws Exception
BinaryAndNumber output = mapper.reader(SCHEMA_WITH_BINARY_JSON).forType(BinaryAndNumber.class).readValue(bytes);
assertEquals("Bob", output.name);
}

public void testFileOutput() throws Exception
{
Employee empl = new Employee();
empl.name = "Bobbee";
empl.age = 39;
empl.emails = new String[] { "[email protected]", "[email protected]" };
empl.boss = null;

AvroFactory af = new AvroFactory();
ObjectMapper mapper = new ObjectMapper(af);

af.enable(AvroGenerator.Feature.AVRO_FILE_OUTPUT);

AvroSchema schema = getEmployeeSchema();
byte[] bytes = mapper.writer(schema).writeValueAsBytes(empl);

assertNotNull(bytes);
assertEquals(301, bytes.length);

DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(schema.getAvroSchema());
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(new SeekableByteArrayInput(bytes), datumReader);
GenericRecord output = dataFileReader.next();

assertNotNull(output);
assertEquals(output.get("name").toString(), empl.name);
}
}

0 comments on commit b66ab0f

Please sign in to comment.