spark读取mysql某列值,封装成List集合
def main(args: Array[String]) = {
Logger.getLogger("org").setLevel(Level.ERROR)
val sparkSession = SparkSession
.builder()
.master("local[*]")
.appName("readFromMysql")
.config("spark.sql.broadcastTimeout", "36000")
.getOrCreate()
val mysqlReader: DataFrame = sparkSession.read.format("jdbc")
.option("url", "jdbc:mysql://localhost:3306/online?rewriteBatchedStatements=true")
.option("dbtable", "student")
.option("user", "root")
.option("password", "szroot")
.load()
mysqlReader.createOrReplaceTempView("student")
val student: List[Row] = sparkSession.sql("select uv from student").rdd.collect().toList
val strings: List[String] = student.map(row => row.mkString)
for (elem <- strings) {
println(elem)
}
}
网友评论