Forum Discussion
Spark Job Definition vs Notebooks
- Anonymous2 years ago
Hi DennesTorres ,
Yes, you are correct we need to use "spark.catalog", it will list out all the lakehouses present inside the workspace, even if not linked to the notebook.
Code:lakehouses = spark.catalog.listDatabases() lakehouse_list = [] for lakehouse in lakehouses: lakehouse_list.append(lakehouse.name) print(lakehouse_list)
In order get list of tables present inside particular lakehouse, you can refer below -
Code:# Get the list of lakehouses to read tables from. lakehouses = ["gopi_lake_house", "gopi_lakehouse_2"] # Loop through the lakehouses and read all tables from each lakehouse. for lakehouse in lakehouses: tables = spark.sql(f"SHOW TABLES IN {lakehouse}") tables.show()
Note: SHOW TABLES IN - will be working even if the lakehouse is not default. In my case only gopi_lakehouse_2 is selected as default, but I am able to see tables present inside gopi_lake_house and gopi_lakehouse_2.
For Example:
Executed in Fabric Notebooks:
Executed in Spark Job Application:
The above code is working fine both in notebook and spark job application.
Hope this was helpful. - 2 years ago
Hi,
Using the information provided until this point, I was able to write a code to make the maintenance of all lakehouses in the same workspace.
The Spark Job Definition, on the other way, can be linked to multiple workspaces. One of the workspace is turned into the default workspace while the other workspaces become a configuration.
We can loop through the configurations and use mssparkutils to make the mount of the lakehouse addresses as local folders.
Once mounted, we loop through the mounts discovering the tables of each lakehouse and executing the maintenance.
It worked like a charm, I will write an article about it.
Thank you for all the help!
Kind Regards,
Dennes
Hi,
The array becomes fixed in this example, I'm trying to achieve something more dynamic. But this may be an option.
Kind Regards,
Dennes
Hi DennesTorres ,
Can you please check this code
lakehouses = catalog.listDatabases()
lakehouse_list = []
for lakehouse in lakehouses:
lakehouse_list.append(lakehouse.name)
print(lakehouse_list)
- Anonymous2 years agoNot applicable
Hello DennesTorres ,
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet . Otherwise, will respond back with the more details and we will try to help . - Anonymous2 years agoNot applicable
Hi DennesTorres,
We haven’t heard from you on the last response and was just checking back to see if you have a resolution yet .
In case if you have any resolution please do share that same with the community as it can be helpful to others .
If you have any question relating to the current thread, please do let us know and we will try out best to help you.
In case if you have any other question on a different issue, we request you to open a new thread .