2016-05-30 53 views
0

我想連接到服務器上的配置單元數據庫使用java程序。下面是java的代碼:錯誤通過java程序連接到配置單元數據庫

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


public class HiveQueryJava { 

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

    public static void main(String[] args) throws SQLException { 

     // Register driver and create driver instance 
     Class.forName(driverName); 


     // get connection 
     Connection con = DriverManager.getConnection("jdbc:hive -hiveconf mapred.job.queue.name=queuename://ip:port/dbname", "uname", ""); 

     // create statement 
     Statement stmt = con.createStatement(); 

     // execute statement 
     Resultset res = stmt.executeQuery("SELECT pincode FROM pincode_city_map LIMIT 5;"); 

     System.out.println("Result:"); 

     while (res.next()) { 
     System.out.println(res.getString(1)); 
     } 
     con.close(); 
    } 
} 

我爲4個NoClassFoundExceptions添加了3個jar文件。 jar文件:

hive-service-0.11.0.jar 
libthrift-0.8.0.jar 
hive-jdbc-0.10.0.jar 
hive-metastore-0.10.0 

現在獲得新NoClassFoundException如下:每到這個異常談到時間

Exception in thread "main" java.lang.NoClassDefFoundError: com/facebook/fb303/FacebookService$Iface 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:760) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) 
     at java.net.URLClassLoader.access$100(URLClassLoader.java:73) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:368) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:362) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:361) 
     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) 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:760) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) 
     at java.net.URLClassLoader.access$100(URLClassLoader.java:73) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:368) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:362) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:361) 
     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) 
     at java.lang.ClassLoader.defineClass1(Native Method) 
     at java.lang.ClassLoader.defineClass(ClassLoader.java:760) 
     at java.security.SecureClassLoader.defineClass(SecureClassLoader.java:142) 
     at java.net.URLClassLoader.defineClass(URLClassLoader.java:467) 
     at java.net.URLClassLoader.access$100(URLClassLoader.java:73) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:368) 
     at java.net.URLClassLoader$1.run(URLClassLoader.java:362) 
     at java.security.AccessController.doPrivileged(Native Method) 
     at java.net.URLClassLoader.findClass(URLClassLoader.java:361) 
     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) 
     at org.apache.hadoop.hive.jdbc.HiveDriver.connect(HiveDriver.java:104) 
     at java.sql.DriverManager.getConnection(DriverManager.java:664) 
     at java.sql.DriverManager.getConnection(DriverManager.java:247) 
     at HiveQueryJava.main(HiveQueryJava.java:21) 
Caused by: java.lang.ClassNotFoundException: com.facebook.fb303.FacebookService$Iface 
     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) 
     ... 40 more 

,我必須尋找此類和下載jar文件。有沒有任何主jar文件包含連接到hive數據庫的所有jar文件?

最後我想通過java程序連接到配置單元。任何人都可以提供不同的代碼或建議其他方式來解決這個問題?

+1

我會推薦Maven以transitively管理你的依賴關係。除此之外,我會認爲Hive jdbc是你需要的所有 –

+0

@NaveenRamawat不,這是一個不同的錯誤 –

+1

可能重複的[如何解決這個Java類未找到異常?](http://stackoverflow.com/questions/ 17408769 /如何解決這個java類的未發現異常)或[如何解決java-lang-noclassdeffounderror](http://stackoverflow.com/questions/17973970/how -to-solve-java-lang-noclassdeffounderror) –

回答

0

如果使用hortonworks,他們已經通過以下兩個jar捆綁所有你可以連接jars..and: - http://repo.hortonworks.com/content/repositories/releases/org/apache/hadoop/hadoop-common/2.7.1.2.3.2.0-2950/hadoop-common-2.7.1.2.3.2.0-2950.jar http://repo.hortonworks.com/content/repositories/releases/org/apache/hive/hive-jdbc/1.2.1.2.3.2.0-2950/hive-jdbc-1.2.1.2.3.2.0-2950-standalone.jar

此外,你只能連接配置單元服務器2,所以你的連接字符串應該像下面那樣,使用hive2而不是配置單元。

Connection con = DriverManager.getConnection("jdbc:hive2://hadoopip:10000/database","username", "password"); 
相關問題