簡體   English   中英

帶有嵌入式配置單元的 Java 節儉客戶端?

[英]java thrift client with embedded hive?

根據這里的鏈接https://cwiki.apache.org/Hive/hiveclient.html#HiveClient-ThriftJavaClient 它說

Thrift Java Client 在嵌入式模式和獨立服務器上運行。

如何在嵌入式模式下使用 hive 運行 thrift java 客戶端?

所以這里是如何在嵌入式模式下運行 hive 'thrift' 客戶端。

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

將以下內容添加到類路徑

$HIVE_HOME/lib/*.jar

$HIVE_HOME/conf

我在 $HIVE_HOME/src/jdbc/src/java/../HiveConnection.java 的 hive 源代碼中找到了這個

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

這個問題真的很老,但我仍然需要一些解決方案,但是在 google/so/hive wiki 上沒有任何有用的信息,所以我深入研究源代碼並找到這些。

全部基於 Hive 3.1.2。

參考:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM