Apache Hive Usage Example – Create and Use Database

Tags: ,

I this post, I describe how  to Create a Hive Database, Create Database using JDBC, Describe and Show hive Database. 

Create Database Statement

A database in Hive is a namespace or a collection of tables.  In order to create a database in Hive, we need to use the Create Database statement. The syntax for this statement is as follows:

CREATE DATABASE|SCHEMA [IF NOT EXISTS] <database name>

Here, IF NOT EXISTS is an optional clause, which notifies the user that a database with the same name already exists. We can use SCHEMA in place of DATABASE in this command. The following query is executed to create a database named userdb:

hive> CREATE DATABASE [IF NOT EXISTS] userdb;

or

hive> CREATE SCHEMA userdb;

The following query is used to verify a databases list:

hive> SHOW DATABASES;
default
userdb

Create Hive Database with Location

The database directory is created under a top-level directory specified by the property hive.metastore.warehouse.dir, You can override this default location for the new directory as shown in this example:

CREATE database test_db LOCATION '/tmp/user/hivedb/'

Check the Location of Hive Database

In case you want to check the status of a Database such as it’s location, you can use DESCRIBE DATABASE <database> command. 

hive> CREATE DATABASE students_db LOCATION '/tmp/user/hivedb/'
hive> DESCRIBE DATABASE students_db;
students_db hdfs://xxx-nn1.blue.ygrid.xxxxx.com:8020/tmp/user/hivedb user USER

So you will know the location of the Database, and its owner. 

Create Hive Database Using JDBC Program

The JDBC program to create 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 HiveCreateDb {
   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:9999/default", "", "");
      Statement stmt = con.createStatement();
      
      stmt.executeQuery("CREATE DATABASE userdb");
      System.out.println(“Database userdb created successfully.”);
      
      con.close();
   }
}

Save the program in a file named HiveCreateDb.java. The following commands are used to compile and execute this program.

$ javac HiveCreateDb.java
$ java HiveCreateDb

Show Database Statement

You can use SHOW DATABASES to list all the databases already created.

hive> SHOW DATABASES;
default
userdb

If you have a lot of databases, you can restrict the ones listed using a regular expression. The following example lists only those databases that start with the letter u and end with any other characters (the .* part):

hive> SHOW DATABASES LIKE 'u.*';
userdb
hive> ...

Select a Hive Database 

To use a Database, use the USE <database> Command

hive> USE userdb;