我有多个Spark作业,它们从不同的数据源读取,它们具有不同的模式,但非常接近,我想做的是将它们全部写入相同的Redshift表中,因此我需要统一所有DataFrame的模式,最好的方法是什么?
比如说第一个输入数据的架构是这样的:
第二个输入源的模式如下:
正如您在最终架构中所看到的那样,我有一些列可能不在输入架构中,因此应为null,某些列名称也应更改。而像
比如说第一个输入数据的架构是这样的:
val schema1 = StructType(Seq(
StructField("date", DateType),
StructField("campaign_id", StringType),
StructField("campaign_name", StringType),
StructField("platform", StringType),
StructField("country", StringType),
StructField("views", DoubleType),
StructField("installs", DoubleType),
StructField("spend", DoubleType)
))
第二个输入源的模式如下:
val schema2 = StructType(Seq(
StructField("date", DateType),
StructField("creator_id", StringType),
StructField("creator_name", StringType),
StructField("platform", StringType),
StructField("views", DoubleType),
StructField("installs", DoubleType),
StructField("spend", DoubleType),
StructField("ecpm", DoubleType)
))
表格模式(预期统一数据框):
val finalSchema = StructType(Seq(
StructField("date", DateType),
StructField("account_name", StringType),
StructField("adset_id", StringType),
StructField("adset_name", StringType),
StructField("campaign_id", StringType),
StructField("campaign_name", StringType),
StructField("pub_id", StringType),
StructField("pub_name", StringType),
StructField("creative_id", StringType),
StructField("creative_name", StringType),
StructField("platform", StringType),
StructField("install_source", StringType),
StructField("views", IntegerType),
StructField("clicks", IntegerType),
StructField("installs", IntegerType),
StructField("cost", DoubleType)
))
正如您在最终架构中所看到的那样,我有一些列可能不在输入架构中,因此应为null,某些列名称也应更改。而像
ecpm
这样的某些列应该被删除。