7

I am using Spark and I would like to know: how to create temporary table named C by executing sql query on tables A and B ?

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()

2 Answers 2

8

You need to save your results as temp table

tableQuery .createOrReplaceTempView("dbtable")

Permanant storage on external table you can use 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)

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

Sign up to request clarification or add additional context in comments.

Comments

2
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.sql(tableQuery).createOrReplaceTempView("C")

Try the above code it will work.

1 Comment

Your answer is working, unfortunatly I can not use sqlContext.sql since it will not execute the entire request in the database. Indeed, sqlContext.sql can split the query into two select and then execute the join on diferrents worker. In my case, the workers have not enough memory to handle it and they crash...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.