Databricks Spark: How to pass value from Python/Scala to Spark SQL
This article will explain how to use Python or Scala variables in Spark SQL without wrapping the SQL statement with spark.sql.
Step 1: Create a new table
%sqldrop table if exists tbl_friends;create table tbl_friends(name string, age int);Step 2: Scala variable assignment.
One important thing to remember is to use a two-part namespace inside spark.conf.
Using a variable without namespace will result in NULL.
In this example, we have used myapplication.name, feel free to use change based on your requirement.
%scalaval age = 30
val name = "Rachel Green"spark.conf.set("myapplication.name", name)
spark.conf.set("myapplication.age", age)Step 3: Access the spark.conf.set variable from Spark SQL
Numeric values can be used with or without quotes.
'${myapplication.age}' or ${myapplication.age}Inserting data into table tbl_friends
%sqlINSERT INTO tbl_friends values ('${myapplication.name}',${myapplication.age})Step 4: Python variable assignment
%pythonage = 31
name = "Ross Geller"spark.conf.set("myapplication.name", name)
spark.conf.set("myapplication.age", age)Step 5: Insert new values into Table
%sqlINSERT INTO tbl_friends values ('${myapplication.name}',${myapplication.age})Step 6: Query the data
%sqlselect * from tbl_friends
%sqlselect * from tbl_friends where age = ${myapplication.age}





