Hive – Create Database from Scala Example

In this article, I will explain how to connect to Hive and create a Hive Database from Scala with an example, In order to connect and run Hive SQL you need to have hive-jdbc dependency, you can download this from Maven or use the below dependency on your pom.xml

Hive Scala Dependency


Start HiveServer2

To connect to Hive from Scala, you need to start hiveserver2 from $HIVE_HOME/bin

prabha@namenode:~/hive/bin$ ./hiveserver2
2020-10-03 23:17:08: Starting HiveServer2

Below is complete Scala example of how to create a Hive Database.

Create a Hive Table from Scala Example

package com.sparkbyexamples.hive;

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

object HiveCreateDatabase extends App{
	var con = null;
	try {
		val conStr = "jdbc:hive2://";
		con = DriverManager.getConnection(conStr, "", "");
		val stmt = con.createStatement();
		stmt.executeQuery("CREATE DATABASE emp");
		System.out.println("Database emp created successfully.");
	} catch (Exception ex) {
	} finally {
		try {
			if (con != null)
		} catch (Exception ex) {

Note: If you are using an older version of Hive, you should use the driver org.apache.hadoop.hive.jdbc.HiveDriver and your connection string should start with jdbc:hive://

Happy Learning !!

Naveen (NNK)

I am Naveen (NNK) working as a Principal Engineer. I am a seasoned Apache Spark Engineer with a passion for harnessing the power of big data and distributed computing to drive innovation and deliver data-driven insights. I love to design, optimize, and managing Apache Spark-based solutions that transform raw data into actionable intelligence. I am also passion about sharing my knowledge in Apache Spark, Hive, PySpark, R etc.

Leave a Reply

You are currently viewing Hive – Create Database from Scala Example