lufzhangzitao commented on issue #7283:
URL: https://github.com/apache/hudi/issues/7283#issuecomment-1330251680

   @xiarixiaoyao 
   
   I use spark df add a new string field and change the datatype of a field
   
   
       val tableName: String = "hudi_cow_test"
       val tablePath: String = s"D:/data/data/$tableName"
   
       val schema = StructType(Array(
         StructField("rowId", StringType, true),
         StructField("partitionId", StringType, true),
         StructField("preComb", LongType, true),
         StructField("name", StringType, true),
         StructField("versionId", StringType, true),
         StructField("intToLong", IntegerType, true)
       ))
   
       val data1 = Seq(Row("row_1", "part_0", 0L, "bob", "v_0", 0),
         Row("row_2", "part_0", 0L, "john", "v_0", 0),
         Row("row_3", "part_0", 0L, "tom", "v_0", 0))
   
   
       var dfFromData1 = spark.createDataFrame(data1, schema)
       dfFromData1.write
         .mode(SaveMode.Append)
         .format("hudi")
         .option("hoodie.datasource.write.operation", "upsert")
         .option("hoodie.datasource.write.reconcile.schema", value = true)
         .option("hoodie.schema.on.read.enable", value = true)
         .option(PRECOMBINE_FIELD.key(), "preComb")
         .option(RECORDKEY_FIELD.key(), "rowId")
         .option(KeyGeneratorOptions.PARTITIONPATH_FIELD_NAME.key(), 
"partitionId")
         .option(INDEX_TYPE.key(), HoodieIndex.IndexType.BLOOM.name())
         .option(TBL_NAME.key(), tableName)
         .save(tablePath)
   
       spark.read
         .format("org.apache.hudi").load(tablePath).show()
   
       // In the new schema, we are going to add a String field and
       // change the datatype `intToLong` field from  int to long.
       val newSchema = StructType(Array(
         StructField("rowId", StringType, true),
         StructField("partitionId", StringType, true),
         StructField("preComb", LongType, true),
         StructField("name", StringType, true),
         StructField("versionId", StringType, true),
         StructField("intToLong", LongType, true),
         StructField("newField", StringType, true)
       ))
   
       val data2 = Seq(
         Row("row_2", "part_0", 5L, "john", "v_1", 1L, "newField_1"),
         Row("row_5", "part_0", 5L, "maroon", "v_1", 5L, "newField_1"),
         Row("row_9", "part_0", 5L, "michael", "v_1", 3L, "newField_1"))
   
       var dfFromData2 = spark.createDataFrame(data2, newSchema)
       dfFromData2.write
         .mode(SaveMode.Append)
         .format("hudi")
         .option("hoodie.datasource.write.operation", "upsert")
         .option("hoodie.datasource.write.reconcile.schema", value = true)
         .option("hoodie.schema.on.read.enable", value = true)
         .option(PRECOMBINE_FIELD.key(), "preComb")
         .option(RECORDKEY_FIELD.key(), "rowId")
         .option(KeyGeneratorOptions.PARTITIONPATH_FIELD_NAME.key(), 
"partitionId")
         .option(INDEX_TYPE.key(), HoodieIndex.IndexType.BLOOM.name())
         .option(TBL_NAME.key(), tableName)
         .save(tablePath)
   
       spark.read
         .format("org.apache.hudi").load(tablePath).show()
   
       spark.stop()


-- 
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