Spark SQL passing variables - Synapse (Spark pool)

Anaid 61 Reputation points
2021-06-02T13:23:02.38+00:00

I have the following SparkSQL (Spark pool - Spark 3.0) code and I want to pass a variable to it. How can I do that? I tried the following:

#cel 1 (Toggle parameter cell):
%%pyspark
stat = 'A'

#cel2:
select * from silver.employee_dim where Status= '$stat'
Azure Synapse Analytics
Azure Synapse Analytics
An Azure analytics service that brings together data integration, enterprise data warehousing, and big data analytics. Previously known as Azure SQL Data Warehouse.
4,935 questions
{count} votes

3 answers

Sort by: Most helpful
  1. HimanshuSinha-msft 19,476 Reputation points Microsoft Employee
    2021-06-03T01:44:12.58+00:00

    Hello @,
    Thanks for the ask and using the Microsoft Q&A platform .

    I tried the below snippet and it worked , Please do let me know how it goes .
    cell1

    %%pyspark  
    tablename = "yourtablename"  
    

    cell2

    %%pyspark  
    query = "SELECT * FROM {}".format(tablename)  
    print (query)  
    from pyspark.sql import SparkSession  
    spark = SparkSession.builder.appName("sample").getOrCreate()  
    df2 = spark.sql(query)   
    df2.show()  
    

    101799-image.png

    Thanks
    Himanshu
    Please do consider clicking on "Accept Answer" and "Up-vote" on the post that helps you, as it can be beneficial to other community members

    0 comments No comments

  2. Anaid 61 Reputation points
    2021-06-03T11:23:12.89+00:00

    Hi Himanshu,

    Thank you for your reply. But we would like to keep the second cell as SQL.
    We are evaluating to migrate some Databricks notebooks to Spark pool. For example:
    102055-image.png

    Thanks

    0 comments No comments

  3. Anaid 61 Reputation points
    2021-06-03T11:25:57.007+00:00

    I found this solution. However, I don't know if it is the best solution:

    102088-image.png


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.