sparkSQL读取并存储mysql

object sparkSqltoMySQL {

  def main(args: Array[String]): Unit = {
    val spark = SparkSession.builder().master("local[3]").appName(this.getclass).getOrCreate()
    import spark.implicits._
    /**
      * 两种spark读取jdbc的方法
      */
//1
//    spark.read.format("jdbc")
//      .option("url", "jdbc:mysql://localhost:3306/scala")
//      .option("dbtable","user")
//      .option("user","root")
//      .option("password","root")
//      .load().show()
//2
    val props = new Properties()
    props.put("user","root")
    props.put("password","root")

   val userDF = spark.read.jdbc("jdbc:mysql://localhost:3306/scala","user",props).show()
    //把userDF读出来的表存储为user1(转储数据库表)
//    userDF.write.jdbc("jdbc:mysql://localhost:3306/scala","user1",props)
    //把userDF读出来的表存储为持久表users
//    userDF.createOrReplaceTempView("users")
    //按条件查询出的结果存储为user3
    /*spark.sql("select userId,userName from users")
          .write.jdbc("jdbc:mysql://localhost:3306/scala","user3",props)*/
  }
}
————————————————
版权声明:本文为CSDN博主「yyf960126」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/yyf960126/article/details/80331804

 

posted @ 2022-02-17 11:32  shan_zhayidian  阅读(176)  评论(0编辑  收藏  举报