object _01_ReadJDBC {
def main(args: Array[String]): Unit = {
//1、创建spark session
val spark: SparkSession = SparkSession.builder().master("local[*]").getOrCreate()
val properties = new Properties()
properties.setProperty("user","root")
properties.setProperty("password","123456")
properties.setProperty("query","id >= 2") //Both 'dbtable' and 'query' can not be specified at the same time.
//从jdbc中读取数据
val dataFrame = spark.read.jdbc("jdbc:mysql://localhost:3306/sql_01?characterEncoding=utf8",
"stu", properties)
//加上过滤条件
dataFrame.createTempView("jdbc_01")
val frame = spark.sql(
"""
|select id,name
|from
|jdbc_01
|where id >= 2;
|""".stripMargin)
frame.show()
spark.stop()
}
}