我尝试为hive2 get error运行jbdc代码。我有Hive1.2.0版本Hadoop1.2.1版本。但是在命令行中,hive和beeline可以正常工作,没有任何问题,但是jdbc会出错。
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 {
//BasicConfigurator.configure();
try {
Class.forName(driverName);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//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 = "cdr";
stmt.execute("drop table if exists " + tableName);
// 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));
}
}
}
我在运行代码时遇到这个错误。
15/06/19 12:08:53 INFO jdbc.HiveConnection: Will try to open client transport with JDBC Uri: jdbc:hive2://localhost:10000/default
15/06/19 12:08:53 ERROR jdbc.HiveConnection: Error opening session
org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=default})
at org.apache.thrift.TApplicationException.read(TApplicationException.java:111)
at org.apache.thrift.TServiceClient.receiveBase(TServiceClient.java:71)
at org.apache.hive.service.cli.thrift.TCLIService$Client.recv_OpenSession(TCLIService.java:156)
at org.apache.hive.service.cli.thrift.TCLIService$Client.OpenSession(TCLIService.java:143)
at org.apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:578)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:192)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at HiveJdbcClient.main(HiveJdbcClient.java:24)
Exception in thread "main" java.sql.SQLException: Could not establish connection to jdbc:hive2://localhost:10000/default: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=default})
at org.apache.hive.jdbc.HiveConnection.openSession(HiveConnection.java:589)
at org.apache.hive.jdbc.HiveConnection.<init>(HiveConnection.java:192)
at org.apache.hive.jdbc.HiveDriver.connect(HiveDriver.java:105)
at java.sql.DriverManager.getConnection(DriverManager.java:571)
at java.sql.DriverManager.getConnection(DriverManager.java:215)
at HiveJdbcClient.main(HiveJdbcClient.java:24)
Caused by: org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset! Struct:TOpenSessionReq(client_protocol:null, configuration:{use:database=default})
有什么解决办法吗?
3条答案
按热度按时间uujelgoq1#
当对旧版本的hiveserver2使用新版本的jdbc驱动程序时,就会发生这种情况。使用与服务器版本相同的jdbc驱动程序(首选),或者使用比服务器版本旧的版本。
该问题已在中跟踪https://issues.apache.org/jira/browse/hive-6050
xfb7svmp2#
org.apache.thrift.tapplicationexception:必填字段“client\u protocol”未设置!struct:topensessionreq(客户)_protocol:null,配置:{use:database=default})在org.apache.thrift.tapplicationexception.read(tapplicationexception。java:111)
如果配置单元和配置单元jdbc之间存在版本不匹配,则通常会发生此错误。请检查两个版本是否匹配。
有关更多信息,请参阅此页。
wrrgggsh3#
“客户端协议”字段在Hive1.1版本中可用。您需要在类路径中添加hive-service-1.1.0.jar。
我希望你的问题能解决。