cancel
Showing results for 
Search instead for 
Did you mean: 
Data Engineering
Join discussions on data engineering best practices, architectures, and optimization strategies within the Databricks Community. Exchange insights and solutions with fellow data engineers.
cancel
Showing results for 
Search instead for 
Did you mean: 

How can I access python variable in Spark SQL?

haseebkhan1421
New Contributor

I have python variable created under %python in my jupyter notebook file in Azure Databricks. How can I access the same variable to make comparisons under %sql. Below is the example:

%python

RunID_Goal = sqlContext.sql("SELECT CONCAT(SUBSTRING(RunID,1,6),'01_',SUBSTRING(RunID,1,6),'01_') FROM RunID_Pace").first()[0] + RunID_Pace[18:] RunID_Goal

%sql

SELECT Type , KPIDate, Value

FROM table

WHERE

RunID = RunID_Goal (This is the variable created under %python and want to compare over here)

When I run this it throws an error:

Error in SQL statement: AnalysisException: cannot resolve '`RunID_Goal`' given input columns:

I am new azure databricks and spark sql any sort of help would be appreciated.

1 ACCEPTED SOLUTION

Accepted Solutions

morganmazouchi
Databricks Employee
Databricks Employee

@haseebkhan1421​ , You can create a temporary view table in your python cell using "DataFrame.createOrReplaceTempView("table_name")" and query that table in SQL. check here for seeing an example

View solution in original post

2 REPLIES 2

morganmazouchi
Databricks Employee
Databricks Employee

@haseebkhan1421​ , You can create a temporary view table in your python cell using "DataFrame.createOrReplaceTempView("table_name")" and query that table in SQL. check here for seeing an example

Nirupam
New Contributor III

You can use {} in spark.sql() of pyspark/scala instead of making a sql cell using %sql.

This will result in a dataframe. If you want you can create a view on top of this using createOrReplaceTempView()

Below is an example to use a variable:-

# A variable

var = "Hello World"

# Using f in pyspark spark.sql.

spark.sql(f""" SELECT '{var}' AS Message """)

# Using format

spark.sql(""" SELECT '{}' AS Message """.format(var))

Connect with Databricks Users in Your Area

Join a Regional User Group to connect with local Databricks users. Events will be happening in your city, and you won’t want to miss the chance to attend and share knowledge.

If there isn’t a group near you, start one and help create a community that brings people together.

Request a New Group