@Test public void testDefaultConversion() throws Exception { Schema input = SchemaBuilder.record("Input") .namespace("com.cloudera.edh").fields() .nullableString("s1", "").requiredString("s2") .requiredString("s3").optionalLong("l1").requiredLong("l2") .endRecord(); Schema output = SchemaBuilder.record("Output") .namespace("com.cloudera.edh").fields().optionalLong("s1") .optionalString("l1").requiredLong("l2").requiredDouble("s3") .endRecord(); AvroRecordConverter converter = new AvroRecordConverter(input, output, EMPTY_MAPPING, LocaleUtils.toLocale("en_US")); Record inputRecord = new Record(input); inputRecord.put("s1", null); inputRecord.put("s2", "blah"); inputRecord.put("s3", "5.5"); inputRecord.put("l1", null); inputRecord.put("l2", 5L); Record outputRecord = converter.convert(inputRecord); assertNull(outputRecord.get("s1")); assertNull(outputRecord.get("l1")); assertEquals(5L, outputRecord.get("l2")); assertEquals(5.5, outputRecord.get("s3")); inputRecord.put("s1", "500"); inputRecord.put("s2", "blah"); inputRecord.put("s3", "5.5e-5"); inputRecord.put("l1", 100L); inputRecord.put("l2", 2L); outputRecord = converter.convert(inputRecord); assertEquals(500L, outputRecord.get("s1")); assertEquals("100", outputRecord.get("l1")); assertEquals(2L, outputRecord.get("l2")); assertEquals(5.5e-5, outputRecord.get("s3")); }
public Record convert(Record input) throws AvroConversionException { Record result = new Record(outputSchema); for (Field outputField : outputSchema.getFields()) { String inputFieldName = outputField.name(); if (fieldMapping.containsKey(outputField.name())) { inputFieldName = fieldMapping.get(outputField.name()); } IndexedRecord currentRecord = input; Schema currentSchema = getNonNullSchema(inputSchema); while (inputFieldName.contains(".")) { int dotIndex = inputFieldName.indexOf('.'); String entityName = inputFieldName.substring(0, dotIndex); Object innerRecord = currentRecord.get(currentSchema.getField( entityName).pos()); if (innerRecord == null) { currentRecord = null; break; } if (innerRecord != null && !(innerRecord instanceof IndexedRecord)) { throw new AvroConversionException(inputFieldName + " stepped through a non-record"); } currentRecord = (IndexedRecord) innerRecord; currentSchema = currentSchema.getField(entityName).schema(); currentSchema = getNonNullSchema(currentSchema); inputFieldName = inputFieldName.substring(dotIndex + 1); } Field f = currentSchema.getField(inputFieldName); if (currentRecord == null) { Schema s = null; if (f != null) { s = f.schema(); } result.put(outputField.name(), convertData(null, s, outputField.schema())); } else { result.put( outputField.name(), convertData(currentRecord.get(f.pos()), f.schema(), outputField.schema())); } } return result; }
AvroRecordConverter { public Record convert(Record input) throws AvroConversionException { Record result = new Record(outputSchema); for (Field outputField : outputSchema.getFields()) { String inputFieldName = outputField.name(); if (fieldMapping.containsKey(outputField.name())) { inputFieldName = fieldMapping.get(outputField.name()); } IndexedRecord currentRecord = input; Schema currentSchema = getNonNullSchema(inputSchema); while (inputFieldName.contains(".")) { int dotIndex = inputFieldName.indexOf('.'); String entityName = inputFieldName.substring(0, dotIndex); Object innerRecord = currentRecord.get(currentSchema.getField( entityName).pos()); if (innerRecord == null) { currentRecord = null; break; } if (innerRecord != null && !(innerRecord instanceof IndexedRecord)) { throw new AvroConversionException(inputFieldName + " stepped through a non-record"); } currentRecord = (IndexedRecord) innerRecord; currentSchema = currentSchema.getField(entityName).schema(); currentSchema = getNonNullSchema(currentSchema); inputFieldName = inputFieldName.substring(dotIndex + 1); } Field f = currentSchema.getField(inputFieldName); if (currentRecord == null) { Schema s = null; if (f != null) { s = f.schema(); } result.put(outputField.name(), convertData(null, s, outputField.schema())); } else { result.put( outputField.name(), convertData(currentRecord.get(f.pos()), f.schema(), outputField.schema())); } } return result; } }
AvroRecordConverter { public Record convert(Record input) throws AvroConversionException { Record result = new Record(outputSchema); for (Field outputField : outputSchema.getFields()) { String inputFieldName = outputField.name(); if (fieldMapping.containsKey(outputField.name())) { inputFieldName = fieldMapping.get(outputField.name()); } IndexedRecord currentRecord = input; Schema currentSchema = getNonNullSchema(inputSchema); while (inputFieldName.contains(".")) { int dotIndex = inputFieldName.indexOf('.'); String entityName = inputFieldName.substring(0, dotIndex); Object innerRecord = currentRecord.get(currentSchema.getField( entityName).pos()); if (innerRecord == null) { currentRecord = null; break; } if (innerRecord != null && !(innerRecord instanceof IndexedRecord)) { throw new AvroConversionException(inputFieldName + " stepped through a non-record"); } currentRecord = (IndexedRecord) innerRecord; currentSchema = currentSchema.getField(entityName).schema(); currentSchema = getNonNullSchema(currentSchema); inputFieldName = inputFieldName.substring(dotIndex + 1); } Field f = currentSchema.getField(inputFieldName); if (currentRecord == null) { Schema s = null; if (f != null) { s = f.schema(); } result.put(outputField.name(), convertData(null, s, outputField.schema())); } else { result.put( outputField.name(), convertData(currentRecord.get(f.pos()), f.schema(), outputField.schema())); } } return result; } AvroRecordConverter(Schema inputSchema, Schema outputSchema,
            Map<String, String> fieldMapping);  AvroRecordConverter(Schema inputSchema, Schema outputSchema,
            Map<String, String> fieldMapping, Locale locale); }
