简体   繁体   English

带有嵌入式配置单元的 Java 节俭客户端?

[英]java thrift client with embedded hive?

According to the link here https://cwiki.apache.org/Hive/hiveclient.html#HiveClient-ThriftJavaClient .根据这里的链接https://cwiki.apache.org/Hive/hiveclient.html#HiveClient-ThriftJavaClient It says that它说

Thrift Java Client Operates both in embedded mode and on standalone server. Thrift Java Client 在嵌入式模式和独立服务器上运行。

How do I run the thrift java client with hive in embedded mode?如何在嵌入式模式下使用 hive 运行 thrift java 客户端?

So here is how to run hive 'thrift' client in embedded mode.所以这里是如何在嵌入式模式下运行 hive 'thrift' 客户端。

import org.apache.hadoop.hive.service.HiveInterface;
import org.apache.hadoop.hive.service.HiveServer;
...
HiveInterface hiveClient = new HiveServer.HiveServerHandler();

Add following to classpath将以下内容添加到类路径

$HIVE_HOME/lib/*.jar $HIVE_HOME/lib/*.jar

$HIVE_HOME/conf $HIVE_HOME/conf

I found this in the hive source code here $HIVE_HOME/src/jdbc/src/java/../HiveConnection.java我在 $HIVE_HOME/src/jdbc/src/java/../HiveConnection.java 的 hive 源代码中找到了这个

Hive use Thrift as RPC framework, and thrift rpc make it easy to "Operates both in embedded mode and on standalone server". Hive 使用 Thrift 作为 RPC 框架,而 thrift rpc 使“在嵌入式模式和独立服务器上运行”变得容易。

Client Mode (connect to a standalone server)客户端模式(连接到独立服务器)

    HiveConf hiveConf = new HiveConf();
    hiveConf.addResource("/Users/tzp/pppathh/hive-site.xml");

    TTransport transport = new TSocket("127.0.0.1", 10000);
    transport = PlainSaslHelper.getPlainTransport(USERNAME, PASSWORD, transport);
    TBinaryProtocol protocol = new TBinaryProtocol(transport);
    transport.open();

    ThriftCLIServiceClient cliServiceClient = new ThriftCLIServiceClient(new TCLIService.Client(protocol), hiveConf);
    SessionHandle sessionHandle = cliServiceClient.openSession(USERNAME, PASSWORD);

    OperationHandle operationHandle = cliServiceClient.executeStatement(sessionHandle, "select * from u_data_ex limit 2", null);
    RowSet results = cliServiceClient.fetchResults(operationHandle);

    for (Object[] result : results) {
        System.out.println(Arrays.asList(result));
    }

Embedded Mode (no external server)嵌入式模式(无外部服务器)

    HiveConf hiveConf = new HiveConf();
    hiveConf.addResource("/Users/tzp/ppppathh/hive-site.xml");
    hiveConf.set("fs.defaultFS", "hdfs://localhost:9000");

    EmbeddedThriftBinaryCLIService service = new EmbeddedThriftBinaryCLIService();
    service.init(hiveConf);

    ICLIService icliService = service.getService();
    SessionHandle sessionHandle = icliService.openSession(USERNAME, PASSWORD, null);

    OperationHandle operationHandle = icliService.executeStatement(sessionHandle, "select * from u_data_ex limit 2", null);
    RowSet results = icliService.fetchResults(operationHandle);

    for (Object[] result : results) {
        System.out.println(Arrays.asList(result));
    }

This question is really old, but I still need some solutions, but there isn't any useful info at google/so/hive wiki, so I dive into the source code and find these.这个问题真的很老,但我仍然需要一些解决方案,但是在 google/so/hive wiki 上没有任何有用的信息,所以我深入研究源代码并找到这些。

All based on Hive 3.1.2.全部基于 Hive 3.1.2。

Reference:参考:

  • org.apache.hive.service.server.HiveServer2 org.apache.hive.service.server.HiveServer2
  • org.apache.hive.service.cli.CLIServiceTest org.apache.hive.service.cli.CLIServiceTest
  • org.apache.hive.service.cli.thrift.ThriftCLIServiceTest org.apache.hive.service.cli.thrift.ThriftCLIServiceTest

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM