Sparksql连接本地mysql数据库

spark版本1.6.2

package cn.itcast.spark.sql

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.sql.SQLContext

object SQL_TEST {
  def main(args: Array[String]): Unit = {
    // spark 上下文
    val conf = new SparkConf().setAppName("ReadMySQL").setMaster("local[1]")
    val sc = new SparkContext(conf)
    val sqlContext = new SQLContext(sc)

    import sqlContext.implicits._
    //  mysql 操作
    val jdbcDF1 = sqlContext.read.format("jdbc").options(Map(
      "url" -> "jdbc:mysql://localhost:3306/zba_dwa?characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true",
      "driver" -> "com.mysql.cj.jdbc.Driver",
      "dbtable" -> "app_name",
      "user" -> "root",
      "password" -> "123456")).load()

    jdbcDF1.registerTempTable("app_name")
    val sql= "select * from app_name"

    sqlContext.sql(sql).show()
    sc.stop()
  }
}

你可能感兴趣的:(spark,spark)