我有一个功能:
def PopulatePostgres(df: DataFrame ,df1: DataFrame,df2: DataFrame table: String): Result = {
val result = Try({
df
.write
.format("jdbc")
.mode(SaveMode.Append)
.option("url", config.url)
.option("user", config.username)
.option("password", config.password)
.option("dbtable", table)
.option("driver", "org.postgresql.Driver")
.save()
})
result match {
case Success(_) => Result(s"Created ${table}")
case Failure(problem) => {
log.error(problem.getMessage)
Result(s"Failed to create ${table}")
}
}
}
但是,我不确定如何将 3 个数据帧一一转储到 postgres 表中。所以我需要将 df、df1、df2 全部插入到 postgres 表中。有人能帮帮我吗
最佳答案
如果你想把所有的数据框存到同一个表中。
val findaldf = df.union(df1).union(df2)
然后您可以使用您的持久性逻辑。
但是所有的df都想单独存放
List(df, df1, df2).map(_.write.format("jdbc")
.mode(SaveMode.Append)
.option("url", config.url)
.option("user", config.username)
.option("password", config.password)
.option("dbtable", table)
.option("driver", "org.postgresql.Driver")
.save())
关于postgresql - 使用scala将多个数据帧插入函数中的postgres表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57429030/