2017-09-11 91 views
-2

喜逢身體我想根據數據幀更新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) 

非常感謝,任何幫助將不勝感激

+0

請編輯您的問題並重新格式化它。我以爲我會編輯它,但現在質量很差 –

+0

您寫了「配置爲只讀」,然後嘗試更新。你確定這是正確的嗎? –

+0

@T.Gawęda剛剛對此感到陌生,please.i做到了這一點,因爲我已經使用它與刪除請求,任何幫助謝謝 –

回答

0
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") 
    })