<?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 Apache Spark SQL query to get organization hierarchy in Data Engineering</title>
    <link>https://community.databricks.com/t5/data-engineering/apache-spark-sql-query-to-get-organization-hierarchy/m-p/103461#M41444</link>
    <description>&lt;P&gt;I'm currently diving deep into Spark SQL and its capabilities, and I'm facing an interesting challenge. I'm eager to learn how to write CTE recursive queries in Spark SQL, but after thorough research, it seems that Spark doesn't natively support recursive queries.&lt;/P&gt;&lt;P&gt;I've been exploring various methods and even attempted some implementations, but I've hit a roadblock – my CTE recursive queries aren't yielding the expected results.&lt;/P&gt;&lt;P&gt;If anyone out there has experience or insights into solving this puzzle, I'd greatly appreciate your guidance.&lt;/P&gt;&lt;P&gt;Consider the following&amp;nbsp;&lt;STRONG&gt;organization hierarchy chart&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rr.png" style="width: 550px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/13758iE4F17AA9102735EA/image-size/large?v=v2&amp;amp;px=999" role="button" title="rr.png" alt="rr.png" /&gt;&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;If I pass David's EmployeeId to the query, then it should display the organization hierarchy as shown below&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RR1.png" style="width: 252px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/13759iAA71F070C57FAC7C/image-size/large?v=v2&amp;amp;px=999" role="button" title="RR1.png" alt="RR1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Perhaps there's a creative workaround or a different approach that I haven't considered yet?&lt;/P&gt;&lt;P&gt;Feel free to drop a comment. Thank you in advance for your support!&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
    <pubDate>Sun, 29 Dec 2024 16:46:57 GMT</pubDate>
    <dc:creator>singhanuj2803</dc:creator>
    <dc:date>2024-12-29T16:46:57Z</dc:date>
    <item>
      <title>Apache Spark SQL query to get organization hierarchy</title>
      <link>https://community.databricks.com/t5/data-engineering/apache-spark-sql-query-to-get-organization-hierarchy/m-p/103461#M41444</link>
      <description>&lt;P&gt;I'm currently diving deep into Spark SQL and its capabilities, and I'm facing an interesting challenge. I'm eager to learn how to write CTE recursive queries in Spark SQL, but after thorough research, it seems that Spark doesn't natively support recursive queries.&lt;/P&gt;&lt;P&gt;I've been exploring various methods and even attempted some implementations, but I've hit a roadblock – my CTE recursive queries aren't yielding the expected results.&lt;/P&gt;&lt;P&gt;If anyone out there has experience or insights into solving this puzzle, I'd greatly appreciate your guidance.&lt;/P&gt;&lt;P&gt;Consider the following&amp;nbsp;&lt;STRONG&gt;organization hierarchy chart&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;&lt;STRONG&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="rr.png" style="width: 550px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/13758iE4F17AA9102735EA/image-size/large?v=v2&amp;amp;px=999" role="button" title="rr.png" alt="rr.png" /&gt;&lt;/span&gt;&lt;/STRONG&gt;&lt;/P&gt;&lt;P&gt;If I pass David's EmployeeId to the query, then it should display the organization hierarchy as shown below&lt;/P&gt;&lt;P&gt;&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="RR1.png" style="width: 252px;"&gt;&lt;img src="https://community.databricks.com/t5/image/serverpage/image-id/13759iAA71F070C57FAC7C/image-size/large?v=v2&amp;amp;px=999" role="button" title="RR1.png" alt="RR1.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Perhaps there's a creative workaround or a different approach that I haven't considered yet?&lt;/P&gt;&lt;P&gt;Feel free to drop a comment. Thank you in advance for your support!&lt;SPAN&gt;&amp;nbsp;&lt;/SPAN&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2024 16:46:57 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apache-spark-sql-query-to-get-organization-hierarchy/m-p/103461#M41444</guid>
      <dc:creator>singhanuj2803</dc:creator>
      <dc:date>2024-12-29T16:46:57Z</dc:date>
    </item>
    <item>
      <title>Re: Apache Spark SQL query to get organization hierarchy</title>
      <link>https://community.databricks.com/t5/data-engineering/apache-spark-sql-query-to-get-organization-hierarchy/m-p/103477#M41450</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;a href="https://community.databricks.com/t5/user/viewprofilepage/user-id/131859"&gt;@singhanuj2803&lt;/a&gt;,&lt;/P&gt;
&lt;P class="p1"&gt;It is correct that Spark SQL does not natively support recursive Common Table Expressions (CTEs). However, there are some workarounds and alternative methods you can use to achieve similar results.&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;OL class="ol1"&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Using DataFrame API with Loops&lt;/STRONG&gt;: You can use the DataFrame API in combination with loops in Scala or Python to simulate recursive queries. This involves iteratively applying transformations until a condition is met.&lt;/LI&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Using Temporary Tables&lt;/STRONG&gt;: Another approach is to use temporary tables to store intermediate results and repeatedly update these tables until the desired result is achieved.&lt;/LI&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;User-Defined Functions (UDFs)&lt;/STRONG&gt;: You can implement the recursive logic within a UDF in Scala or Python. This allows you to encapsulate the recursive logic and apply it to your DataFrame.&lt;/LI&gt;
&lt;LI class="li1"&gt;&lt;STRONG&gt;Workaround Example&lt;/STRONG&gt;: Here is a simplified example of how you might implement a recursive query using a loop in PySpark:&lt;/LI&gt;
&lt;/OL&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;from pyspark.sql import SparkSession&lt;/P&gt;
&lt;P class="p1"&gt;from pyspark.sql.functions import col, expr&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;spark = SparkSession.builder.appName("RecursiveCTE").getOrCreate()&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;# Initial DataFrame&lt;/P&gt;
&lt;P class="p1"&gt;df = spark.createDataFrame([(1,)], ["id"])&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;# Recursive logic&lt;/P&gt;
&lt;P class="p1"&gt;max_iterations = 10&lt;/P&gt;
&lt;P class="p1"&gt;for _ in range(max_iterations):&lt;/P&gt;
&lt;P class="p1"&gt;&lt;SPAN class="Apple-converted-space"&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp; &lt;/SPAN&gt;df = df.union(df.select(expr("id + 1"))).distinct()&lt;/P&gt;
&lt;P class="p2"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;df.show()&lt;/P&gt;
&lt;P class="p1"&gt;&amp;nbsp;&lt;/P&gt;
&lt;P class="p1"&gt;Please refer to:&amp;nbsp;&lt;A href="https://sqlandhadoop.com/how-to-implement-recursive-queries-in-spark/" target="_blank"&gt;https://sqlandhadoop.com/how-to-implement-recursive-queries-in-spark/&lt;/A&gt;&lt;/P&gt;</description>
      <pubDate>Sun, 29 Dec 2024 22:45:13 GMT</pubDate>
      <guid>https://community.databricks.com/t5/data-engineering/apache-spark-sql-query-to-get-organization-hierarchy/m-p/103477#M41450</guid>
      <dc:creator>Alberto_Umana</dc:creator>
      <dc:date>2024-12-29T22:45:13Z</dc:date>
    </item>
  </channel>
</rss>

