Hive JDBC Code抛出异常。我尝试使用Hive 0.13.0,Hive 1.12.1和Hive 0.12.0。
但是,这些都没有创建连接。
package com.cisco.installbase.hiveconnector;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.log4j.Logger;
import java.sql.Connection;
public class CreateConnection {
private static final Logger LOGGER = Logger.getLogger(CreateConnection.class);
private static Connection instance = null;
static final String drivername = "org.apache.hive.jdbc.HiveDriver";
private CreateConnection() {
try {
LOGGER.info("Creating the connection");
Class.forName(drivername);
instance = DriverManager.getConnection("jdbc:hive://hddev-c01-edge-02:9083/");
} catch (ClassNotFoundException e) {
LOGGER.error("Error occurred to create connection",e);
} catch (SQLException e) {
LOGGER.error(e.getMessage());
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
public static Connection getInstance() {
LOGGER.info("Connection Instance");
if (instance == null) {
instance = (Connection) new CreateConnection();
}
return instance;
}
}
异常StackTrace:
16/02/11 07:01:46 INFO hiveconnector.CreateConnection:Connection Instance 16/02/11 07:01:46 INFO hiveconnector.CreateConnection: 创建连接16/02/11 07:01:46错误 hiveconnector.CreateConnection:找不到合适的驱动程序 jdbc:hive:// hddev-c01-edge-02:9083 /线程中的异常" main" java.lang.ExceptionInInitializerError由以下原因引起: java.lang.ClassCastException:com.cisco.installbase.hiveconnector.Cre ateConnection无法转换为java.sql.Connection at com.cisco.installbase.hiveconnector.CreateConnection.getInstance(克雷亚 teConnection.java:39) 在com.cisco.installbase.hiveconnector.CommonDBUtilities。(CommonDBUtilities.java:19) 在com.cisco.installbase.hiveconnector.MainApp。(MainApp.java:33)
的pom.xml
<dependency>
<groupId>org.apache.hive</groupId>
<artifactId>hive-jdbc</artifactId>
<version>0.12.0</version>
</dependency>
< dependency> <groupId>org.apache.hive</groupId> <artifactId>hive-jdbc</artifactId>
<version>1.2.1</version> </dependency>
<dependency>
答案 0 :(得分:4)
你有两个问题,第一个&#34;没有为jdbc找到合适的驱动程序:hive:// hddev-c01-edge-02:9083 /&#34; 意味着你要么您加载的驱动程序的驱动程序或URL错误。
当您加载org.apache.hive.jdbc.HiveDriver
时,您使用HiveServer2 client,其中使用jdbc:hive2://<host>:<port>
作为连接字符串(请注意网址中的 2 )。< / p>
对于第二个问题,请查看异常:
线程中的异常&#34; main&#34; java.lang.ExceptionInInitializerError由以下原因引起:java.lang.ClassCastException: com.cisco.installbase.hiveconnector.CreateConnection 无法在com.cisco中强制转换为 java.sql.Connection .installbase.hiveconnector.CreateConnection.getInstance(Crea teConnection.java:39)
和你的代码:
public static Connection getInstance() {
LOGGER.info("Connection Instance");
if (instance == null) {
instance = (Connection) new CreateConnection();
}
return instance;
}
您正在尝试将自己的CreateConnection
课程的实例投射到java.sql.Connection
,而不是java.sql.Connection
。你的代码并没有多大意义:你是1)在构造函数中初始化静态字段,2)尝试将所述类作为Connection
返回。作为旁注:与数据库的静态,单例连接通常是一个坏主意。
您可以通过将构造函数更改为返回public class CreateConnection {
private static final Logger LOGGER = Logger.getLogger(CreateConnection.class);
private static Connection instance = null;
static final String drivername = "org.apache.hive.jdbc.HiveDriver";
public static Connection getInstance() {
LOGGER.info("Connection Instance");
if (instance == null) {
instance = createConnection();
}
return instance;
}
private static Connection createConnection() {
try {
LOGGER.info("Creating the connection");
Class.forName(drivername);
return DriverManager.getConnection("jdbc:hive2://hddev-c01-edge-02:9083/");
} catch (ClassNotFoundException e) {
LOGGER.error("Error occurred to create connection",e);
} catch (SQLException e) {
LOGGER.error(e.getMessage());
} catch (Exception e) {
LOGGER.error(e.getMessage());
}
}
}
的方法来修复代码,但这仍然会让您不了解单例连接对象:
use