spark学习8.1:sparkSQL的数据库操作

本文介绍了如何在Spark Shell中通过JDBC连接MySQL,并在IntelliJ IDEA中实现数据的读写操作,包括配置驱动、读取表格和写入数据的步骤。同时,展示了创建DataFrame并验证数据操作的关键点。
摘要由CSDN通过智能技术生成

1.在spark-shell交互环境

 在启动spark-shell时候,必须指定 jar 和 driver类路径,这两个路径是一致的,都是mysql驱动的路径。
 

2.在idea中

2.1.添加mysql驱动

然后选择对应的jar包

2.2读取数据

import org.apache.spark.sql.SparkSession

object sparkSQLTestJDBC {
  def main(args: Array[String]): Unit = {

    //create spark commander
    val spark = SparkSession.builder().master("local").appName("sparkSQLTest").getOrCreate()

    //read
 val studentDF = spark.read.format("jdbc")
  .option("url", "jdbc:mysql://10.10.10.10:3306/dbName?serverTimezone=GMT") //IP and db name
  .option("driver", "com.mysql.jdbc.Driver")
  .option("dbtable", "student") //table name
  .option("user", "root")  //user name
  .option("password", "123456")  //password
  .load()

    //show
    studentDF.show()

  }

}

2.3写入数据

写入数据跟编程方式定义RDD一样

//for create table schema
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.SparkSession
import org.apache.spark.sql.types.{IntegerType, StringType, StructField, StructType}
import org.apache.spark.sql.Row

//for save connection info  
import java.util.Properties

object sparkSQLTestJDBCWrite {
  def main(args: Array[String]): Unit = {

    //create spark commander
    val spark = SparkSession.builder().appName("sparkSQLTest").master("local").getOrCreate()


    //create table schema
    val schema = StructType(List(StructField("id", IntegerType, true),
      StructField("name", StringType, true),
      StructField("score", IntegerType, true),
      StructField("course", StringType, true)
    ))

    //generate the insert data
    val insertRDD: RDD[String] = spark.sparkContext.parallelize(Array("6 墨子 66 地理", "7 孔子 86 英语", "8 非子 96 化学"))

    //split the rdd and into rows
    val insertDataDF = insertRDD.map(_.split(" "))
      .map(x => Row(x(0).trim.toInt, x(1), x(2).trim.toInt, x(3)))

    //merge
    val dataFrame = spark.createDataFrame(insertDataDF, schema)

    /*jdbc*/
    //for restore the connection info
    val prop = new Properties()

    prop.put("user","admin")
    prop.put("password","123456777")
    prop.put("driver", "com.mysql.jdbc.Driver")

    //write table
    dataFrame.write.mode("append")
      .jdbc("jdbc:mysql://10.10.10.10:3306/finereport?serverTimezone=GMT","student",prop)
    

  }

}

结果校验:

ps:在指定字段数据类型时候,注意包的选择

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值