在 Hortonworks 上使用 Jdbc 远程连接到 Hive 时出现 ClassNotFoundException
Posted
技术标签:
【中文标题】在 Hortonworks 上使用 Jdbc 远程连接到 Hive 时出现 ClassNotFoundException【英文标题】:ClassNotFoundException while connecting to Hive Remotely using Jdbc on Hortonworks 【发布时间】:2016-12-06 04:49:29 【问题描述】:我在连接到 beeline 时遇到此异常,hive2 版本 1.2.1000.2.5.0.0,我已将 hive-jdbc.jar 文件添加到我的 Windows 10 机器上的类路径中。
例外:
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));
-
我不确定我需要在此处添加哪些其他 jar?
这些 jar 文件会根据 Hive 的版本而变化?
Cloudera 和 Hortonworks 等有不同的 jar 文件吗,请访问这个page,这太混乱了。
为什么连接到 Hive 如此复杂,我在 Hortonworks 上有我的 hive,即http://142.56.78.174:10003/default,我已将 hive-jdbc jar 文件添加到类路径中,并且我在下面有我的 java 类。这还不够吗?
请让我知道我真正缺少什么。
谢谢你的回答,我已经试过了,
private static String driverName = "org.apache.hive.jdbc.HiveDriver";
提前致谢!!
【问题讨论】:
【参考方案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
【讨论】:
谢谢我已经试过了private static String driverName = "org.apache.hive.jdbc.HiveDriver";
【参考方案2】:
我在 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 文件,这对你来说应该没有错误:
-
http://mvnrepository.com/artifact/org.apache.hive/hive-serde/1.2.1
http://mvnrepository.com/artifact/org.apache.hive/hive-common/1.2.1
http://mvnrepository.com/artifact/org.apache.hive/hive-shims/1.2.1
http://mvnrepository.com/artifact/org.apache.hive/hive-beeline/1.2.1
http://mvnrepository.com/artifact/org.apache.hive/hive-exec/1.2.1
http://mvnrepository.com/artifact/org.apache.hive/hive-metastore/1.2.1
http://mvnrepository.com/artifact/org.apache.hive/hive-service/1.2.1
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.2.5
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore/4.2.1
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpcore/4.3-alpha1
https://mvnrepository.com/artifact/org.apache.httpcomponents/httpclient/4.3.4
https://mvnrepository.com/artifact/commons-logging/commons-logging/1.2
https://mvnrepository.com/artifact/org.apache.hadoop/hadoop-common/2.2.0
https://mvnrepository.com/artifact/org.slf4j/slf4j-api/1.7.21
http://mvnrepository.com/artifact/org.apache.hive/hive-jdbc/1.2.1
如果您只想测试连接而不添加到类路径中,您可以使用此命令 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
【讨论】:
以上是关于在 Hortonworks 上使用 Jdbc 远程连接到 Hive 时出现 ClassNotFoundException的主要内容,如果未能解决你的问题,请参考以下文章
如何在 hortonworks 沙箱上的 Ambari 中启用 HDFS 文件视图?
从外部在 Hortonworks Sandbox 上执行 Spark 作业