use of org.apache.hive.service.rpc.thrift.TFetchResultsReq in project hive by apache.
the class HiveStatement method getQueryLog.
/**
* Get the execution logs of the given SQL statement.
* This method is a public API for usage outside of Hive, although it is not part of the
* interface java.sql.Statement.
* @param incremental indicate getting logs either incrementally or from the beginning,
* when it is true or false.
* @param fetchSize the number of lines to fetch
* @return a list of logs. It can be empty if there are no new logs to be retrieved at that time.
* @throws SQLException
* @throws ClosedOrCancelledStatementException if statement has been cancelled or closed
*/
public List<String> getQueryLog(boolean incremental, int fetchSize) throws SQLException, ClosedOrCancelledStatementException {
checkConnection("getQueryLog");
if (isCancelled) {
throw new ClosedOrCancelledStatementException("Method getQueryLog() failed. The " + "statement has been closed or cancelled.");
}
List<String> logs = new ArrayList<String>();
TFetchResultsResp tFetchResultsResp = null;
try {
if (stmtHandle != null) {
TFetchResultsReq tFetchResultsReq = new TFetchResultsReq(stmtHandle, getFetchOrientation(incremental), fetchSize);
tFetchResultsReq.setFetchType((short) 1);
tFetchResultsResp = client.FetchResults(tFetchResultsReq);
Utils.verifySuccessWithInfo(tFetchResultsResp.getStatus());
} else {
if (isQueryClosed) {
throw new ClosedOrCancelledStatementException("Method getQueryLog() failed. The " + "statement has been closed or cancelled.");
}
if (isExecuteStatementFailed) {
throw new SQLException("Method getQueryLog() failed. Because the stmtHandle in " + "HiveStatement is null and the statement execution might fail.");
} else {
return logs;
}
}
} catch (SQLException e) {
throw e;
} catch (TException e) {
throw new SQLException("Error when getting query log: " + e, e);
} catch (Exception e) {
throw new SQLException("Error when getting query log: " + e, e);
}
try {
RowSet rowSet;
rowSet = RowSetFactory.create(tFetchResultsResp.getResults(), connection.getProtocol());
for (Object[] row : rowSet) {
logs.add(String.valueOf(row[0]));
}
} catch (TException e) {
throw new SQLException("Error building result set for query log: " + e, e);
}
return logs;
}
use of org.apache.hive.service.rpc.thrift.TFetchResultsReq in project hive by apache.
the class HiveQueryResultSet method next.
/**
* Moves the cursor down one row from its current position.
*
* @see java.sql.ResultSet#next()
* @throws SQLException
* if a database access error occurs.
*/
public boolean next() throws SQLException {
if (isClosed) {
throw new SQLException("Resultset is closed");
}
if (emptyResultSet || (maxRows > 0 && rowsFetched >= maxRows)) {
return false;
}
/**
* Poll on the operation status, till the operation is complete.
* We need to wait only for HiveStatement to complete.
* HiveDatabaseMetaData which also uses this ResultSet returns only after the RPC is complete.
*/
if ((statement != null) && (statement instanceof HiveStatement)) {
((HiveStatement) statement).waitForOperationToComplete();
}
try {
TFetchOrientation orientation = TFetchOrientation.FETCH_NEXT;
if (fetchFirst) {
// If we are asked to start from begining, clear the current fetched resultset
orientation = TFetchOrientation.FETCH_FIRST;
fetchedRows = null;
fetchedRowsItr = null;
fetchFirst = false;
}
if (fetchedRows == null || !fetchedRowsItr.hasNext()) {
TFetchResultsReq fetchReq = new TFetchResultsReq(stmtHandle, orientation, fetchSize);
TFetchResultsResp fetchResp;
fetchResp = client.FetchResults(fetchReq);
Utils.verifySuccessWithInfo(fetchResp.getStatus());
TRowSet results = fetchResp.getResults();
fetchedRows = RowSetFactory.create(results, protocol);
fetchedRowsItr = fetchedRows.iterator();
}
if (fetchedRowsItr.hasNext()) {
row = fetchedRowsItr.next();
} else {
return false;
}
rowsFetched++;
} catch (SQLException eS) {
throw eS;
} catch (Exception ex) {
ex.printStackTrace();
throw new SQLException("Error retrieving next row", ex);
}
// NOTE: fetchOne doesn't throw new SQLFeatureNotSupportedException("Method not supported").
return true;
}
use of org.apache.hive.service.rpc.thrift.TFetchResultsReq in project hive by apache.
the class ThriftCLIServiceClient method fetchResults.
@Override
public RowSet fetchResults(OperationHandle opHandle, FetchOrientation orientation, long maxRows, FetchType fetchType) throws HiveSQLException {
try {
TFetchResultsReq req = new TFetchResultsReq();
req.setOperationHandle(opHandle.toTOperationHandle());
req.setOrientation(orientation.toTFetchOrientation());
req.setMaxRows(maxRows);
req.setFetchType(fetchType.toTFetchType());
TFetchResultsResp resp = cliService.FetchResults(req);
checkStatus(resp.getStatus());
return RowSetFactory.create(resp.getResults(), opHandle.getProtocolVersion());
} catch (HiveSQLException e) {
throw e;
} catch (Exception e) {
throw new HiveSQLException(e);
}
}
Aggregations