Skip to content

Commit 8465329

Browse files
authored
Merge pull request #46 from aosagie/feature/avro-file-output
Add Avro file output and test
2 parents 84315d4 + 4a0bafd commit 8465329

File tree

4 files changed

+63
-3
lines changed

4 files changed

+63
-3
lines changed

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/AvroGenerator.java

+13-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,13 @@ public enum Feature
4949
*
5050
* @since 2.7
5151
*/
52-
AVRO_BUFFERING(true)
52+
AVRO_BUFFERING(true),
53+
54+
/**
55+
* Feature that tells Avro to write data in file format (i.e. including the schema with the data)
56+
* rather than the RPC format
57+
*/
58+
AVRO_FILE_OUTPUT(false)
5359
;
5460

5561
protected final boolean _defaultState;
@@ -600,8 +606,12 @@ protected void _complete() throws IOException
600606
// do not want to hide the original problem...
601607
// First one sanity check, for a (relatively?) common case
602608
if (_rootContext != null) {
603-
_rootContext.complete();
604-
_encoder.flush();
609+
if (isEnabled(Feature.AVRO_FILE_OUTPUT)) {
610+
_rootContext.complete(_output);
611+
} else {
612+
_rootContext.complete();
613+
_encoder.flush();
614+
}
605615
}
606616
}
607617
}

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/AvroWriteContext.java

+5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package com.fasterxml.jackson.dataformat.avro.ser;
22

33
import java.io.IOException;
4+
import java.io.OutputStream;
45

56
import org.apache.avro.Schema;
67
import org.apache.avro.Schema.Type;
@@ -97,6 +98,10 @@ public void complete() throws IOException {
9798
throw new IllegalStateException("Can not be called on "+getClass().getName());
9899
}
99100

101+
public void complete(OutputStream outputStream) throws IOException {
102+
throw new IllegalStateException("Can not be called on "+getClass().getName());
103+
}
104+
100105
@Deprecated // remove from 2.9
101106
public void complete(BinaryEncoder encoder) throws IOException { complete(); }
102107

avro/src/main/java/com/fasterxml/jackson/dataformat/avro/ser/RootContext.java

+13
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
package com.fasterxml.jackson.dataformat.avro.ser;
22

33
import java.io.IOException;
4+
import java.io.OutputStream;
45

56
import org.apache.avro.Schema;
7+
import org.apache.avro.file.DataFileWriter;
68
import org.apache.avro.generic.*;
79
import org.apache.avro.io.BinaryEncoder;
810

911
import com.fasterxml.jackson.databind.JsonMappingException;
1012
import com.fasterxml.jackson.dataformat.avro.AvroGenerator;
13+
import org.apache.avro.io.DatumWriter;
1114

1215
class RootContext
1316
extends AvroWriteContext
@@ -105,6 +108,16 @@ public void complete() throws IOException {
105108
_rootValue = null;
106109
}
107110

111+
@Override
112+
public void complete(OutputStream outputStream) throws IOException {
113+
DatumWriter<Object> datumWriter = new NonBSGenericDatumWriter<>(_schema);
114+
DataFileWriter<Object> dataFileWriter = new DataFileWriter<>(datumWriter);
115+
116+
dataFileWriter.create(_schema, outputStream);
117+
dataFileWriter.append(_rootValue);
118+
dataFileWriter.close();
119+
}
120+
108121
@Override
109122
public void appendDesc(StringBuilder sb) {
110123
sb.append("/");

avro/src/test/java/com/fasterxml/jackson/dataformat/avro/SimpleGenerationTest.java

+32
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,11 @@
33
import java.io.ByteArrayOutputStream;
44
import java.io.IOException;
55

6+
import org.apache.avro.file.DataFileReader;
7+
import org.apache.avro.file.SeekableByteArrayInput;
8+
import org.apache.avro.generic.GenericDatumReader;
9+
import org.apache.avro.generic.GenericRecord;
10+
import org.apache.avro.io.DatumReader;
611
import org.junit.Assert;
712

813
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
@@ -160,4 +165,31 @@ public void testIgnoringOfUnknownObject() throws Exception
160165
BinaryAndNumber output = mapper.reader(SCHEMA_WITH_BINARY_JSON).forType(BinaryAndNumber.class).readValue(bytes);
161166
assertEquals("Bob", output.name);
162167
}
168+
169+
public void testFileOutput() throws Exception
170+
{
171+
Employee empl = new Employee();
172+
empl.name = "Bobbee";
173+
empl.age = 39;
174+
empl.emails = new String[] { "[email protected]", "[email protected]" };
175+
empl.boss = null;
176+
177+
AvroFactory af = new AvroFactory();
178+
ObjectMapper mapper = new ObjectMapper(af);
179+
180+
af.enable(AvroGenerator.Feature.AVRO_FILE_OUTPUT);
181+
182+
AvroSchema schema = getEmployeeSchema();
183+
byte[] bytes = mapper.writer(schema).writeValueAsBytes(empl);
184+
185+
assertNotNull(bytes);
186+
assertEquals(301, bytes.length);
187+
188+
DatumReader<GenericRecord> datumReader = new GenericDatumReader<>(schema.getAvroSchema());
189+
DataFileReader<GenericRecord> dataFileReader = new DataFileReader<>(new SeekableByteArrayInput(bytes), datumReader);
190+
GenericRecord output = dataFileReader.next();
191+
192+
assertNotNull(output);
193+
assertEquals(output.get("name").toString(), empl.name);
194+
}
163195
}

0 commit comments

Comments
 (0)