postgresql - 使用 scala 在函数中将多个 Dataframes 插入到 postgres 表中
问题描述
我有一个功能:
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())
推荐阅读
- struct - 从 Octave 结构中提取给定字段名的所有行的值
- github - 请求更改的审核后如何在 github 上批准审核?
- angular - 角度 http 帖子从不调用后端操作
- amazon-web-services - 如何限制从 Amazon S3 存储桶读取的次数
- reinforcement-learning - 学习率衰减到累积奖励?
- r - ggplot2 将数据标签添加到分组直方图
- sql - SQL 查询以获取最近 24 小时的连续小时数,可能会丢失小时数
- firebase-realtime-database - 允许一个用户写入另一个用户的数据是否安全?
- android - 如何下载预编译的训练模型并将其包含到我的项目中?
- c# - Nuget 包应该添加到我的解决方案的每个项目中吗?