Hive 简明教程

Hive - Drop Database

本章介绍如何在 Hive 中删除数据库。SCHEMA 和 DATABASE 的用法相同。

This chapter describes how to drop a database in Hive. The usage of SCHEMA and DATABASE are same.

Drop Database Statement

Drop Database 是一款可删除所有数据表并删除数据库的语句。其语法如下:

Drop Database is a statement that drops all the tables and deletes the database. Its syntax is as follows:

DROP DATABASE StatementDROP (DATABASE|SCHEMA) [IF EXISTS] database_name
[RESTRICT|CASCADE];

使用以下查询可删除数据库。我们假设数据库名称为 userdb

The following queries are used to drop a database. Let us assume that the database name is userdb.

hive> DROP DATABASE IF EXISTS userdb;

以下查询使用 CASCADE 删除数据库。这意味着在删除数据库之前先删除各自的数据表。

The following query drops the database using CASCADE. It means dropping respective tables before dropping the database.

hive> DROP DATABASE IF EXISTS userdb CASCADE;

以下查询使用 SCHEMA 删除数据库。

The following query drops the database using SCHEMA.

hive> DROP SCHEMA userdb;

此条款已添加到 Hive 0.6 中。

This clause was added in Hive 0.6.

JDBC Program

以下为删除数据库的 JDBC 程序。

The JDBC program to drop a database is given below.

import java.sql.SQLException;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.Statement;
import java.sql.DriverManager;

public class HiveDropDb {
   private static String driverName = "org.apache.hadoop.hive.jdbc.HiveDriver";

   public static void main(String[] args) throws SQLException {

      // Register driver and create driver instance
      Class.forName(driverName);

      // get connection
      Connection con = DriverManager.getConnection("jdbc:hive://localhost:10000/default", "", "");
      Statement stmt = con.createStatement();
      stmt.executeQuery("DROP DATABASE userdb");

      System.out.println(“Drop userdb database successful.”);

      con.close();
   }
}

在名为 HiveDropDb.java 的文件中保存程序。以下为编译和执行此程序的命令。

Save the program in a file named HiveDropDb.java. Given below are the commands to compile and execute this program.

$ javac HiveDropDb.java
$ java HiveDropDb

Output:

Drop userdb database successful.