错误jdbc.HiveConnection:打开会话Hive时出错

时间:2015-06-19 06:43:25

标签: java hadoop jdbc hive

我尝试为Hive2 get错误运行JBDC代码。我有hive 1.2.0版hadoop 1.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 个答案:

答案 0 :(得分:13)

  

org.apache.thrift.TApplicationException:必填字段   'client_protocol'未设置!   结构:TOpenSessionReq(client_protocol:空,   配置:{用途:数据库=默认})           在org.apache.thrift.TApplicationException.read(TApplicationException.java:111)

如果您的hive和hive-jdbc之间存在版本不匹配,则通常会发生此错误。请检查两个版本是否匹配。

有关详细信息,请参阅this

答案 1 :(得分:2)

当旧版本的HiveServer2使用新版本的JDBC驱动程序时会发生这种情况。 使用与服务器(首选)版本相同的jdbc驱动程序,或者比服务器旧版本的版本。

问题在https://issues.apache.org/jira/browse/HIVE-6050

中进行了跟踪

答案 2 :(得分:0)

“client_protocol”字段在hive 1.1版本中可用。 您需要在类路径中添加hive-service-1.1.0.jar。

我希望你的问题能够得到解决。