writer.append(record);
}
writer.flush();
writer.close();
DataFileReader<GenericData.Record> reader = new DataFileReader(new ReadAvroContainerBuilder.ForwardOnlySeekableInputStream(new ByteArrayInputStream(bout.toByteArray())), new GenericDatumReader());
Schema schema2 = reader.getSchema();
assertEquals(schema, schema2);
for (GenericData.Record record : records) {
assertTrue(reader.hasNext());
GenericData.Record record2 = reader.next();
assertEquals(record, record2);
}
assertFalse(reader.hasNext());
reader.close();
Record event = new Record();
event.getFields().put(Fields.ATTACHMENT_BODY, new ByteArrayInputStream(bout.toByteArray()));
morphline = createMorphline("test-morphlines/readAvroContainer");
deleteAllDocuments();
assertTrue(load(event));
assertEquals(records.length, queryResultSetSize("*:*"));
GenericDatumWriter datumWriter = new GenericDatumWriter(schema);
bout = new ByteArrayOutputStream();
Encoder encoder = EncoderFactory.get().binaryEncoder(bout, null);
for (GenericData.Record record : records) {
datumWriter.write(record, encoder);
}
encoder.flush();
Decoder decoder = DecoderFactory.get().binaryDecoder(new ByteArrayInputStream(bout.toByteArray()), null);
DatumReader<GenericData.Record> datumReader = new GenericDatumReader<GenericData.Record>(schema);
for (int i = 0; i < records.length; i++) {
GenericData.Record record3 = datumReader.read(null, decoder);
assertEquals(records[i], record3);
}
event = new Record();
event.getFields().put(Fields.ATTACHMENT_BODY, new ByteArrayInputStream(bout.toByteArray()));
File tmp = new File("target/tmp-test-schema.avsc");
try {
tmp.deleteOnExit();
Files.write(schema.toString(true), tmp, Charsets.UTF_8);
morphline = createMorphline("test-morphlines/readAvroWithExternalSchema");
deleteAllDocuments();
assertTrue(load(event));
assertEquals(records.length, queryResultSetSize("*:*"));
} finally {
tmp.delete();
}
for (GenericData.Record record : records) {
event = new Record();
event.getFields().put(Fields.ATTACHMENT_BODY, record);
morphline = createMorphline("test-morphlines/extractAvroTree");
deleteAllDocuments();
assertTrue(load(event));
assertEquals(1, queryResultSetSize("*:*"));
}
String[] formats = new String[] {"", "AndSnappy"};
for (String format : formats) {
morphline = createMorphline("test-morphlines/writeAvroToByteArrayWithContainer" + format);
event = new Record();
event.getFields().putAll(Fields.ATTACHMENT_BODY, Arrays.asList(records));
deleteAllDocuments();
assertTrue(load(event));
assertEquals(1, collector.getFirstRecord().get(Fields.ATTACHMENT_BODY).size());
byte[] bytes = (byte[]) collector.getFirstRecord().getFirstValue(Fields.ATTACHMENT_BODY);
assertNotNull(bytes);
reader = new DataFileReader(new ReadAvroContainerBuilder.ForwardOnlySeekableInputStream(new ByteArrayInputStream(bytes)), new GenericDatumReader());
assertEquals("bar", new String(reader.getMeta("foo"), Charsets.UTF_8));
assertEquals("Nadja", new String(reader.getMeta("firstName"), Charsets.UTF_8));
assertEquals(schema, reader.getSchema());
for (GenericData.Record record : records) {
assertTrue(reader.hasNext());
GenericData.Record record2 = reader.next();
assertEquals(record, record2);
}
assertFalse(reader.hasNext());
reader.close();
}
formats = new String[] {"Binary", "JSON"};
for (String format : formats) {
morphline = createMorphline("test-morphlines/writeAvroToByteArrayWithContainerless" + format);