package com.netqin.hive.kpi;
import org.apache.hadoop.hive.service.HiveClient;
import org.apache.hadoop.hive.service.HiveServerException;
import org.apache.log4j.Logger;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.TTransportException;
public class AndroidKpi {
// Thrift Server IP Address
private static String HOST = "192.168.0.59";
// Thrift Server Port
private static int PORT = 10000;
public static final Logger logger = Logger.getLogger(AndroidKpi.class); // logger
public static void main(String[] args) {
TTransport transport = new TSocket(HOST, PORT);
TProtocol protocol = new TBinaryProtocol(transport);
HiveClient client = new HiveClient(protocol);
String tableName = "tableName";
try {
transport.open();
// Execute Hive Query Language Command
client.execute("select count(*) from "+tableName);
System.out.println(client.fetchOne());
//logger.debug("size "+client.fetchOne());
} catch (TTransportException e) {
logger.error(e.getMessage());
e.printStackTrace();
} catch (HiveServerException e) {
logger.error(e.getMessage());
e.printStackTrace();
} catch (TException e) {
logger.error(e.getMessage());
e.printStackTrace();
}
}
}
运行以上的程序所必需的Jar包:
本文提供了一个使用Java实现的Hive客户端示例程序,该程序通过Thrift连接到Hive Server并执行简单的HiveQL查询。示例展示了如何设置连接、执行查询以及处理可能发生的异常。
2908

被折叠的 条评论
为什么被折叠?



