result.foreachPartition(
it => {
var url = "jdbc:mysql://地址/库名?characterEncoding=utf8"
val conn = DriverManager.getConnection(url, "user", "password")
val pstat = conn.prepareStatement("INSERT IGNORE INTO `表名 (`geo_hash`, `month_sale`, `mark_size`) VALUES (?, ?,?)")
for (obj <- it) {
pstat.setString(1, obj._1)
pstat.setInt(2, obj._2)
pstat.setInt(3, obj._3)
pstat.addBatch
}
try {
pstat.executeBatch
} finally {
pstat.close
conn.close
}
}
)
Spark 写入Mysql
最新推荐文章于 2022-07-21 11:06:02 发布