CollapserUtils { @SuppressWarnings("unchecked") public static <T> T getNestedFieldValue(IndexedRecord record, String fieldName) { if (record == null || fieldName == null) { return null; } IndexedRecord actRecord = record; String[] path = fieldName.split("\\."); for (int i = 0; i < path.length; i++) { Schema schema = actRecord.getSchema(); if (schema.getField(path[i]) == null) { return null; } Object nextRecord = actRecord.get(schema.getField(path[i]).pos()); if (i == path.length-1) { return (T) nextRecord; } else if (nextRecord instanceof IndexedRecord) { actRecord = (IndexedRecord) nextRecord; } else { return null; } } return null; } private  CollapserUtils(); static boolean haveEqualSchema(Collection<IndexedRecord> records); static boolean isEnvelopeSchema(Schema schema); static String getOriginValue(IndexedRecord record); static IndexedRecord getDataRecord(IndexedRecord record); @SuppressWarnings("unchecked") static T getNestedFieldValue(IndexedRecord record, String fieldName); static int getNumberOfFilledFields(IndexedRecord record, List<String> fields); static void sortByFilledDataFields(List<T> records, final List<String> fields); static T merge(T base, T update);  }
@Test public void testGetNestedFieldValue() { assertNull(CollapserUtils.getNestedFieldValue(null, null)); assertNull(CollapserUtils.getNestedFieldValue(SampleData.metadataRecord11, null)); assertNull(CollapserUtils.getNestedFieldValue(null, "notnull")); assertNull(CollapserUtils.getNestedFieldValue(SampleData.metadataRecord11, "field")); assertNull(CollapserUtils.getNestedFieldValue(SampleData.metadataRecord11, "id.field")); assertEquals("id-1", CollapserUtils.getNestedFieldValue(SampleData.metadataRecord11, "id")); assertEquals(Integer.valueOf(1990), CollapserUtils.getNestedFieldValue(SampleData.metadataRecord11, "year")); assertEquals(true, CollapserUtils.getNestedFieldValue(SampleData.metadataRecord11, "publicationType.article")); assertEquals("id-1", CollapserUtils.getNestedFieldValue(SampleData.envMetadataRecord11, "data.id")); assertEquals(Integer.valueOf(1990), CollapserUtils.getNestedFieldValue(SampleData.envMetadataRecord11, "data.year")); assertEquals(true, CollapserUtils.getNestedFieldValue(SampleData.envMetadataRecord11, "data.publicationType.article")); }