Jerry Nixon
Jerry Nixon

Reputation: 31823

Set a databricks python variable with a %sql statement

The code in cell 1 works just fine.

The problem

I just don't know the correct keywords to search to see how I can make the code in cells 2 & 3 work. Basically, in a %sql cell, can I select into a variable that can be later used in a python cell? I realize this may not even be possible. Cheers!

cell 1

ds = spark.sql("select * from duamonds")
display(ds)

cell 2

%sql
select * from diamonds

cell 3

display({ sql result })

Upvotes: 2

Views: 4150

Answers (2)

Marc
Marc

Reputation: 3323

It CAN be done:

According to the cell in my DB right now (runtime 11):

SQL cell result stored as PySpark data frame _sqldf. Learn more

Upvotes: 0

Anupam Chand
Anupam Chand

Reputation: 2687

It cannot be done. However, you can do this:

Cell1

sqlresult= sqlContext.sql("select * from diamonds")

Cell2

display(sqlresult)

You can find more details on using SQL with dataframes HERE.

Upvotes: 3

Related Questions