@Test public void testLowerAndUpperCaseTransformation() throws Exception { StructuredRecord record = StructuredRecord.builder( Schema.recordOf("dbrecord", Schema.Field.of("Name", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("Age", Schema.of(Schema.Type.INT)))).set("Name", "Abcd").set("Age", 10).build(); StructuredRecord upperCaseRecord = StructuredRecordUtils.convertCase( record, FieldCase.toFieldCase("upPer")); Assert.assertEquals("Abcd", upperCaseRecord.get("NAME")); Assert.assertEquals(10, upperCaseRecord.<Integer>get("AGE").intValue()); Assert.assertNull(upperCaseRecord.get("Age")); Assert.assertNull(upperCaseRecord.get("Name")); StructuredRecord lowerCaseRecord = StructuredRecordUtils.convertCase( record, FieldCase.toFieldCase("lowEr")); Assert.assertEquals("Abcd", lowerCaseRecord.get("name")); Assert.assertEquals(10, lowerCaseRecord.<Integer>get("age").intValue()); Assert.assertNull(upperCaseRecord.get("Age")); Assert.assertNull(upperCaseRecord.get("Name")); StructuredRecord noChangeRecord = StructuredRecordUtils.convertCase( record, FieldCase.toFieldCase("no change")); Assert.assertEquals("Abcd", noChangeRecord.get("Name")); Assert.assertEquals(10, noChangeRecord.<Integer>get("Age").intValue()); }
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);  }