2016-12-06 72 views
0

我在連接到直線,hive2版本1.2.1000.2.5.0.0時遇到此異常,我已將hive-jdbc.jar文件添加到Windows 10機器上的類路徑中。在Hortonworks上使用Jdbc遠程連接到Hive時出現ClassNotFoundException

例外:

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. 請讓我知道我真的很想念我。

感謝您的回答,我已經提前想這一點,

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

謝謝!

回答

0

我在Windows上盡力而爲,無法成功運行。所以,我切換到centos,這也應該幫助Windows用戶,請做任何問題的評論:)

很難找到所有的罐子到1.2.1000.2.5.0.0,所以我嘗試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文件,這應該爲你工作沒有任何錯誤:

  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

如果你只是想測試的連接不添加到類路徑中,你可以使用這個命令去做就 的Linux:

編譯:

[[email protected] ~]# 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 

執行:

[[email protected] ~]# 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 
0

,如果你使用最新的罐子(如版本2.1倍) 你應該在最近的JDBC JAR嘗試的

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

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

,而不是有沒有org.apache.hadoop.hive.jdbc.HiveDriver

+0

謝謝我已經試過這個'private static String driverName =「org.apache.hive.jdbc.HiveDriver」;' – Jordon

相關問題