def main(args: Array[String]): Unit = {
println("Program Start Running")
val properties = new Properties()
properties.setProperty("user","root")
properties.setProperty("password","123456")
/**
* 读取mysql的第一种方式
*/
val mysql_url = "jdbc:mysql://10.10.1.10:3306/test"
// val sql_str = "data_example"
val sql_str = "(select * from test.data_example) T "
val sourceDF1: DataFrame = spark.read.jdbc(mysql_url, sql_str, properties)
sourceDF1.show()
/**
* 读取mysql的第二种方式
*/
val map: Map[String, String] = Map[String, String](
elems = "url" -> mysql_url,
"driver" -> "com.mysql.jdbc.Driver",
"user" -> "root",
"password" -> "123456",
"dbtable" -> data_example
)
val sourceDF2: DataFrame = spark.read.format("jdbc").options(map).load
sourceDF2.show()
/**
* 读取mysql的第三种方式
*/
val reader: DataFrameReader = spark.read.format("jdbc")
.option("url", mysql_url)
.option("driver", "com.mysql.jdbc.Driver")
.option("user", "root")
.option("password", "123456")
.option("dbtable", sql_str)
val sourceDF3: DataFrame = reader.load()
sourceDF3.show()
/**
* 写mysql
*/
sourceDF1.write.mode(SaveMode.Overwrite).jdbc(mysql_url,"data_example_tmp", properties)
println("Program Finish Running")
println("-+*" * 50)
spark.stop()
}
Spark读写mysql
最新推荐文章于 2023-12-13 16:37:21 发布