The*_*ion 3 scala jdbc jenkins apache-spark
我正在使用 Spark,我想知道:如何通过对表 A 和 B 执行 sql 查询来创建名为 C 的临时表?
sqlContext
.read.json(file_name_A)
.createOrReplaceTempView("A")
sqlContext
.read.json(file_name_B)
.createOrReplaceTempView("B")
val tableQuery = "(SELECT A.id, B.name FROM A INNER JOIN B ON A.id = B.fk_id) C"
sqlContext.read
.format(SQLUtils.FORMAT_JDBC)
.options(SQLUtils.CONFIG())
.option("dbtable", tableQuery)
.load()
Run Code Online (Sandbox Code Playgroud)
您需要将结果保存为临时表
tableQuery .createOrReplaceTempView("dbtable")
Run Code Online (Sandbox Code Playgroud)
可以使用 JDBC 的外部表上的永久存储
val prop = new java.util.Properties
prop.setProperty("driver", "com.mysql.jdbc.Driver")
prop.setProperty("user", "vaquar")
prop.setProperty("password", "khan")
//jdbc mysql url - destination database is named "temp"
val url = "jdbc:mysql://localhost:3306/temp"
//destination database table
val dbtable = "sample_data_table"
//write data from spark dataframe to database
df.write.mode("append").jdbc(url, dbtable, prop)
Run Code Online (Sandbox Code Playgroud)
https://docs.databricks.com/spark/latest/data-sources/sql-databases.html
http://spark.apache.org/docs/latest/sql-programming-guide.html# Saving-to-persistent-tables
| 归档时间: |
|
| 查看次数: |
29779 次 |
| 最近记录: |