dnks23
dnks23

Reputation: 369

How to delete a hive database with spark-sql?

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?

Upvotes: 5

Views: 7813

Answers (2)

Giorgos Myrianthous
Giorgos Myrianthous

Reputation: 39840

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}')

Upvotes: 2

notNull
notNull

Reputation: 31490

Try with below

DROP DATABASE IF EXISTS <database_name> CASCADE;
  • CASCADE will drop all the tables in the database.
  • Default, the mode is 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)

Upvotes: 8

Related Questions