AvroRecordConverter { public Record convert(Record input) throws AvroConversionException { Record result = new Record(outputSchema); for (Field outputField : outputSchema.getFields()) { String inputFieldName = outputField.name(); if (fieldMapping.containsKey(outputField.name())) { inputFieldName = fieldMapping.get(outputField.name()); } IndexedRecord currentRecord = input; Schema currentSchema = getNonNullSchema(inputSchema); while (inputFieldName.contains(".")) { int dotIndex = inputFieldName.indexOf('.'); String entityName = inputFieldName.substring(0, dotIndex); Object innerRecord = currentRecord.get(currentSchema.getField( entityName).pos()); if (innerRecord == null) { currentRecord = null; break; } if (innerRecord != null && !(innerRecord instanceof IndexedRecord)) { throw new AvroConversionException(inputFieldName + " stepped through a non-record"); } currentRecord = (IndexedRecord) innerRecord; currentSchema = currentSchema.getField(entityName).schema(); currentSchema = getNonNullSchema(currentSchema); inputFieldName = inputFieldName.substring(dotIndex + 1); } Field f = currentSchema.getField(inputFieldName); if (currentRecord == null) { Schema s = null; if (f != null) { s = f.schema(); } result.put(outputField.name(), convertData(null, s, outputField.schema())); } else { result.put( outputField.name(), convertData(currentRecord.get(f.pos()), f.schema(), outputField.schema())); } } return result; } AvroRecordConverter(Schema inputSchema, Schema outputSchema,
            Map<String, String> fieldMapping);  AvroRecordConverter(Schema inputSchema, Schema outputSchema,
            Map<String, String> fieldMapping, Locale locale); Collection<String> getUnmappedFields(); Record convert(Record input); Schema getInputSchema(); Schema getOutputSchema(); }
AvroRecordConverter { public Record convert(Record input) throws AvroConversionException { Record result = new Record(outputSchema); for (Field outputField : outputSchema.getFields()) { String inputFieldName = outputField.name(); if (fieldMapping.containsKey(outputField.name())) { inputFieldName = fieldMapping.get(outputField.name()); } IndexedRecord currentRecord = input; Schema currentSchema = getNonNullSchema(inputSchema); while (inputFieldName.contains(".")) { int dotIndex = inputFieldName.indexOf('.'); String entityName = inputFieldName.substring(0, dotIndex); Object innerRecord = currentRecord.get(currentSchema.getField( entityName).pos()); if (innerRecord == null) { currentRecord = null; break; } if (innerRecord != null && !(innerRecord instanceof IndexedRecord)) { throw new AvroConversionException(inputFieldName + " stepped through a non-record"); } currentRecord = (IndexedRecord) innerRecord; currentSchema = currentSchema.getField(entityName).schema(); currentSchema = getNonNullSchema(currentSchema); inputFieldName = inputFieldName.substring(dotIndex + 1); } Field f = currentSchema.getField(inputFieldName); if (currentRecord == null) { Schema s = null; if (f != null) { s = f.schema(); } result.put(outputField.name(), convertData(null, s, outputField.schema())); } else { result.put( outputField.name(), convertData(currentRecord.get(f.pos()), f.schema(), outputField.schema())); } } return result; } AvroRecordConverter(Schema inputSchema, Schema outputSchema,
            Map<String, String> fieldMapping);  AvroRecordConverter(Schema inputSchema, Schema outputSchema,
            Map<String, String> fieldMapping, Locale locale); Collection<String> getUnmappedFields(); Record convert(Record input); Schema getInputSchema(); Schema getOutputSchema();  }