我在连接到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));
}
}
}
感谢您的回答,我已经尝试过这个,
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
提前致谢!!
答案 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文件,这应该对你有用而没有错误:
如果您只是想在不添加到类路径的情况下测试连接,则可以使用此命令来执行此操作 的 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