Support Questions

Find answers, ask questions, and share your expertise

Hive JDBC connection

Hello,

I am creating a java wrapper for my thesis work with Hive-Hbase integration. So far, Hbase JAVA api worked well, while I tried to connect Hive JDBC , it issued "Service Unavailable Retry Strategy error.

Following are the dependencies in pom.xml:

<dependencies>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-client</artifactId>
        <version>1.1.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.hbase</groupId>
        <artifactId>hbase-common</artifactId>
        <version>1.1.2</version>
    </dependency>

    <!-- https://mvnrepository.com/artifact/org.apache.phoenix/phoenix-core -->
    <dependency>
        <groupId>org.apache.phoenix</groupId>
        <artifactId>phoenix-core</artifactId>
        <version>4.4.0-HBase-1.1</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-hbase-handler -->
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-hbase-handler -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-hbase-handler</artifactId>
        <version>1.2.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-exec -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-exec</artifactId>
        <version>1.2.2</version>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.hive/hive-jdbc -->
    <dependency>
        <groupId>org.apache.hive</groupId>
        <artifactId>hive-jdbc</artifactId>
        <version>1.2.2</version>
    </dependency>
    <dependency>
        <groupId>org.apache.thrift</groupId>
        <artifactId>libthrift</artifactId>
        <version>0.9.0</version>
        <type>pom</type>
    </dependency>
    <!-- https://mvnrepository.com/artifact/org.apache.thrift/libfb303 -->
    <dependency>
        <groupId>org.apache.thrift</groupId>
        <artifactId>libfb303</artifactId>
        <version>0.9.0</version>
        <type>pom</type>
    </dependency>
    <!-- https://mvnrepository.com/artifact/commons-httpclient/commons-httpclient -->
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
</dependencies>

Following is the part of the simple code where I am just testing the hive jdbc connection:

private static String driverName = "org.apache.hive.jdbc.HiveDriver";
try {
    Class.forName(driverName);
}
catch (ClassNotFoundException e) {
    e.printStackTrace();
    System.exit(1);
}
    Connection con1= DriverManager.getConnection("jdbc:hive2://localhost:10000/firehose", "hive"
           ,"hive");
    System.out.println("hello");
    Statement stmt = con1.createStatement();
    stmt.executeQuery("CREATE TABLE IF NOT EXISTS employee (eid int, name String);");
String tableName = "testHiveDriverTable";
stmt.execute("drop table if exists " + tableName);
stmt.execute("create table " + tableName + " (key int, value string)");
// show tables
// String sql = "show tables '" + tableName + "'";
String sql = ("show tables");
ResultSet res = stmt.executeQuery(sql);
if (res.next()) {
    System.out.println(res.getString(1));
}

While I execute my program, I get the following error:

Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/http/client/ServiceUnavailableRetryStrategy
    at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
    at java.sql.DriverManager.getConnection(DriverManager.java:664)
    at java.sql.DriverManager.getConnection(DriverManager.java:247)
    at hbasepingtest.MavenMainHbase.connectHive(MavenMainHbase.java:63)
    at hbasepingtest.MavenMainHbase.main(MavenMainHbase.java:48)
Caused by: java.lang.ClassNotFoundException: org.apache.http.client.ServiceUnavailableRetryStrategy
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:335)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    ... 5 more

To cross check, I beeline'd my jdbc url with the database, it logs in fine.

Am I missing some more dependency in my pom file?

2 REPLIES 2

It worked. I added the following dependency:

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient-osgi</artifactId>
    <version>4.3-beta2</version>
</dependency>

Super Mentor

@Jasim Waheed Ansari

As the error that you are getting is basically ClassNotFountException.

Causedby: java.lang.ClassNotFoundException: org.apache.http.client.ServiceUnavailableRetryStrategy
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
.

The httpclient-4.2.x and above contains that class "org.apache.http.client.ServiceUnavailableRetryStrategy".

So i guess you might want to include the following dependency in your pom.xml to make sure that it gets that dependency.

<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.2.1</version>
</dependency>

Reference: https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.2.1

.