nleena123 commented on issue #5539:
URL: https://github.com/apache/hudi/issues/5539#issuecomment-1122307710

   we are adding the new field in hudi code with the below code
                **Schema.Field f = new Schema.Field(CDC_TS, 
Schema.createUnion(optionalString), CDC_TS, "null");**
   
   static GenericRecord getDataWithCDC_TS(GenericRecord record, String ts) {
   
                Schema base = record.getSchema();
                List<Schema.Field> baseFields = base.getFields().stream()
                                .map(field -> {
                                        Schema.Field f = new 
Schema.Field(field.name(), field.schema(), field.doc(), field.defaultVal());
                                        field.aliases().forEach(f::addAlias);
                                        return f;
                                })
                                .collect(Collectors.toList());
                List<Schema.Field> newFields = new ArrayList<>(baseFields);
                ArrayList<Schema> optionalString = new ArrayList<>();
                optionalString.add(Schema.create(Schema.Type.NULL));
                optionalString.add(Schema.create(Schema.Type.STRING));
                
                Schema.Field f = new Schema.Field(CDC_TS, 
Schema.createUnion(optionalString), CDC_TS, "null");
                if (!baseFields.contains(f))
                        newFields.add(f);
   
   
                Schema newSchema = Schema.createRecord(
                                base.getName(),
                                "New schema by adding a new field,CDC_TS",
                                base.getNamespace(), base.isError(),
                                newFields);
   
                GenericRecord newRecord = new GenericData.Record(newSchema);
   
                base.getFields().stream().forEach(field -> 
newRecord.put(field.name(), record.get(field.name())));
                newRecord.put(CDC_TS, ts);
   
                return newRecord;
   
        }


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to