简体   繁体   中英

java thrift client with embedded hive?

According to the link here https://cwiki.apache.org/Hive/hiveclient.html#HiveClient-ThriftJavaClient . It says that

Thrift Java Client Operates both in embedded mode and on standalone server.

How do I run the thrift java client with hive in embedded mode?

So here is how to run hive 'thrift' client in embedded mode.

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/conf

I found this in the hive source code here $HIVE_HOME/src/jdbc/src/java/../HiveConnection.java

Hive use Thrift as RPC framework, and thrift rpc make it easy to "Operates both in embedded mode and on standalone server".

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.

All based on Hive 3.1.2.

Reference:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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