spark连接mysql的几种方式

import java.util.Properties

import org.apache.spark.sql.{DataFrameReader, SparkSession}


object testMysql2 {

  def main(args: Array[String]): Unit = {
    //创建sparkSession
    val spark =
      SparkSession.builder()
        .appName("MysqlSupport")
        .master("local")
        //  .master("spark://ip:7077")
        .config("spark.executor.memory", "512m")
        .getOrCreate()


    val tm = new testMysql2()
    tm.method1(spark)
    //method2(spark)
    //method3(spark)

    spark.stop()
  }

}



 class testMysql2 {
  def method1 (sparkSession: SparkSession): Unit ={
    val properties = new Properties()
    properties.setProperty("user", "root")
    properties.setProperty("password" ,"123456")

    val test = sparkSession.read.jdbc("jdbc:mysql://ip/test?useUnicode=true&characterEncoding=utf8&useSSL=false","company_category_code",properties)

    test.show()

  }




  def method2(spark: SparkSession): Unit = {
    val url = "jdbc:mysql://ip/test?useUnicode=true&characterEncoding=utf8&useSSL=false"
    val map: Map[String, String] = Map[String, String](
      elems = "url" -> url,
      "driver" -> "com.mysql.jdbc.Driver",
      "user" -> "root",
      "password" -> "123456",
      "dbtable" -> "crm_company"
    )
    val cm = spark.read.format("jdbc").options(map).load

    println("method2: cm show")
    cm.show(2)
  }

  def method3(spark: SparkSession): Unit = {
    val url = "jdbc:mysql://ip/test?useUnicode=true&characterEncoding=utf8&useSSL=false"
    val reader: DataFrameReader = spark.read.format("jdbc")
      .option("url", url)
      .option("driver", "com.mysql.jdbc.Driver")
      .option("user", "root")
      .option("password", "123456")
      .option("dbtable", "crm_company")

    val cm = reader.load()
    println("method3: cm show")
    cm.show(3)


  }

}

  

spark连接mysql的几种方式

上一篇:declare常见用法


下一篇:Opencv4文档阅读