3
votes

I am using Hive 2.1.1 and trying to create a connection. Following is the code I am trying:

private static String driverName = "org.apache.hive.jdbc.HiveDriver";

public static void main(String[] args) throws SQLException, ClassNotFoundException {
    // Register driver and create driver instance

    Class.forName(driverName);
    // get connection

    Connection con = DriverManager.getConnection("jdbc:hive2://", "", "");
    Statement stmt = con.createStatement();

    stmt.executeQuery("CREATE DATABASE userdb");
    System.out.println("Database userdb created successfully.");

    con.close();
}

These are my classpaths declared in bashrc:

CLASSPATH=$CLASSPATH:/usr/lib/hive/lib/hive-jdbc-2.1.1.jar CLASSPATH=$CLASSPATH:/usr/local/hadoop/lib/*:. CLASSPATH=$CLASSPATH:/usr/local/apache-hive-2.1.1-bin/lib

I am getting the error below.

ERROR StatusLogger No log4j2 configuration file found. Using default configuration: logging only errors to the console. Exception in thread "main" java.lang.NoClassDefFoundError: org/apache/hadoop/hive/ql/metadata/HiveException at org.apache.hive.service.cli.thrift.EmbeddedThriftBinaryCLIService.(EmbeddedThriftBinaryCLIService.java:33) at org.apache.hive.jdbc.HiveConnection.(HiveConnection.java:163) at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:107) at java.sql.DriverManager.getConnection(DriverManager.java:664) at java.sql.DriverManager.getConnection(DriverManager.java:247) at com.raad.ams.bean.HiveJdbc.main(HiveJdbc.java:17) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at com.intellij.rt.execution.application.AppMain.main(AppMain.java:147) Caused by: java.lang.ClassNotFoundException: org.apache.hadoop.hive.ql.metadata.HiveException at java.net.URLClassLoader.findClass(URLClassLoader.java:381) at java.lang.ClassLoader.loadClass(ClassLoader.java:424) at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331) at java.lang.ClassLoader.loadClass(ClassLoader.java:357)

Any help would be appreciated.

2
Use "hive-jdbc-standalone-2.1.1.jar" to get rid of the dependency headaches...Samson Scharfrichter
Caveat: even with the so-called "standalone" JAR you will also need hadoop-common.jar and commons-configuration.jar in CLASSPATH, and possibly hadoop-auth.jar (required for Kerberos)Samson Scharfrichter

2 Answers

1
votes

Here is what i would suggest : Create a Maven/ Gradle project and add Hive-jdbc in pom. It will download all dependencies.

Otherwise check this : Dependencies

You need to include all jars which are under dependencies tag.

0
votes

I encountered a similar issue where we were getting noclassdeffound error working on Robot automation framework and Hive. It looks to be jar compatibility issue. We resolved it by adding the latest hive jdbc maven dependency to the pom file and it worked. You should check for all the required Hive related jars and update them accordingly.