pom.xml:https://cloud.tencent.com/developer/article/1818625
SparkJDBCExample.scala
package com.xtd.spark.imooc
import org.apache.spark.sql.SparkSession
object SparkJDBCExample {
def main(args: Array[String]): Unit = {
// 创建一个SparkSession对象
val spark = SparkSession.builder
.master("local")
.appName("SparkJDBCExample")
.config("spark.some.config.option", "some-value")
.getOrCreate()
// 创建一个sparkDataFrame对象
val jdbcDF = spark.read.format("jdbc")
.option("url", "jdbc:mysql://127.0.0.1:3306")
.option("dbtable", "test.xy")
.option("driver", "com.mysql.jdbc.Driver")
.option("user", "root")
.option("password", "123456")
.load()
// 打印表schema
jdbcDF.printSchema()
// 打印表所有数据
jdbcDF.show()
}
}
运行结果
MySQL表