def main(args: Array[String]): Unit = {
val spark = SparkSession.builder()//创建 spark sql 的开始。必不可少
.master(“local[2]”)//地址
.appName(“ReadFromMysql”)//名字
.getOrCreate()//结束,必不可少
//从mysql读取数据
val prop = new Properties()//容器,存放用户名和密码
prop.setProperty("user","root")
prop.setProperty("password","123456")
val mysqlDF = spark.read.jdbc("jdbc:mysql://192.168.72.142:3306/testA","student",prop)
//年龄大于20的保存到数据库中
mysqlDF.createTempView("t")
val resDF = spark.sql("select * from t where age > 20")
resDF.write.mode("overwrite").jdbc("jdbc:mysql://192.168.72.142:3306/testA","student_copy",prop)
spark.stop()
}