user4398985
user4398985

Reputation:

ClassNotFoundException while connecting to Hive Remotely using Jdbc on Hortonworks

I am facing this exception while connecting to beeline, hive2 version 1.2.1000.2.5.0.0, I have added hive-jdbc.jar file into classpath on my Windows 10 machine.

Exception:

java.lang.ClassNotFoundException: org.apache.hadoop.hive.jdbc.HiveDriver
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at java.lang.Class.forName0(Native Method)
        at java.lang.Class.forName(Unknown Source)
        at HiveJdbcClient.main(HiveJdbcClient.java:17)

HiveJdbcClient.java

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

public class HiveJdbcClient {

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

  public static void main(String[] args) throws SQLException {
      try {
      Class.forName(driverName);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }
    //replace "hive" here with the name of the user the queries should run as
    Connection con = DriverManager.getConnection("jdbc:hive://localhost:10003/default", "", "");
    Statement stmt = con.createStatement();
    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 + "'";
    System.out.println("Running: " + sql);
    ResultSet res = stmt.executeQuery(sql);
    if (res.next()) {
      System.out.println(res.getString(1));
    }
  }
}
  1. I am not sure what other jars I need to add here?
  2. These jar files change based on the version of Hive?
  3. Does Cloudera and Hortonworks etc have different jar files, please visit this page, this is so confusing.
  4. Why it's so complicated to connect to Hive, I have my hive on Hortonworks, which is http://142.56.78.174:10003/default, and I have added hive-jdbc jar files to the classpath and I have my java class below. Isn't this enough?
  5. Please let me know what I am really missing.

Thanks for the answer, I have already tried this,

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

Thanks in advance!!

Upvotes: 1

Views: 4098

Answers (2)

user4398985
user4398985

Reputation:

I tried my best on Windows couldn't run it successfully. So, I switched to centos, this should also help Windows users, do comment for any questions :)

It’s hard to find all the jars to 1.2.1000.2.5.0.0, so I tried 1.2.1 version and it worked on our nn2 server. I have wasted lot of time on Windows, Linux has a very good documentation.

HiveJdbcClient.java:

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

public class HiveJdbcClient {

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

  /**
   * @param args
   * @throws SQLException
   */
  public static void main(String[] args) throws SQLException {
      try {
      Class.forName(driverName);
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
      System.exit(1);
    }
    //replace "hive" here with the name of the user the queries should run as
    Connection con = DriverManager.getConnection("jdbc:hive2://localhost:10000/default", "", "");
    Statement stmt = con.createStatement();
    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 + "'";
    System.out.println("Running: " + sql);
    ResultSet res = stmt.executeQuery(sql);
    if (res.next()) {
      System.out.println(res.getString(1));
    }
  }
}

Above is the simple program which I tried on our nn2, before you run any of those files, please add all these jars into the classpath of any machine which you are working on.

I hope I am not missing some jar files, this should work for you with no errors:

  1. http://mvnrepository.com/artifact/org.apache.hive/hive-serde/1.2.1
  2. http://mvnrepository.com/artifact/org.apache.hive/hive-common/1.2.1
  3. http://mvnrepository.com/artifact/org.apache.hive/hive-shims/1.2.1
  4. http://mvnrepository.com/artifact/org.apache.hive/hive-beeline/1.2.1
  5. http://mvnrepository.com/artifact/org.apache.hive/hive-exec/1.2.1
  6. http://mvnrepository.com/artifact/org.apache.hive/hive-metastore/1.2.1
  7. http://mvnrepository.com/artifact/org.apache.hive/hive-service/1.2.1
  8. https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.2.5
  9. https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore/4.2.1
  10. https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore/4.3-alpha1
  11. https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.3.4
  12. https://mvnrepository.com/artifact/commons-logging/commons-logging/1.2
  13. https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common/2.2.0
  14. https://mvnrepository.com/artifact/org.slf4j/slf4j-api/1.7.21
  15. http://mvnrepository.com/artifact/org.apache.hive/hive-jdbc/1.2.1

If you just want to test the connection without adding into the classpath you can do it using this command on Linux:

Compile:

[root@centosserver ~]# javac -cp .:hive-jdbc-1.2.1.jar:hive-service-1.2.1.jar:hive-exec-1.2.1.jar:hive-metastore-1.2.1.jar:hive-shims-1.2.1.jar:hive-beeline-1.2.1:hive-serde-1.2.1:hive-common-1.2.1:httpclient-4.2.5.jar:httpcore-4.2.1.jar:httpcore-4.3-alpha1.jar:httpclient-4.0-alpha4.jar:httpclient-4.3.4.jar:commons-logging-1.2.jar:hadoop-common-2.2.0.jar:slf4j-api-1.7.21.jar HiveJdbcClient.java

Execute:

[root@centosserver ~]# java -cp .:hive-jdbc-1.2.1.jar:hive-service-1.2.1.jar:hive-exec-1.2.1.jar:hive-metastore-1.2.1.jar:hive-shims-1.2.1.jar:hive-beeline-1.2.1:hive-serde-1.2.1:hive-common-1.2.1:httpclient-4.2.5.jar:httpcore-4.2.1.jar:httpcore-4.3-alpha1.jar:httpclient-4.0-alpha4.jar:httpclient-4.3.4.jar:commons-logging-1.2.jar:hadoop-common-2.2.0.jar:slf4j-api-1.7.21.jar HiveJdbcClient

Basic command is

java -cp .:your-Jar-File1:your-Jar-File2:your-Jar-File3 yourMainClass.Java

Upvotes: 0

nail fei
nail fei

Reputation: 2319

if you use the most recent jar (e.g version 2.1x) you should try this

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

instead of

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

for in recent JDBC jar there is no org.apache.hadoop.hive.jdbc.HiveDriver

Upvotes: 1

Related Questions