xiarixiaoyao commented on issue #5452:
URL: https://github.com/apache/hudi/issues/5452#issuecomment-1111963890
@santoshsb pls use follow code to solve your problem
```
def createNewDF(df: DataFrame, oldTableSchema: StructType): DataFrame = {
val writeSchema = df.schema
val neededSchema = {
val neededFields = mutable.ListBuffer[StructField]()
oldTableSchema.foreach(neededFields.append(_))
writeSchema.filterNot { col =>
oldTableSchema.exists(targetCol =>
SQLConf.get.resolver(targetCol.name, col.name))
}.foreach(neededFields.append(_))
StructType(neededFields)
}
val missingCols = {
neededSchema.zipWithIndex.map { case (field, index) =>
(index, writeSchema.indexWhere(p => SQLConf.get.resolver(p.name,
field.name)))
}.toMap
}
val filledRdd = df.rdd.mapPartitions { iter =>
iter.map { row =>
val tmp = new Array[Any](neededSchema.length)
for (i <- (0 to tmp.length - 1)) {
val index = missingCols.getOrDefault(i, -1)
tmp.update(i, if (index != -1) row.get(index) else null)
}
Row.fromSeq(tmp)
}
}
spark.createDataFrame(filledRdd, neededSchema)
}
val orgString =
"""{"resourceType":"Patient","id":"4ad86a5c-926e-439b-9352-f8ac9ab780f1","lastUpdated":"2022-03-11T15:18:18.90836+05:30","source":"4a0701fe-5c3b-482b-895d-875fcbd21481","gender":"male","birthDate":"1974-01-05","maritalStatus":{"coding":[{"system":"http://terminology.hl7.org/CodeSystem/v3-MaritalStatus","code":"M","display":"M"}],"text":"M"}}"""
val sqlContext = spark.sqlContext
import sqlContext.implicits._
val orgStringDf = spark.read.json(Seq(orgString).toDS())
val hudiOptions = Map[String,String](
HoodieWriteConfig.TABLE_NAME -> "patient_hudi",
DataSourceWriteOptions.TABLE_TYPE_OPT_KEY -> "COPY_ON_WRITE",
DataSourceWriteOptions.RECORDKEY_FIELD_OPT_KEY -> "id",
DataSourceWriteOptions.PARTITIONPATH_FIELD_OPT_KEY -> "source",
DataSourceWriteOptions.PRECOMBINE_FIELD_OPT_KEY -> "lastUpdated",
DataSourceWriteOptions.HIVE_STYLE_PARTITIONING_OPT_KEY -> "true"
)
orgStringDf.write
.format("org.apache.hudi")
.option(DataSourceWriteOptions.OPERATION_OPT_KEY,
DataSourceWriteOptions.INSERT_OPERATION_OPT_VAL)
.options(hudiOptions)
.mode(SaveMode.Overwrite)
.save("/tmp/default/clustering/updateTst/json_schema_tst/hudi")
val patienthudi =
spark.read.format("hudi").load("/tmp/default/clustering/updateTst/json_schema_tst/hudi")
val updatedString =
"""{"resourceType":"Patient","id":"596c7a94-bada-4303-85d4-7067c586999e","lastUpdated":"2022-04-20T15:18:18.90836+05:30","source":"4a0701fe-5c3b-482b-895d-875fcbd2148a","gender":"female","birthDate":"2005-08-30","multipleBirthBoolean":true}"""
val updatedStringDf =
createNewDF(spark.read.json(Seq(updatedString).toDS), patienthudi.schema)
updatedStringDf.write
.format("org.apache.hudi")
.options(hudiOptions)
.option(DataSourceWriteOptions.OPERATION_OPT_KEY,
DataSourceWriteOptions.UPSERT_OPERATION_OPT_VAL)
.option(DataSourceWriteOptions.PAYLOAD_CLASS_OPT_KEY,
"org.apache.hudi.common.model.EmptyHoodieRecordPayload")
.mode(SaveMode.Append)
.save("/tmp/default/clustering/updateTst/json_schema_tst/hudi")
val patienthudi1 =
spark.read.format("hudi").load("/tmp/default/clustering/updateTst/json_schema_tst/hudi")
patienthudi1.select("id","gender","maritalStatus").show(false)
```
--
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]