Spark读写MySQL

18 篇文章 0 订阅

1. Spark读取MySQL数据

         1. spark.read.jdbc()

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

    val prop = new Properties()
    prop.put("user", "root")
    prop.put("password", "863863")

    val url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"

    val dataFrame = spark.read.jdbc(url,"test01",prop).select("id").where("id >= 3").show()

    spark.stop()
  }

         2. spark.read.format().option().load()

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

    //useUnicode=true&characterEncoding=UTF-8 编码
    //serverTimezone=UTC 时区
    val dataDF = spark.read.format("jdbc")
      .option("url","jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC")
      .option("dbtable","test01")
      .option("user","root")
      .option("password","863863")
      .load()

    dataDF.createOrReplaceTempView("tmptable")

    val sql = "select * from tmptable where id >= 3"

    spark.sql(sql).show()

    spark.stop()
  }
}

 

2. Spark写数据到MySQL

         spark.write.mode().jdbc()

         1. 查询后写入

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

    val prop = new Properties()
    prop.put("user", "root")
    prop.put("password", "863863")

    val url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"

    val dataFrame = spark.read.jdbc(url,"test01",prop).where("id >= 3")

    dataFrame.write.mode(SaveMode.Append).jdbc(url,"test02",prop)

    spark.stop()
  }

         1. 通过构建DataFrame再写入

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

    val prop = new Properties()
    prop.put("user","root")
    prop.put("password","863863")

    val url = "jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&serverTimezone=UTC"

    val rdd = spark.sparkContext.textFile("d://data/word.txt")

    val rdd2 = rdd.flatMap(_.split(",")).distinct().zipWithIndex().map(t =>{Row(t._2,t._1)})

    val schema = StructType{
      List(
        StructField("id",LongType,true),
        StructField("user",StringType,true)
      )}

    val dataFrame = spark.createDataFrame(rdd2,schema)

    dataFrame.write.mode(SaveMode.Overwrite).jdbc(url,"test02",prop)

    spark.stop()
  }

3. 演示

         1. spark.read.jdbc() 

         2. spark.read.format().option().load()

         3. Spark查询数据写入MySQL

         4. 构建DataFrame写入MySQL

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

訾零

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值