database 在 Hive 中删除包含表的数据库

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42129891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 08:03:54  来源:igfitidea点击:

Delete a database with tables in Hive

databasehivehiveql

提问by franklinsijo

I have a database in hive which has around 100 tables. I would like to delete the whole database in a single shot query.

我在 hive 中有一个数据库,它有大约 100 个表。我想在单次查询中删除整个数据库。

How can we achieve that in Hive?

我们如何在 Hive 中实现这一目标?

回答by franklinsijo

Use,

用,

DROP DATABASE database_name CASCADE;

By default, the mode is RESTRICTwhich blocks the deletion of database if it holds tables.

默认情况下,模式是RESTRICT如果数据库有表则阻止删除数据库。

回答by Sandeep Singh

You can use this command:

你可以使用这个命令:

DROP DATABASE IF EXISTS HIVE_DATABASE_NAME CASCADE;

In case, you are using older version of Hive: Drop all tables and views from hive database first, and then drop the database. You can consolidate below command in a shell script to execute.

如果您使用的是旧版本的 Hive:首先从 hive 数据库中删除所有表和视图,然后再删除数据库。您可以在 shell 脚本中合并以下命令以执行。

hive -e 'use HIVE_DATABASE_NAME;show tables' | xargs -I '{}' hive -e 'use HIVE_DATABASE_NAME;DROP TABLE IF EXISTS {}'

hive -e 'use HIVE_DATABASE_NAME;show tables' | xargs -I '{}' hive -e 'use HIVE_DATABASE_NAME;DROP VIEW IF EXISTS {}'

hive -e 'DROP DATABASE IF EXISTS HIVE_DATABASE_NAME;'

回答by Santosh Singh

General syntax is as follows:

一般语法如下:

hive> DROP DATABASE [IF EXISTS] database_name [RESTRICT|CASCADE];

The default behaviour is RESTRICT, where DROP DATABASE will fail if the database is not empty. To drop the tables in the database as well, use DROP DATABASE … with CASCADE option.

默认行为是 RESTRICT,如果数据库不为空,则 DROP DATABASE 将失败。要删除数据库中的表,请使用 DROP DATABASE ... 和 CASCADE 选项。

1. Drop database without table or Empty Database:

1.删​​除无表或空数据库的数据库:

hive> DROP DATABASE database_name;

2. Drop database with tables:

2.删除带有表的数据库:

hive> DROP DATABASE database_name CASCADE;

It dropping respective tables before dropping the database.

它在删除数据库之前删除相应的表。