<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: How can I access python variable in Spark SQL? in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/how-can-i-access-python-variable-in-spark-sql/m-p/16518#M10696</link>
    <description>&lt;P&gt;@haseebkhan1421​&amp;nbsp;, You can create a temporary view table in your python cell using "&lt;B&gt;DataFrame.createOrReplaceTempView("table_name")" &lt;/B&gt;and query that table in SQL. check &lt;A href="https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.sql.DataFrame.createOrReplaceTempView.html" alt="https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.sql.DataFrame.createOrReplaceTempView.html" target="_blank"&gt;here&lt;/A&gt; for seeing an example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
    <pubDate>Fri, 17 Sep 2021 17:19:45 GMT</pubDate>
    <dc:creator>morganmazouchi</dc:creator>
    <dc:date>2021-09-17T17:19:45Z</dc:date>
    <item>
      <title>How can I access python variable in Spark SQL?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-can-i-access-python-variable-in-spark-sql/m-p/16517#M10695</link>
      <description>&lt;P&gt;&lt;/P&gt;
&lt;P&gt;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:&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;%python&lt;P&gt;&lt;/P&gt;
&lt;P&gt;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&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;%sql
&lt;P&gt;&lt;/P&gt;SELECT Type , KPIDate, Value 
&lt;P&gt;&lt;/P&gt;FROM table 
&lt;P&gt;&lt;/P&gt;WHERE 
&lt;P&gt;&lt;/P&gt;RunID = RunID_Goal (This is the variable created under %python and want to compare over here)
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;When I run this it throws an error:
&lt;P&gt;&lt;/P&gt;Error in SQL statement: AnalysisException: cannot resolve '`RunID_Goal`' given input columns:
&lt;P&gt;I am new azure databricks and spark sql any sort of help would be appreciated. &lt;/P&gt; 
&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 15 Aug 2021 18:00:17 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-can-i-access-python-variable-in-spark-sql/m-p/16517#M10695</guid>
      <dc:creator>haseebkhan1421</dc:creator>
      <dc:date>2021-08-15T18:00:17Z</dc:date>
    </item>
    <item>
      <title>Re: How can I access python variable in Spark SQL?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-can-i-access-python-variable-in-spark-sql/m-p/16518#M10696</link>
      <description>&lt;P&gt;@haseebkhan1421​&amp;nbsp;, You can create a temporary view table in your python cell using "&lt;B&gt;DataFrame.createOrReplaceTempView("table_name")" &lt;/B&gt;and query that table in SQL. check &lt;A href="https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.sql.DataFrame.createOrReplaceTempView.html" alt="https://spark.apache.org/docs/latest/api/python/reference/api/pyspark.sql.DataFrame.createOrReplaceTempView.html" target="_blank"&gt;here&lt;/A&gt; for seeing an example&lt;/P&gt;&lt;P&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 17 Sep 2021 17:19:45 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-can-i-access-python-variable-in-spark-sql/m-p/16518#M10696</guid>
      <dc:creator>morganmazouchi</dc:creator>
      <dc:date>2021-09-17T17:19:45Z</dc:date>
    </item>
    <item>
      <title>Re: How can I access python variable in Spark SQL?</title>
      <link>https://community.databricks.com/t5/data-engineering/how-can-i-access-python-variable-in-spark-sql/m-p/16520#M10698</link>
      <description>&lt;P&gt;You can use {} in spark.sql() of pyspark/scala instead of making a sql cell using %sql.&lt;/P&gt;&lt;P&gt;This will result  in a dataframe. If you want you can create a view on top of this using createOrReplaceTempView()&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Below is an example to use a variable:-&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;# A variable&lt;/P&gt;&lt;P&gt;var = "Hello World"&lt;/P&gt;&lt;P&gt;# Using f in pyspark spark.sql.&lt;/P&gt;&lt;P&gt;spark.sql(f""" SELECT '{var}' AS Message """) &lt;/P&gt;&lt;P&gt;# Using format&lt;/P&gt;&lt;P&gt;spark.sql(""" SELECT '{}' AS Message """.format(var))&lt;/P&gt;</description>
      <pubDate>Thu, 14 Apr 2022 08:06:28 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/how-can-i-access-python-variable-in-spark-sql/m-p/16520#M10698</guid>
      <dc:creator>Nirupam</dc:creator>
      <dc:date>2022-04-14T08:06:28Z</dc:date>
    </item>
  </channel>
</rss>

