}
public HiveQueryOutput getQueryResult(String tokenid, String username,
String database, String hql, int resultLimit, Boolean isStoreFile,
long timestamp) {
HiveQueryOutput hqo = new HiveQueryOutput();
UserGroupInformation ugi = ugiCache.getIfPresent(tokenid);
Connection conn = getConnection(ugi);
Statement stmt;
try {
stmt = conn.createStatement();
stmt.executeQuery("use " + database);
ResultSet rs = stmt.executeQuery(hql);
ResultSetMetaData rsm = rs.getMetaData();
int columnCount = rsm.getColumnCount();
List<String> columnNames = new ArrayList<String>(columnCount);
StringBuilder sb = new StringBuilder(200);
for (int i = 1; i <= columnCount; i++) {
columnNames.add(rsm.getColumnName(i));
if (logger.isDebugEnabled()) {
sb.append(rsm.getColumnName(i)).append("\t")
.append(rsm.getColumnTypeName(i)).append("\n");
}
}
if (logger.isDebugEnabled()) {
logger.debug("resultset meta data is:" + sb.toString());
}
hqo.setTitleList(columnNames);
int maxShowRowCount = resultLimit < USER_SHOW_ROW_MAXIMUM_COUNT ? resultLimit
: USER_SHOW_ROW_MAXIMUM_COUNT;
int currentRow = 0;
String storeFilePath = "";
BufferedWriter bw = null;
if (isStoreFile) {
storeFilePath = DataFileStore.getStoreFilePath(tokenid,
username, database, hql, timestamp);
bw = DataFileStore.openOutputStream(storeFilePath);
}
logger.info("isStoreFile:" + isStoreFile + " storeFilePath:"
+ storeFilePath);
if (!StringUtils.isBlank(storeFilePath)) {
hqo.setStoreFileLocation(storeFilePath);
}
while (rs.next()
&& currentRow < DataFileStore.FILE_STORE_LINE_LIMIT) {
if (currentRow < maxShowRowCount) {
List<String> oneRowData = new ArrayList<String>();
for (int i = 1; i <= columnCount; i++) {
String value = rs.getString(i) == null ? "" : rs
.getString(i);
oneRowData.add(value);
if (isStoreFile) {
bw.write(value);
if (i < columnCount)
bw.write('\t');
}
}
hqo.addRow(oneRowData);
if (isStoreFile) {
bw.write('\n');
}
} else if (isStoreFile) {
for (int i = 1; i <= columnCount; i++) {
bw.write(rs.getString(i));
if (i < columnCount)
bw.write('\t');
}
bw.write('\n');
} else {
break;
}
currentRow++;
}
if (isStoreFile && bw != null) {
bw.flush();
IOUtils.closeQuietly(bw);
}
rs.close();
stmt.close();
} catch (Exception e) {
hqo.setErrorMessage(e.toString());
logger.error("getQueryResult failed, db:" + database + " hql:"
+ hql, e);
} finally {
try {
conn.close();