}
}
@Test
public void testMap() throws Exception {
Schema schema = new Parser().parse(new File("src/test/resources/test-avro-schemas/intero1.avsc"));
GenericData.Record document0 = new GenericData.Record(schema);
Map map = new LinkedHashMap();
Schema mapRecordSchema = schema.getField("mapField").schema().getValueType();
GenericData.Record mapRecord = new GenericData.Record(mapRecordSchema);
mapRecord.put("label", "nadja");
map.put(utf8("foo"), mapRecord);
document0.put("mapField", map);
morphline = createMorphline("test-morphlines/extractAvroPaths");
deleteAllDocuments();
Record record = new Record();
record.put(Fields.ATTACHMENT_BODY, document0);
startSession();
// System.out.println(schema.toString(true));
// System.out.println(document0.toString());
assertTrue(morphline.process(record));
assertEquals(1, collector.getRecords().size());
assertEquals(Arrays.asList("nadja"), collector.getFirstRecord().get("/mapField/foo/label"));
assertEquals(Arrays.asList(), collector.getFirstRecord().get("/unknownField"));
morphline = createMorphline("test-morphlines/extractAvroPathsFlattened");
deleteAllDocuments();
record = new Record();
record.put(Fields.ATTACHMENT_BODY, document0);
startSession();
// System.out.println(documentSchema.toString(true));
// System.out.println(document0.toString());
assertTrue(morphline.process(record));
assertEquals(1, collector.getRecords().size());
assertEquals(Arrays.asList("nadja"), collector.getFirstRecord().get("/mapField/foo/label"));
assertEquals(Arrays.asList(), collector.getFirstRecord().get("/unknownField"));
ingestAndVerifyAvro(schema, document0);
Record event = new Record();
event.getFields().put(Fields.ATTACHMENT_BODY, document0);
morphline = createMorphline("test-morphlines/extractAvroTree");
deleteAllDocuments();
//System.out.println(document0);
assertTrue(load(event));
assertEquals(1, queryResultSetSize("*:*"));
Record first = collector.getFirstRecord();
assertEquals(Arrays.asList("nadja"), first.get("/mapField/foo/label"));
AbstractParser.removeAttachments(first);
assertEquals(1, first.getFields().asMap().size());
{
morphline = createMorphline("test-morphlines/toAvro");
Record jdoc0 = new Record();
jdoc0.put("_dataset_descriptor_schema", schema);
jdoc0.put("mapField", new HashMap(ImmutableMap.of(
utf8("foo"), ImmutableMap.of("label", "nadja")
))
);
Record expect0 = jdoc0.copy();
expect0.put(Fields.ATTACHMENT_BODY, document0);
processAndVerifySuccess(jdoc0, expect0, false);
// verify that multiple maps can't be converted to a non-array schema
jdoc0 = new Record();
jdoc0.put("_dataset_descriptor_schema", schema);
jdoc0.put("mapField", new HashMap(ImmutableMap.of(
utf8("foo"), ImmutableMap.of("label", "nadja")
))
);
jdoc0.put("mapField", new HashMap(ImmutableMap.of(
utf8("foo"), ImmutableMap.of("label", "nadja")
))
);
collector.reset();
assertFalse(morphline.process(jdoc0));
// verify that an exception is raised if a required field is missing
jdoc0 = new Record();
jdoc0.put("_dataset_descriptor_schema", schema);
jdoc0.put("mapField", new HashMap(ImmutableMap.of(
utf8("foo"), ImmutableMap.of()
))
);
collector.reset();
assertFalse(morphline.process(jdoc0));
// verify that default field is used if value is missing
Schema schema2 = new Parser().parse(new File("src/test/resources/test-avro-schemas/intero2.avsc"));
jdoc0 = new Record();
jdoc0.put("_dataset_descriptor_schema", schema2);
jdoc0.put("mapField", new HashMap(ImmutableMap.of(
utf8("foo"), ImmutableMap.of()
))