In Databricks, there is no built-in functionality that directly returns a list of Hive metastore tables created by a specific user, unless your workflow includes adding creator information as a table property when tables are created.
However, there are a few methods you could explore to potentially achieve this, assuming you have the required permissions and that appropriate metadata has been recorded either within the table properties or via audit logs.
Method 1: Using Table Properties
If table properties include metadata identifying the table creator, you can use Spark SQL to filter tables based on these properties. Here's a conceptual example of how you might list tables by creator:
from pyspark.sql import SparkSession
spark = SparkSession.builder.getOrCreate()
catalog = spark.catalog
# List all tables in a given database
db_tables = catalog.listTables("your_database_name")
# This is a conceptual loop that assumes you have a property 'creator' associated with the tables
creator_name = "specific_creator"
for table in db_tables:
# Get extended table properties
table_info = spark.sql(f"DESCRIBE EXTENDED {table.database}.{table.name}").collect()
# Example of how you might find the 'creator' property in the table info
for row in table_info:
if "creator" in row.asDict()["col_name"] and creator_name in row.asDict()["data_type"]:
# Print or store the table information
print(f"Table Name: {table.name} | Creator: {creator_name}")
Remember that the above code is an example and would require a 'creator' property to already be present.
Method 2: Using Databricks Audit Logs
Another method is to utilize the Databricks audit logs if they are enabled for your workspace. You'll need to download and parse the audit logs, looking for CreateTable or CreateHiveTable events, then filter those logs by the creator's name.
import json
# This sample assumes you have an audit log with JSON entries for each activity, which may look like this:
# {"action": "CreateTable", "userName": "specific_creator", "requestParams": {"database": "your_database_name", "tableName": "table_name"}, ...}
logs = [...] # Replace this with the actual logic to load audit logs
creator_name = "specific_creator"
for log in logs:
log_entry = json.loads(log)
if log_entry["action"] in ["CreateTable", "CreateHiveTable"] and log_entry["userName"] == creator_name:
print(f"Table Name: {log_entry['requestParams']['tableName']} | Creator: {creator_name}")
Method 3: Using Database Comments or Naming Conventions
If you've adopted a pattern of either adding creator information within the table comments or using naming conventions that include the creator's username, you can use these patterns to identify the tables created by a particular user.
The SQL statement might look something like the following, querying the INFORMATION_SCHEMA or SHOW TABLES and filtering by COMMENT or name pattern:
SHOW TABLES IN your_database_name
You would then apply your logic to filter these results by the comments or naming pattern associated with a particular creator.
These methods require either proactive metadata management practices or access to audit logs, which may not always be available. Without such information, it is not possible to retrieve creator details for Hive metastore tables directly from the metastore in Databricks. Hence, it is good practice to record the creator's information as part of the table metadata (e.g., properties, comments) when a table is initially created for future reference.