@Test public void testInvalidTransformation() throws Exception { StructuredRecord record = StructuredRecord.builder( Schema.recordOf("dbrecord", Schema.Field.of("age", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("Age", Schema.of(Schema.Type.INT)))).set("age", "10").set("Age", 10).build(); try { StructuredRecordUtils.convertCase(record, FieldCase.toFieldCase("lower")); Assert.fail(); } catch (Exception e) { } }
public static StructuredRecord convertCase(StructuredRecord input, FieldCase fieldCase) throws Exception { if (fieldCase.equals(FieldCase.NONE)) { return input; } Schema oldSchema = input.getSchema(); Map<String, String> fieldNameMap = new HashMap<>(); List<Schema.Field> newFields = new ArrayList<>(); for (Schema.Field field : oldSchema.getFields()) { String newName = changeName(field.getName(), fieldCase); if (fieldNameMap.containsValue(newName)) { throw new IllegalStateException(String.format( "Duplicate field/column name %s found when trying to confirm to the chosen case option %s. " + "Check Database Table schema.", field.getName(), fieldCase)); } fieldNameMap.put(field.getName(), newName); newFields.add(Schema.Field.of(newName, field.getSchema())); } StructuredRecord.Builder recordBuilder = StructuredRecord.builder(Schema.recordOf("dbRecord", newFields)); for (Map.Entry<String, String> nameMap : fieldNameMap.entrySet()) { recordBuilder.set(nameMap.getValue(), input.get(nameMap.getKey())); } return recordBuilder.build(); }
StructuredRecordUtils { public static StructuredRecord convertCase(StructuredRecord input, FieldCase fieldCase) throws Exception { if (fieldCase.equals(FieldCase.NONE)) { return input; } Schema oldSchema = input.getSchema(); Map<String, String> fieldNameMap = new HashMap<>(); List<Schema.Field> newFields = new ArrayList<>(); for (Schema.Field field : oldSchema.getFields()) { String newName = changeName(field.getName(), fieldCase); if (fieldNameMap.containsValue(newName)) { throw new IllegalStateException(String.format( "Duplicate field/column name %s found when trying to confirm to the chosen case option %s. " + "Check Database Table schema.", field.getName(), fieldCase)); } fieldNameMap.put(field.getName(), newName); newFields.add(Schema.Field.of(newName, field.getSchema())); } StructuredRecord.Builder recordBuilder = StructuredRecord.builder(Schema.recordOf("dbRecord", newFields)); for (Map.Entry<String, String> nameMap : fieldNameMap.entrySet()) { recordBuilder.set(nameMap.getValue(), input.get(nameMap.getKey())); } return recordBuilder.build(); } }
StructuredRecordUtils { public static StructuredRecord convertCase(StructuredRecord input, FieldCase fieldCase) throws Exception { if (fieldCase.equals(FieldCase.NONE)) { return input; } Schema oldSchema = input.getSchema(); Map<String, String> fieldNameMap = new HashMap<>(); List<Schema.Field> newFields = new ArrayList<>(); for (Schema.Field field : oldSchema.getFields()) { String newName = changeName(field.getName(), fieldCase); if (fieldNameMap.containsValue(newName)) { throw new IllegalStateException(String.format( "Duplicate field/column name %s found when trying to confirm to the chosen case option %s. " + "Check Database Table schema.", field.getName(), fieldCase)); } fieldNameMap.put(field.getName(), newName); newFields.add(Schema.Field.of(newName, field.getSchema())); } StructuredRecord.Builder recordBuilder = StructuredRecord.builder(Schema.recordOf("dbRecord", newFields)); for (Map.Entry<String, String> nameMap : fieldNameMap.entrySet()) { recordBuilder.set(nameMap.getValue(), input.get(nameMap.getKey())); } return recordBuilder.build(); } private  StructuredRecordUtils(); }
StructuredRecordUtils { public static StructuredRecord convertCase(StructuredRecord input, FieldCase fieldCase) throws Exception { if (fieldCase.equals(FieldCase.NONE)) { return input; } Schema oldSchema = input.getSchema(); Map<String, String> fieldNameMap = new HashMap<>(); List<Schema.Field> newFields = new ArrayList<>(); for (Schema.Field field : oldSchema.getFields()) { String newName = changeName(field.getName(), fieldCase); if (fieldNameMap.containsValue(newName)) { throw new IllegalStateException(String.format( "Duplicate field/column name %s found when trying to confirm to the chosen case option %s. " + "Check Database Table schema.", field.getName(), fieldCase)); } fieldNameMap.put(field.getName(), newName); newFields.add(Schema.Field.of(newName, field.getSchema())); } StructuredRecord.Builder recordBuilder = StructuredRecord.builder(Schema.recordOf("dbRecord", newFields)); for (Map.Entry<String, String> nameMap : fieldNameMap.entrySet()) { recordBuilder.set(nameMap.getValue(), input.get(nameMap.getKey())); } return recordBuilder.build(); } private  StructuredRecordUtils(); static StructuredRecord convertCase(StructuredRecord input, FieldCase fieldCase); }
StructuredRecordUtils { public static StructuredRecord convertCase(StructuredRecord input, FieldCase fieldCase) throws Exception { if (fieldCase.equals(FieldCase.NONE)) { return input; } Schema oldSchema = input.getSchema(); Map<String, String> fieldNameMap = new HashMap<>(); List<Schema.Field> newFields = new ArrayList<>(); for (Schema.Field field : oldSchema.getFields()) { String newName = changeName(field.getName(), fieldCase); if (fieldNameMap.containsValue(newName)) { throw new IllegalStateException(String.format( "Duplicate field/column name %s found when trying to confirm to the chosen case option %s. " + "Check Database Table schema.", field.getName(), fieldCase)); } fieldNameMap.put(field.getName(), newName); newFields.add(Schema.Field.of(newName, field.getSchema())); } StructuredRecord.Builder recordBuilder = StructuredRecord.builder(Schema.recordOf("dbRecord", newFields)); for (Map.Entry<String, String> nameMap : fieldNameMap.entrySet()) { recordBuilder.set(nameMap.getValue(), input.get(nameMap.getKey())); } return recordBuilder.build(); } private  StructuredRecordUtils(); static StructuredRecord convertCase(StructuredRecord input, FieldCase fieldCase);  }