use of org.apache.hive.service.rpc.thrift.TFetchResultsResp 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;
}
// running state when fetching results (implicit state transition)
if ((statement instanceof HiveStatement) && (operationStatus == null || !operationStatus.isHasResultSet())) {
operationStatus = ((HiveStatement) statement).waitForOperationToComplete();
}
try {
TFetchOrientation orientation = TFetchOrientation.FETCH_NEXT;
if (fetchFirst) {
// If we are asked to start from beginning, 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);
LOG.debug("HiveQueryResultsFetchReq: {}", fetchReq);
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()) {
return false;
}
row = fetchedRowsItr.next();
rowsFetched++;
} catch (SQLException eS) {
throw eS;
} catch (Exception ex) {
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.TFetchResultsResp 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.");
}
TFetchResultsResp tFetchResultsResp = null;
try {
if (stmtHandle.isPresent()) {
TFetchResultsReq tFetchResultsReq = new TFetchResultsReq(stmtHandle.get(), 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.");
} else {
return Collections.emptyList();
}
}
} catch (SQLException e) {
throw e;
} catch (Exception e) {
throw new SQLException("Error when getting query log", e);
}
final List<String> logs = new ArrayList<>();
try {
final 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);
}
return Collections.unmodifiableList(logs);
}
use of org.apache.hive.service.rpc.thrift.TFetchResultsResp in project hive by apache.
the class ThriftCLIService method FetchResults.
@Override
public TFetchResultsResp FetchResults(TFetchResultsReq req) throws TException {
TFetchResultsResp resp = new TFetchResultsResp();
final int maxFetchSize = hiveConf.getIntVar(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_RESULTSET_MAX_FETCH_SIZE);
if (req.getMaxRows() > maxFetchSize) {
LOG.warn("Fetch Size greater than maximum allowed. Capping fetch size. [req={}, max={}]", req.getMaxRows(), maxFetchSize);
req.setMaxRows(maxFetchSize);
}
try {
RowSet rowSet = cliService.fetchResults(new OperationHandle(req.getOperationHandle()), FetchOrientation.getFetchOrientation(req.getOrientation()), req.getMaxRows(), FetchType.getFetchType(req.getFetchType()));
resp.setResults(rowSet.toTRowSet());
resp.setHasMoreRows(false);
resp.setStatus(OK_STATUS);
} catch (Exception e) {
LOG.error("Failed fetch results [request: {}]", req, e);
resp.setStatus(HiveSQLException.toTStatus(e));
}
return resp;
}
use of org.apache.hive.service.rpc.thrift.TFetchResultsResp 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