一开始用的JdbcTemplate做查询,后来又要返回日志,只能通过手写jdbc来实现了,期间遇到的问题在下面有记录
public QueryResultVo queryAndLog(String sql, List<Object> params) {
QueryResultVo result = new QueryResultVo();
List<Map<String, Object>> list = Collections.EMPTY_LIST;
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
Class.forName("org.apache.hive.jdbc.HiveDriver");
connection = DriverManager.getConnection(url, userName, password);
statement = connection.prepareStatement(sql);
if (params != null && params.size() > 0) {
for (int i = 0; i < params.size(); i++) {
statement.setObject(i + 1, params.get(i));
}
}
rs = statement.executeQuery();
if (rs != null) {
/**
* 默认设置数据量为50条
*/
int count = 50;
list = new ArrayList<>(count);
/**
* 遍历生成数据列表
*/
while (rs.next()) {
ResultSetMetaData metaData = rs.getMetaData();
int columnCount = metaData.getColumnCount();
Map<String, Object> data = new LinkedHashMap<>(columnCount);
for (int i = 0; i < columnCount; i++) {
String columnName = metaData.getColumnName(i + 1);
Object value = rs.getObject(columnName);
data.put(columnName, value);
}
list.add(data);
}
result.setData(list);
/**
* 生成列标题
*/
if (list.size() > 0) {
result.setColumnList(new ArrayList<>(list.get(0).keySet()));
}
}
/**
* 查询日志,statement要进行转换
*/
HiveStatement hiveStatement = (HiveStatement) statement;
List<String> logList = hiveStatement.getQueryLog(true, 1000);
result.setLogList(logList);
} catch (Exception e) {
e.printStackTrace();
List<String> logList = new ArrayList<>(1);
logList.add(e.getMessage());
result.setLogList(logList);
result.setSuccess(false);
} finally {
/**
* 关闭连接
*/
JdbcUtils.closeConnection(connection);
JdbcUtils.closeStatement(statement);
JdbcUtils.closeResultSet(rs);
}
return result;
}
期间遇到了问题,获取日志时报找不到日志的异常:
org.apache.hive.service.cli.HiveSQLException: Couldn't find log associated with operation handle: OperationHandle [opType=EXECUTE_STATEMENT, getHandleIdentifier()=b3d05ca6-e3e8-4bef-b869-0ea0732c3ac5]
配置项:hive.server2.logging.operation.enabled = true,日志路径hive.server2.logging.operation.log.location = /usr/local/hive/logs/operation_logs
说明日志已经开启了,后来发现根本没有生成operation_logs这个目录,找运维后说是集群有问题,重启一次之后问题解决
汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗汗。。。。。。。。。。