Created
October 21, 2021 19:21
-
-
Save DaveRuijter/135b11542774894f7132d5d649b9d174 to your computer and use it in GitHub Desktop.
Script that iterates all tables in all databases that match the given database name filter, and performs the VACUUM command on each table.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
database_names_filter = "20_silver*" | |
try: | |
dbs = spark.sql(f"SHOW DATABASES LIKE '{database_names_filter}'").select("databaseName").collect() | |
dbs = [(row.databaseName) for row in dbs] | |
for database_name in dbs: | |
print(f"Found database: {database_name}, performing actions on all its tables..") | |
tables = spark.sql(f"SHOW TABLES FROM `{database_name}`").select("tableName").collect() | |
tables = [(row.tableName) for row in tables] | |
for table_name in tables: | |
#spark.sql(f"ALTER TABLE `{database_name}`.`{table_name}` SET TBLPROPERTIES ('delta.logRetentionDuration'='interval 30 days', 'delta.deletedFileRetentionDuration'='interval 7 days')") | |
spark.sql(f"VACUUM `{database_name}`.`{table_name}`") | |
spark.sql(f"ANALYZE TABLE `{database_name}`.`{table_name}` COMPUTE STATISTICS") | |
except Exception as e: | |
raise Exception(f"Exception:`{str(e)}`") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment