cancel
Showing results for 
Search instead for 
Did you mean: 
Community Platform Discussions
Connect with fellow community members to discuss general topics related to the Databricks platform, industry trends, and best practices. Share experiences, ask questions, and foster collaboration within the community.
cancel
Showing results for 
Search instead for 
Did you mean: 

How to retrieve Spark Session inside java jar library installed on Cluster

ayush19
New Contributor III

I have a java app in form of jar package. This jar is installed on a Databricks cluster. This jar package reads and writes to few tables in databricks. In order to achieve that, I need SparkSession available in the code. Given that spark session is already running in Databricks, I need to somehow access it. 

I tried various methods like SparkSession.builder, SparkSession.active and SparkSession.getActiveSession but none are available in databricks. How can I access the session inside my code?

I tried to pass session directly via main function arguments and it's working but as a requirement of the main function, I also need to pass some other String arguments so I need a way to get session in code some other way

1 ACCEPTED SOLUTION

Accepted Solutions

Kaniz_Fatma
Community Manager
Community Manager

Hi @ayush19To retrieve the SparkSession inside your Java JAR library installed on a Databricks cluster, you can use the SparkSession.getOrCreate() method. This method will either return the existing SparkSession or create a new one if it doesn’t exist. 

In Databricks, the SparkSession is typically created automatically and assigned to the variable spark. 

Using SparkSession.builder().getOrCreate() ensures that you are accessing the existing session.

If you need to pass additional arguments to your main function, you can handle them separately while still retrieving the SparkSession as shown above.

Does this help with your issue? If you have any more questions or need further assistance, feel free to ask!

View solution in original post

3 REPLIES 3

Kaniz_Fatma
Community Manager
Community Manager

Hi @ayush19To retrieve the SparkSession inside your Java JAR library installed on a Databricks cluster, you can use the SparkSession.getOrCreate() method. This method will either return the existing SparkSession or create a new one if it doesn’t exist. 

In Databricks, the SparkSession is typically created automatically and assigned to the variable spark. 

Using SparkSession.builder().getOrCreate() ensures that you are accessing the existing session.

If you need to pass additional arguments to your main function, you can handle them separately while still retrieving the SparkSession as shown above.

Does this help with your issue? If you have any more questions or need further assistance, feel free to ask!

ayush19
New Contributor III

Thank you for reply. This solves the issue. I was passing ".appName" as well with builder which was causing the issue. I tried the solution you mentioned it worked. Thank you

Cheers! I'm glad it worked.

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