Spark Dataframes UPSERT to Postgres Table

前端 未结 4 2098
北荒
北荒 2020-11-30 03:50

I am using Apache Spark DataFrames to join two data sources and get the result as another DataFrame. I want to write the result to another Postgres table. I see this option

4条回答
  •  萌比男神i
    2020-11-30 04:12

    If you are going to do it manually and via option 1 mentioned by zero323, you should take a look at Spark source code for the insert statement here

      def insertStatement(conn: Connection, table: String, rddSchema: StructType): PreparedStatement = {
        val columns = rddSchema.fields.map(_.name).mkString(",")
        val placeholders = rddSchema.fields.map(_ => "?").mkString(",")
        val sql = s"INSERT INTO $table ($columns) VALUES ($placeholders)"
        conn.prepareStatement(sql)
      }
    

    The PreparedStatement is part of java.sql and it has methods like execute() and executeUpdate(). You still have to modify the sql accordingly, of course.

提交回复
热议问题