我想使用java程序连接到服务器上的hive数据库。下面是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程序连接到hive。任何人都可以提供不同的代码或建议其他方式来解决这个问题吗?
答案 0 :(得分:2)
CLASSPATH中缺少libfb * - * .jar。请确保CLASSPATH中包含的必要罐子的版本正确且没有任何重复。
以下页面可能有所帮助,
http://docs.aws.amazon.com/ElasticMapReduce/latest/DeveloperGuide/HiveJDBCDriver.html https://cwiki.apache.org/confluence/display/Hive/HiveServer2+Clients#HiveServer2Clients-JDBCClientSampleCode
答案 1 :(得分:1)
如果您正在使用hortonworks,他们已经捆绑了所有的罐子......您可以使用以下两个罐子连接: - 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");