java thrift client with embedded hive?

问题描述 投票:0回答:2

根据这里的链接 https:/cwiki.apache.orgHivehiveclient.html#HiveClient-ThriftJavaClient。. 它说:

Thrift Java客户端既可以在嵌入式模式下运行,也可以在独立的服务器上运行。

如何在嵌入式模式下运行thrift java客户端与hive?

java hive thrift
2个回答
2
投票

因此,这里是如何运行hive'thrift'客户端在嵌入式模式。

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

添加以下内容到classpath

$HIVE_HOMElib*.jar

$HIVE_HOMEconf

我在这里的hive源代码中找到了这个 $HIVE_HOMEsrcjdbcsrcjava.HiveConnection.java。


0
投票

Hive使用Thrift作为RPC框架,而Thrift rpc可以轻松实现 "同时在嵌入式模式和独立服务器上操作"。

客户端模式(连接到独立服务器)

    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));
    }

嵌入式模式(无外部服务器

    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));
    }

这个问题真的是老问题了,但我还是需要一些解决方案,但在googlesohive wiki上没有任何有用的信息,所以我深入到源代码中,找到了这些。

都是基于Hive 3.1.2的。

参考文献。

  • 参考: org.apache.hive.service.server.HiveServer2。
  • org.apache.hive.service.cli.CLIServiceTest。
  • org.apache.hive.service.cli.thrift.ThriftCLIServiceTest。
© www.soinside.com 2019 - 2024. All rights reserved.