头图

这是不需要改源码的方式

//user表样例类
case class User1(id: Long, name: String, password: String, imgUrl: String, update_date: String)

object SparkSQLUpdateMySQLOfJDBC {

  def main(args: Array[String]): Unit = {
    //SparkSession
    val spark: SparkSession = SparkSession.builder()
      .appName("SparkSqlToMysql")
      .master("local")
      .getOrCreate()

    //读取json/csv文件数据
    val df = spark.read.json("data/user.json")
    df.show()
    val data: Array[Row] = df.collect()

    //创建数据库连接
    val connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/ssm?characterEcoding=UTF-8",
      "root", "000000")
      
    //声明执行的SQL
    val statement = connection.prepareStatement("update user set name=?, password=?, imgUrl=?, update_date=? where id=?")

    //组装参数
    val now: String = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss").format(LocalDateTime.now)
    data.foreach(
      u => {
        // statement.setObject(sql参数占位符的位置, 样例类的属性)
        statement.setObject(1, u.getString(2))
        statement.setObject(2, u.getString(3))
        statement.setObject(3, u.getString(4))
        statement.setObject(4, now)
        statement.setObject(5, u.getLong(0))
        statement.addBatch() //批量执行
      }
    )

    //执行SQL
    statement.executeBatch()

    spark.stop()
  }
}

写入中文后有出现乱码'??'的情况, 有知道的大佬指点一下


打了个冷颤
19 声望0 粉丝

且听风吟