Java 未设置必填字段“client_protocol”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/24694415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 14:12:28  来源:igfitidea点击:

Required field 'client_protocol' is unset

javahadoopjdbchive

提问by user3782579

I am using Hive 0.12, and I'm trying the JDBC from apache. When I try to run the code, I get apache.thrift.TApplicationException.

我正在使用 Hive 0.12,并且我正在尝试来自 apache 的 JDBC。当我尝试运行代码时,我得到 apache.thrift.TApplicationException。

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", "hive", "");
    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));
    }
    // describe table
    sql = "describe " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(res.getString(1) + "\t" + res.getString(2));
    }

    // load data into table
    // NOTE: filepath has to be local to the hive server
    // NOTE: /tmp/a.txt is a ctrl-A separated file with two fields per line
    String filepath = "/tmp/a.txt";
    sql = "load data local inpath '" + filepath + "' into table " + tableName;
    System.out.println("Running: " + sql);
    stmt.execute(sql);

    // select * query
    sql = "select * from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(String.valueOf(res.getInt(1)) + "\t" + res.getString(2));
    }

    // regular hive query
    sql = "select count(1) from " + tableName;
    System.out.println("Running: " + sql);
    res = stmt.executeQuery(sql);
    while (res.next()) {
        System.out.println(res.getString(1));
    }
}

}

}

I imported all the necessary jars, and when I try to run my code, I get the following error:

我导入了所有必需的 jar,当我尝试运行我的代码时,出现以下错误:

 org.apache.thrift.TApplicationException: Required field 'client_protocol' is unset!   Struct:TOpenSessionReq(client_protocol:null)

How can I fix this?

我怎样才能解决这个问题?

回答by Kamaldeep Singh

Got same issue. It works if you set

有同样的问题。如果你设置它就可以工作

hive JDBC Maven Repo version as 1.1.0 .

hive JDBC Maven Repo 版本为 1.1.0 。

Check this jira. Newest hive-jdbc version is not supported with HIve 0.13. https://issues.apache.org/jira/browse/HIVE-6050

检查这个吉拉。HIve 0.13 不支持最新的 hive-jdbc 版本。 https://issues.apache.org/jira/browse/HIVE-6050

Add this in your pom.

将此添加到您的 pom.xml 文件中。

<dependency>
  <groupId>org.apache.hive</groupId>
  <artifactId>hive-jdbc</artifactId>
  <version>1.1.0</version>
  <classifier>standalone</classifier>
</dependency>

回答by Ihar Sadounikau

This indicates a version mismatch between client and server, namely that the client is newer than the server, which is your case.

这表明客户端和服务器之间的版本不匹配,即客户端比服务器新,这就是您的情况。

回答by Sunny

Guys even I faced the same issue and go the solution by doing the following steps

伙计们,我也遇到了同样的问题,并通过执行以下步骤来解决

Step 01:- Include the Hive lib jars on yours eclipse(which ever might be the IDE) by using the below link.

步骤 01:- 使用以下链接在您的 eclipse(可能是 IDE)中包含 Hive lib jar。

http://mirrors.supportex.net/apache/hive/hive-1.0.1/(apache-hive-1.0.1-bin.tar.gz)

http://mirrors.supportex.net/apache/hive/hive-1.0.1/(apache-hive-1.0.1-bin.tar.gz)

Step 02: add hadoop-core-1.1.0 jar

步骤 02:添加 hadoop-core-1.1.0 jar

as everyone mentioned this error occurs due to the version mismatch with hadoop standalone and hadoop core.

正如每个人都提到的,这个错误是由于版本与 hadoop standalone 和 hadoop core 不匹配造成的。

回答by Ganesh

I have faces same issue. I fixed this by following below steps.

我有同样的问题。我按照以下步骤解决了这个问题。

<dependency>
    <groupId>org.apache.hive</groupId>
    <artifactId>hive-jdbc</artifactId>
    <version>1.1.0</version>
    <classifier>standalone</classifier>
    <exclusions>
        <exclusion>
            <groupId>org.eclipse.jetty.aggregate</groupId>
            <artifactId>*</artifactId>
        </exclusion>
    </exclusions>
</dependency>
  • Make sure that, you add above dependency.
  • Class.forName("org.apache.hive.jdbc.HiveDriver");Add this before calling DriverManager.getConnection(url);
  • Represent hive url by IP instead of hostname.
  • 确保您添加了上述依赖项。
  • Class.forName("org.apache.hive.jdbc.HiveDriver");在调用之前添加这个 DriverManager.getConnection(url);
  • 通过 IP 而不是主机名来表示 hive url。