36 lines
788 B
Scala
36 lines
788 B
Scala
package org.example.sql
|
|
|
|
import org.apache.spark.sql.SparkSession
|
|
|
|
object DataFrameSql {
|
|
def main(args: Array[String]): Unit = {
|
|
val spark = SparkSession.builder()
|
|
.master("local[*]")
|
|
.appName("DataFrameDemo")
|
|
.getOrCreate()
|
|
|
|
val dfSql = spark.read
|
|
.format("jdbc")
|
|
.option("url", "jdbc:mysql://192.168.48.10:3306/gmall")
|
|
.option("dbtable", "user_info")
|
|
.option("user", "root")
|
|
.option("password", "root123456")
|
|
.load()
|
|
|
|
println("============show============")
|
|
dfSql.show()
|
|
|
|
println("============sql============")
|
|
dfSql.createTempView("user_info")
|
|
spark.sql(
|
|
"""
|
|
|select
|
|
|login_name
|
|
|,name
|
|
|,user_level
|
|
|from user_info
|
|
|""".stripMargin).show()
|
|
|
|
}
|
|
}
|