Multi-Table Operations Made Simple
In the ever-evolving landscape of data science and engineering, the ability to efficiently manage and manipulate data across multiple tables and databases is paramount. DiscoverX is a Python package that simplifies maintenance and optimization tasks like vacuuming and z-order by applying them concurrently across multiple tables with a single command. In this blog post, we'll walk through the process of installing DiscoverX, initializing it, and executing a multi-table count operation.
Getting Started with DiscoverX
To begin using DiscoverX, you first need to install the package within your Databricks environment. This can be done using the `%pip install` command as shown below:
%pip install dbl-discoverx
![archana986db_1-1719603072251.jpeg archana986db_1-1719603072251.jpeg](/t5/image/serverpage/image-id/9025i5F9ADCAA9ED1E3F6/image-size/large?v=v2&px=999)
After the installation, it's recommended to restart the Python kernel to ensure that the newly installed package is properly loaded. This can be achieved with the following command:
dbutils.library.restartPython()
Running Counts Across Tables
One of the common tasks in data analysis is to count the number of records in tables. DiscoverX simplifies this process by allowing you to define a set of tables and then apply a SQL template to each table. First, you need to import DiscoverX and initialize it:
from discoverx import DX
dx = DX()
![archana986db_2-1719603197199.jpeg archana986db_2-1719603197199.jpeg](/t5/image/serverpage/image-id/9026i0816EFEE69C578D7/image-size/large?v=v2&px=999)
Next, define the set of tables you want to run the operation on. In this example, we're using a wildcard to select all tables within a specific schema:
from_tables = "archana_krish_fe_dsa.*.*"
This is my test catalog: "archana_krish_fe_dsa" and the code considers all schemas and tables under it.
Then, you can specify the SQL operation you want to perform. In this case, we're counting the number of records in each table:
dx.from_tables(from_tables).with_sql("""SELECT COUNT(*) FROM {full_table_name}""").explain()
The explain() method provides a preview of the SQL queries that will be executed without actually running them. This is useful for verification purposes to ensure that the correct operations will be applied.
![archana986db_4-1719603788021.jpeg archana986db_4-1719603788021.jpeg](/t5/image/serverpage/image-id/9028i232953D6266BEEF7/image-size/large?v=v2&px=999)
Finally, to execute the count operation across the selected tables, use the apply() method:
table_counts=dx.from_tables(from_tables).with_sql("""SELECT COUNT(*) FROM {full_table_name}""").apply()
![archana986db_5-1719604756301.jpeg archana986db_5-1719604756301.jpeg](/t5/image/serverpage/image-id/9029iA4A526D8D5EE906F/image-size/large?v=v2&px=999)
The results can then be displayed using the display() method, which will show the count of records for each table:
table_counts.display()
![archana986db_6-1719605020400.jpeg archana986db_6-1719605020400.jpeg](/t5/image/serverpage/image-id/9030iCFD27BCDF2B0F1D4/image-size/large?v=v2&px=999)
You may further diagnose via a data profile or visualization.
![archana986db_7-1719605033012.jpeg archana986db_7-1719605033012.jpeg](/t5/image/serverpage/image-id/9031iC78D5B45E1BB66A9/image-size/large?v=v2&px=999)
Conclusion
In summary, DiscoverX offers a powerful and efficient way to perform multi-table operations within a Databricks Data Intelligence Platform. By simplifying tasks such as running counts across tables, it enables data professionals to focus more on analysis and less on the repetitive aspects of data management.This straightforward solution has been frequently requested by multiple customers and proven beneficial for them for simple discovery and auditing. Whether you're dealing with governance, compliance, or simply trying to get a quick overview of your datasets, DiscoverX is a tool worth exploring.
Citations:
[1] https://github.com/databrickslabs
[2] https://github.com/databrickslabs/discoverx
[3] https://www.databricks.com/blog/2022/04/29/announcing-general-availability-of-databricks-feature-sto...
[4] https://www.youtube.com/watch?v=yWT13TUM8lk
[5] https://pypi.org/project/dbl-discoverx/
[6] https://www.databricks.com/learn/labs