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: 

How to get all the tables name with a specific column or columns in a database?

ramankr48
Contributor II

let's say there is a database db in which 700 tables are there, and we need to find all the tables name in which column "project_id" is present.

just an example for ubderstanding the questions.

1 ACCEPTED SOLUTION

Accepted Solutions

Anonymous
Not applicable
databaseName = "db"
desiredColumn = "project_id"
database = spark.sql(f"show tables in {databaseName} ").collect()
tablenames = []
for row in database:
  cols = spark.table(row.tableName).columns
  if desiredColumn in cols:
    tablenames.append(row.tableName)
 

Something close to that should work.

View solution in original post

5 REPLIES 5

Anonymous
Not applicable
databaseName = "db"
desiredColumn = "project_id"
database = spark.sql(f"show tables in {databaseName} ").collect()
tablenames = []
for row in database:
  cols = spark.table(row.tableName).columns
  if desiredColumn in cols:
    tablenames.append(row.tableName)
 

Something close to that should work.

Thanks josephk it worked

Hubert-Dudek
Esteemed Contributor III

Other possible solutions:

  • use new databricks search,
  • for those who migrated, use lineage in the unity catalog,
  • use lineage with Pureview (there is integration with hive metastore)

Being-UK
New Contributor II

Thanks @Joseph Kambourakis​ 

but the code seems to rendering errors at my end:

com.immuta.spark.exceptions.NoSuchDataSourceException: A data source with the table name '`v_table_name`' does not exist, is not in the current project, or is not accessible by the current user.

I've changed the original table name to 'v_table_name', as it happenes the table name is the first table on the schema, so it seems the code is searching through the schema but fails on the first table for some reason. Thanks

Anonymous
Not applicable

Sounds like an immuta problem, ask them. Def not a spark error.

Join 100K+ Data Experts: Register Now & Grow with Us!

Excited to expand your horizons with us? Click here to Register and begin your journey to success!

Already a member? Login and join your local regional user group! If there isn’t one near you, fill out this form and we’ll create one for you to join!