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: 

Apache Spark SQL query to get organization hierarchy

singhanuj2803
New Contributor III

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.

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.

If anyone out there has experience or insights into solving this puzzle, I'd greatly appreciate your guidance.

Consider the following organization hierarchy chart

rr.png

If I pass David's EmployeeId to the query, then it should display the organization hierarchy as shown below

RR1.png

Perhaps there's a creative workaround or a different approach that I haven't considered yet?

Feel free to drop a comment. Thank you in advance for your support! 

1 REPLY 1

Alberto_Umana
Databricks Employee
Databricks Employee

Hi @singhanuj2803,

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.

 

  1. Using DataFrame API with Loops: 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.
  2. Using Temporary Tables: Another approach is to use temporary tables to store intermediate results and repeatedly update these tables until the desired result is achieved.
  3. User-Defined Functions (UDFs): 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.
  4. Workaround Example: Here is a simplified example of how you might implement a recursive query using a loop in PySpark:

 

from pyspark.sql import SparkSession

from pyspark.sql.functions import col, expr

 

spark = SparkSession.builder.appName("RecursiveCTE").getOrCreate()

 

# Initial DataFrame

df = spark.createDataFrame([(1,)], ["id"])

 

# Recursive logic

max_iterations = 10

for _ in range(max_iterations):

     df = df.union(df.select(expr("id + 1"))).distinct()

 

df.show()

 

Please refer to: https://sqlandhadoop.com/how-to-implement-recursive-queries-in-spark/

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