postgresql - 使用scala将多个数据帧插入函数中的postgres表

标签 postgresql scala dataframe apache-spark

我有一个功能:

 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/

相关文章:

python - Pandas 从数据帧的所有列中减去数据帧中的每一列,并将结果写入第三个数据帧

r - 基于最小 5 个连续值的向量序列的子集数据帧

ruby-on-rails - Postgresql数据到highchart

node.js - Sequelize 迁移 : ERROR: Cannot read property 'toString' of undefined

scala - 使用反射和解释器动态解析字符串并在 Scala 中返回一个函数

java - Spark 历史日志手动解压

python - 如何使用自定义顺序按两列对 DataFrame 进行排序?

sql - 在 SELECT ... WHERE ... ANY 中使用来自 CTE 的数组

sql-server - 唯一的 RID 会是 "overflow"吗?

scala - 如何在 spark Dataframe 中使用 Except 函数