Simple Hive JDBC Client

Here I am explaining a sample hive jdbc client. With this we can fire hive queries from java programs. The only thing is that we need to start the hive server. By default, hive server listens at port 10000. The sample program is given below. The program is self explanatory and you can rewrite it to execute any type of hive queries. For this program you need the mysql-connector jar in the classpath.

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

   * @author
   * Amal G Jose

public class HiveJdbc
  private static String driver = "org.apache.hadoop.hive.jdbc.HiveDriver";

   * @param args
   * @throws SQLException
  public static void main(String[] args) throws SQLException {
      try {
    } catch (ClassNotFoundException e) {

    Connection connect = DriverManager.getConnection("jdbc:hive://:10000/default", "", "");
    Statement state = connect.createStatement();
    String tableName = "test";
    state.executeQuery("drop table " + tableName);
    ResultSet res = state.executeQuery("create table " + tableName + " (key int, value string)");
   // Query to show tables
    String show = "show tables";
    System.out.println("Running: " + show);
    res = state.executeQuery(show);
    if ( {

    // Query to describe table
    String describe = "describe " + tableName;
    System.out.println("Running: " + describe);
    res = state.executeQuery(describe);
    while ( {
      System.out.println(res.getString(1) + "\t" + res.getString(2));


About amalgjose
I am an Electrical Engineer by qualification, now I am working as a Software Architect. I am very much interested in Electrical, Electronics, Mechanical and now in Software fields. I like exploring things in these fields. I love travelling, long drives and music.

11 Responses to Simple Hive JDBC Client

  1. Priya says:


    M getting below error. could you please help me to resolve it-

    Exception in thread “main” java.lang.NoClassDefFoundError: org/apache/hadoop/hive/service/HiveInterface
    at org.apache.hadoop.hive.jdbc.HiveDriver.connect(
    at java.sql.DriverManager.getConnection(
    at java.sql.DriverManager.getConnection(
    at Hive_JDBC.main(
    Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hive.service.HiveInterface
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    … 4 more

    Thanks in advance

  2. prashant says:


    M getting below error. could you please help me to resolve it-
    java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver
    at Method)
    at java.lang.ClassLoader.loadClass(
    at sun.misc.Launcher$AppClassLoader.loadClass(
    at java.lang.ClassLoader.loadClass(
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(
    at Jdbc.main(

  3. srinu says:

    the error i am getting here is java.lang.ClassNotFoundException: org.apache.hive.jdbc.HiveDriver and i have added the hive service and also hive jdbc jars but the error is not getting resolved.please help me

    • amalgjose says:

      Please ensure that the jars are added to the classpath properly.

      • srinu says:

        When i am executing same code and jars with simple program it’s connecting properly…but when i am adding servlets to it and started debugging,it is showing me that error and after class.forname() it is directly throwing me this error

  4. vishal says:

    i am facing this error.

    ava.sql.SQLException: Invalid URL: jdbc:hive2://server:10000/default
    at org.apache.hadoop.hive.jdbc.HiveConnection.(
    at org.apache.hadoop.hive.jdbc.HiveDriver.connect(
    at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnection.createConnection(
    at org.eclipse.datatools.connectivity.DriverConnectionBase.internalCreateConnection(
    at org.eclipse.datatools.connectivity.drivers.jdbc.JDBCConnectionFactory.createConnection(
    at org.eclipse.datatools.connectivity.internal.ConnectionFactoryProvider.createConnection(
    at org.eclipse.datatools.connectivity.internal.ConnectionProfile.createConnection(
    at org.eclipse.datatools.connectivity.ui.PingJob.createTestConnection(

  5. shirisha says:

    Did anyone get this to work. i tried the same and code throws the same error. i ensured all the jar files are in the classpath

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: