val sql="""select * from db.your_table where id>1""" val jdbcDF = spark.read .format("jdbc") .option("url", "jdbc:mysql:dbserver") .option("dbtable", s"( $sql ) t") .option("user", "username") .option("password", "password") .load()
write example
1 2 3 4 5 6 7 8
import org.apache.spark.sql.SaveMode
val prop=new java.util.Properties() prop.put("user","username") prop.put("password","yourpassword") val url="jdbc:mysql://host:3306/db_name" //df是一个dataframe,也就是你要写入的数据 df.write.mode(SaveMode.Append).jdbc(url,"table_name",prop)