最近需要做一个通过java提交Impala查询的功能,翻阅了一些资料,可以通过Cloudera Manager提供的API实现Impala查询
但是多了一层CM平台,如果CM平台不稳定,很可能会导致Impala查询出错。如果能直接让Impala查询并返回结果是最好的方式了。
参考GitHub上一位大佬的代码,网上并没有直接可用的jar包或者java文件,需要通过thrift文件生成java文件,添加libthrift-0.9.1.jar、hive-service.jar、hive-metastore.jar、slf4j.api-1.6.1.jar.zip和commons-lang3-3.3.2-bin.tar.gz依赖,然后就能使用了,其中hive-service和hive-metastore版本根据自己使用情况而定,详细的操作可以看大佬代码中的build.sh文件。
下面是代码示例
submitQuery
public static HashMap submitQuery(String host, int port, String sql, String username, String password) throws Exception {
TSocket transport = new TSocket(host, port, 99999);
TBinaryProtocol protocol = new TBinaryProtocol(transport);
TCLIService.Client client = new ImpalaHiveServer2Service.Client(protocol);
transport.open();
TOpenSessionReq openReq = new TOpenSessionReq();
openReq.setClient_protocol(TProtocolVersion.HIVE_CLI_SERVICE_PROTOCOL_V1);
openReq.setUsername(username);
openReq.setPassword(password);
TOpenSessionResp openResp = client.OpenSession(openReq);
// TStatus status = openResp.getStatus();
// if (status.getStatusCode() == TStatusCode.ERROR_STATUS) {