You are currently viewing Hive – Create Database from Java Example

Let’s learn how to create a Hive Database in Java, To connect to Hive from Java you need hive-hdbc dependency, The hive-jdbc.jar is a Java Archive (JAR) file that contains the JDBC driver for Apache Hive. It provides the necessary classes and functionality for Java applications to connect and interact with Hive databases using JDBC (Java Database Connectivity).

Advertisements

When you’re working with Hive in a Java application and you need to establish a connection to HiveServer2 or HiveServer, you include the hive-jdbc.jar file in your project’s classpath. This allows your Java application to use the JDBC API to execute SQL queries, fetch data, and perform other database operations on Hive tables.

If you are using Maven to build your project, please have the following dependency in your pom.xml file.

Hive Java Dependency

Maven hive-jdbc dependency


<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>3.1.2</version>
</dependency>

Start HiveServer2

HiveServer2 is a server component of Apache Hive that provides a service for clients to execute queries against Hive. It acts as a central point for connecting to Hive and executing SQL-like queries on Hive tables and data stored in Hadoop Distributed File System (HDFS) or other supported storage systems.

You can start HiveServer2 by running the hiveserver2 command from the command line. This command starts HiveServer2 service on the default port (10000). You can also start HiveServer2 in the background using the nohup command to keep it running even after you log out of the session


# Start hiveserver2
user:~/hive/bin$ ./hiveserver2
2020-10-03 23:17:08: Starting HiveServer2

Create a Hive Database from Java Example

Following are the steps to create a Hive Database from Java

  1. Import JDBC Packages: Import the required JDBC packages for database connectivity.
  2. Load Hive Driver: Load the Hive JDBC driver class.
  3. Establish Connection: Create a connection to the Hive server.
  4. Create Statement: Create a statement object to execute SQL queries.
  5. Define SQL Query: Define the SQL query to create the database.
  6. Execute Query: Execute the SQL query to create the database.
  7. Close Resources: Close the statement and connection objects.

Below is an example that uses the above steps.


package com.sparkbyexamples.hive;

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

public class HiveCreateDatabase {
	public static void main(String[] args) {
		Connection con = null;
		try {
			String conStr = "jdbc:hive2://192.168.1.148:10000/default";
			Class.forName("org.apache.hive.jdbc.HiveDriver");
			con = DriverManager.getConnection(conStr, "", "");
			Statement stmt = con.createStatement();
			stmt.executeQuery("CREATE DATABASE emp");
			System.out.println("Database emp created successfully.");
		} catch (Exception ex) {
			ex.printStackTrace();
		} finally {
			try {
				if (con != null)
					con.close();
			} catch (Exception ex) {
			}
		}
	}
}

Please note: If you’re working with an older version of Hive, utilize the driver org.apache.hadoop.hive.jdbc.HiveDriver and ensure that your connection string begins with jdbc:hive://.

Happy Learning !!

Leave a Reply