xiarixiaoyao commented on a change in pull request #4786:
URL: https://github.com/apache/hudi/pull/4786#discussion_r816389411
##########
File path:
hudi-client/hudi-spark-client/src/main/scala/org/apache/hudi/AvroConversionUtils.scala
##########
@@ -60,7 +62,41 @@ object AvroConversionUtils {
def convertStructTypeToAvroSchema(structType: StructType,
structName: String,
recordNamespace: String): Schema = {
- getAvroSchemaWithDefaults(SchemaConverters.toAvroType(structType, nullable
= false, structName, recordNamespace))
+ val schema =
getAvroSchemaWithDefaults(SchemaConverters.toAvroType(structType, nullable =
false, structName, recordNamespace))
+ avroSchemaAddFiledDoc(schema, structType)
+ }
+
+ /**
+ *
+ * Return avro schema added the doc of fields
+ *
+ * @param schema Avro schema.
+ * @param structType Dataframe Struct Type.
+ * @return
+ */
+ def avroSchemaAddFiledDoc(schema: Schema, structType: StructType): Schema = {
+ val parentFields = ArrayBuffer[Schema.Field]()
+ val structFields = structType.fields
+ val fields = schema.getFields
+ structFields.foreach(structField => {
+ val i = structFields.indexOf(structField)
+ val field = fields.get(i)
+ val comment: String = if (structField.metadata.contains("comment")) {
+ structField.metadata.getString("comment")
+ } else {
+ field.doc
+ }
+ val newField: Schema.Field = new Schema.Field(field.name, field.schema,
if (StringUtils.isNullOrEmpty(comment)) field.doc
+ else comment, field.defaultVal)
+
+ field.getObjectProps.keys.foreach(key =>
newField.addProp(key,field.getObjectProp(key)))
+
+ parentFields.add(newField)
+ })
+
+ val mergedSchema: Schema = Schema.createRecord(schema.getName,
schema.getDoc, schema.getNamespace, false)
+ mergedSchema.setFields(parentFields.asJava)
+ mergedSchema
}
Review comment:
it will be better to merge this method into getAvroSchemaWithDefaults,
and support add doc to nested column
--
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]