Java lite-log hive

Connect to Hive via HiveServer2 JDBC Driver

9 views 0 comments about 4 days ago Raymond Tang

This post shows you how to connect to HiveServer2 via Hive JDBC driver in Java.

*The way to connect to HiveServer1 is very similar though the driver names are different:

Version Driver Name
HiveServer1 org.apache.hadoop.hive.jdbc.HiveDriver
HiveServer2 org.apache.hive.jdbc.HiveDriver

The JDBC connection URL will also be different:

Version Driver Name
HiveServer1 jdbc:hive://…
HiveServer2 jdbc:hive2://…

Add dependency

First we need to add dependency in your project pom file (if you are using Maven):

<project xmlns=""


Or you need to directly reference Hive JAR file.

Create a Java class to connect to HiveServer2

Now, we can connect to HiveServer2 as you can do with any other supported databases.

The following is a sample class for your reference:

package examples;

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

public class HiveJdbcExample {

      * HiveServer2 JDBC driver name
     private static String driverName = "org.apache.hive.jdbc.HiveDriver";

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

        Connection conn = DriverManager.getConnection("jdbc:hive2://localhost:10000", "hive", "");
         Statement stmt = conn.createStatement();
         // show tables
         String sql = "SHOW databases";
         System.out.println("Running: " + sql);
         ResultSet rs = stmt.executeQuery(sql);
         while ( {

        String sql2 = "select * from test_db.test_table";
         System.out.println("Running: " + sql2);
         ResultSet rs2 = stmt.executeQuery(sql2);

        ResultSetMetaData rsmd = rs2.getMetaData();
         int columnsNumber = rsmd.getColumnCount();
         while ( {
             for (int i = 1; i <= columnsNumber; i++) {
                 if (i > 1)
                     System.out.print(",  ");
                 String columnValue = rs2.getString(i);
                 System.out.print(rsmd.getColumnName(i) + " " + columnValue);




For detailed information about connection URLs (as HiveServer2 service can run in different modes), please refer to the following page published by Apache:

Sample output


In HiveServer2 Web UI, we can also find the query history:


Clicking Drilldown link, you can also view the query detailed information:


Add comment

Comments (0)
No comments yet.