我正在使用 Spark sql DataSet 将数据写入配置单元。如果架构相同,它可以正常工作,但是如果我更改 avro 架构,在两者之间添加新列,它会显示错误(架构是从架构注册表提供的)
Error running job streaming job 1519289340000 ms.0
org.apache.spark.sql.AnalysisException: The column number of the existing table default.sample(struct<collection_timestamp:bigint,managed_object_id:string,managed_object_type:string,if_admin_status:string,date:string,hour:int,quarter:bigint>) doesn't match the data schema(struct<collection_timestamp:bigint,managed_object_id:string,if_oper_status:string,managed_object_type:string,if_admin_status:string,date:string,hour:int,quarter:bigint>);
if_oper_status
是必须添加新列。请建议。
StructType struct = convertSchemaToStructType(SchemaRegstryClient.getLatestSchema("simple"));
Dataset<Row> dataset = getSparkInstance().createDataFrame(newRDD, struct);
dataset=dataset.withColumn("date",functions.date_format(functions.current_date(), "dd-MM-yyyy"));
dataset=dataset.withColumn("hour",functions.hour(functions.current_timestamp()));
dataset=dataset.withColumn("quarter",functions.floor(functions.minute(functions.current_timestamp()).divide(5)));
dataset
.coalesce(1)
.write().mode(SaveMode.Append)
.option("charset", "UTF8")
.partitionBy("date","hour","quarter")
.option("checkpointLocation", "/tmp/checkpoint")
.saveAsTable("sample");