In my use-case I want to "create or replace" a hive database from spark-sql. I was wondering whether that is even possible? or do I have to manually delete all tables within the database in a for-loop first and then drop the empty database?
Try with below
DROP DATABASE IF EXISTS <database_name> CASCADE;
CASCADE
will drop all the tables in the database. RESTRICT
hive will not delete database if there are some tables exists in the db. Once you drop the database then create a new database
using spark-sql.
For more info refer to Create/Drop/Alter/UseDatabase page.
Next option would be the way you have suggested in the post ( delete all tables then drop the database
)
In order to delete the database and all the tables :
spark_session.sql(f'DROP DATABASE IF EXISTS {db_name} CASCADE')
In order to delete a specific table :
spark_session.sql(f'DROP TABLE IF EXISTS {db_name}.{table_name}')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.