【问题标题】:Executing update on JDBC Scala Postgresql在 JDBC Scala Postgresql 上执行更新
【发布时间】:2018-02-19 14:24:57
【问题描述】:

大家好,我想根据数据框更新 postgresql 中的表,但是没有任何帮助,谢谢

val local_pos = spark.load("jdbc", Map("url" -> url, "dbtable" -> "pos")).select("id", "name")
      val join = local_pos.join(TMP_SITE, local_pos("id") === TMP_SITE("SITE"), "inner")
      val temp = join.withColumn("changes", when(trim($"LIBELLE") === trim($"name"), lit("nothing")).otherwise("need an update"))

      //get row that need update
      val dataToBeUpdated = temp.filter($"changes" === "need an update")
      classOf[org.postgresql.Driver]
        val conn = DriverManager.getConnection(url)
        //   Configure to be Read Only
        val statement = conn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY)

        val result = dataToBeUpdated.collect().map { row =>
                  println("update pos set name = "+ row.getString(3).trim()+ "where id =" + row.getLong(0).toString() +";")
                val rs = statement.executeUpdate("update pos set name = "+ row.getString(3).trim()+ "where id =" + row.getLong(0).toString() +";")
                  println("tuple with " +  row.getLong(0) + "has been updateed")

        }
        println(result)

非常感谢,任何帮助将不胜感激

【问题讨论】:

  • 请编辑您的问题并重新格式化。我想我会编辑它,但现在它的质量非常低
  • 您写了“配置为只读”,然后您尝试更新。你确定这是正确的吗?
  • @T.Gawęda 我刚接触这个,请提供任何帮助。我这样做了,因为我已经将它用于删除请求,任何帮助谢谢

标签: sql scala apache-spark dataframe jdbc


【解决方案1】:
val local_pos = spark.load("jdbc", Map("url" -> url, "dbtable" -> "pos")).select("id", "name")

val join = local_pos.join(TMP_SITE, local_pos("id") === TMP_SITE("SITE"), "inner")

val temp = join.withColumn("changes", when(trim($"LIBELLE") === trim($"name"), lit("nothing")).otherwise("need an update"))
//get row that need update
val dataToBeUpdated = temp.filter($"changes" === "need an update")



classOf[org.postgresql.Driver]
val connection = DriverManager.getConnection(url)
dataToBeUpdated.collect().map(row =>

  {
    val ps = connection.prepareStatement("UPDATE pos SET name = ? WHERE id = ? ");
    ps.setString(1, row.getString(3));
    ps.setLong(2, row.getLong(0));
    ps.executeUpdate();
    println("update done successfully")
  })

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-06-29
    • 2012-03-13
    • 2011-04-05
    • 2014-02-20
    • 2020-10-18
    相关资源
    最近更新 更多