我有一个 mysql 表(portfolio),有以下两列(id,Status)和三个记录。
+----+--------+
| Id | Status |
+----+--------+
| 1 | 0 |
| 2 | 1 |
| 3 | 3 |
+----+--------+
我想将 Id=1 的状态列从 0 更新为 1
我在 Spark scala 中尝试的是:
val portfolio_id =1
val dbUrl ="jdbc:mysql://localhost:3306/test?user=root&password=root"
val prop = new java.util.Properties
prop.setProperty("user","root")
prop.setProperty("password","root")
val sqlContext = new org.apache.spark.sql.SQLContext(sc)
val portfolioDF=sqlContext.read.jdbc(dbUrl,"portfolio",prop)
portfolioDF.registerTempTable("portfolioDF")
val results =sqlContext.sql("SELECT status from portfolioDF where id="+portfolio_id)
//display the record
results.show()
//results.insertIntoJDBC(dbUrl, "portfolio", true);
现在我如何使用“结果”数据框来修改“状态”列值。 或者有没有其他方法可以通过数据帧修改 mysql 列值。 任何指导都会有帮助
提前致谢。
最佳答案
I would like to update the Status Column from 0 to 1 for Id=1
更新时使用UPDATE
而不是SELECT
UPDATE portfolioDF
SET status = 1
WHERE id = 1;
关于mysql - 使用 Spark Dataframe 更新 Mysql 列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32439269/