【发布时间】: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