根据此处的链接https://cwiki.apache.org/Hive/hiveclient.html#HiveClient-ThriftJavaClient。它说
Thrift Java Client 在嵌入模式和独立模式下运行 服务器
如何在嵌入模式下使用hive运行thrift java客户端?
答案 0 :(得分:2)
所以这里是如何在嵌入模式下运行hive'thrift'客户端。
import org.apache.hadoop.hive.service.HiveInterface;
import org.apache.hadoop.hive.service.HiveServer;
...
HiveInterface hiveClient = new HiveServer.HiveServerHandler();
将以下内容添加到类路径
$ HIVE_HOME / lib中/ *。罐
$ HIVE_HOME / conf
我在hive源代码中找到了这个$ HIVE_HOME / src / jdbc / src / java /../ HiveConnection.java
答案 1 :(得分: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));
}
这个问题确实很老,但是我仍然需要一些解决方案,但是google / so / hive wiki中没有任何有用的信息,因此我深入研究源代码并找到了这些。
全部基于Hive 3.1.2。
参考: