在Hortonworks上使用Jdbc远程连接到Hive时出现ClassNotFoundException

时间:2016-12-06 04:49:30

标签: java hadoop jdbc hive hortonworks-data-platform

我在连接到beeline,hive2版本1.2.1000.2.5.0.0时遇到此异常,我已将hive-jdbc.jar文件添加到Windows 10计算机上的classpath中。

例外:

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. 我不确定我需要在这里添加哪些其他罐子?
  2. 这些jar文件会根据Hive的版本而改变吗?
  3. Cloudera和Hortonworks等是否有不同的jar文件,请访问此page,这太令人困惑了。
  4. 为什么连接到Hive这么复杂,我在Hortonworks上有我的配置文件,http://142.56.78.174:10003/default,我已经将hive-jdbc jar文件添加到类路径中,我在下面有我的java类。这还不够吗?
  5. 请让我知道我真正想念的是什么。
  6. 感谢您的回答,我已经尝试过这个,

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

    提前致谢!!

2 个答案:

答案 0 :(得分:1)

如果您使用最新的jar (例如版本2.1x)  你应该试试这个

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

而不是

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

对于最近的JDBC jar,没有 org.apache.hadoop.hive.jdbc.HiveDriver

答案 1 :(得分:0)

我在Windows上尽我所能无法成功运行它。所以,我切换到centos,这也应该帮助Windows用户,对任何问题做评论:)

很难找到1.2.1000.2.5.0.0的所有jar,所以我尝试了1.2.1版本,它在我们的nn2服务器上工作。我在Windows上浪费了很多时间,Linux有很好的文档。

<强> 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));
    }
  }
}

上面是我在nn2上尝试的简单程序,在运行任何这些文件之前,请将所有这些jar添加到您正在处理的任何计算机的类路径中。

我希望我没有错过一些jar文件,这应该对你有用而没有错误:

  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
  16. 如果您只是想在不添加到类路径的情况下测试连接,则可以使用此命令来执行此操作 的 Linux的:

    编译:

    [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
    

    <强>执行:

    [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
    

    基本命令是

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