Search in sources :

Example 1 with TFetchResultsResp

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;
}
Also used : TRowSet(org.apache.hive.service.rpc.thrift.TRowSet) SQLException(java.sql.SQLException) TFetchResultsResp(org.apache.hive.service.rpc.thrift.TFetchResultsResp) TFetchOrientation(org.apache.hive.service.rpc.thrift.TFetchOrientation) TFetchResultsReq(org.apache.hive.service.rpc.thrift.TFetchResultsReq) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLException(java.sql.SQLException)

Example 2 with TFetchResultsResp

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);
}
Also used : TException(org.apache.thrift.TException) SQLException(java.sql.SQLException) TFetchResultsResp(org.apache.hive.service.rpc.thrift.TFetchResultsResp) ArrayList(java.util.ArrayList) RowSet(org.apache.hive.service.cli.RowSet) TFetchResultsReq(org.apache.hive.service.rpc.thrift.TFetchResultsReq) SQLFeatureNotSupportedException(java.sql.SQLFeatureNotSupportedException) SQLTimeoutException(java.sql.SQLTimeoutException) SQLException(java.sql.SQLException) TApplicationException(org.apache.thrift.TApplicationException) TException(org.apache.thrift.TException)

Example 3 with TFetchResultsResp

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;
}
Also used : TFetchResultsResp(org.apache.hive.service.rpc.thrift.TFetchResultsResp) RowSet(org.apache.hive.service.cli.RowSet) OperationHandle(org.apache.hive.service.cli.OperationHandle) ServiceException(org.apache.hive.service.ServiceException) TException(org.apache.thrift.TException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) LoginException(javax.security.auth.login.LoginException) HiveSQLException(org.apache.hive.service.cli.HiveSQLException)

Example 4 with TFetchResultsResp

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);
    }
}
Also used : TFetchResultsResp(org.apache.hive.service.rpc.thrift.TFetchResultsResp) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) TFetchResultsReq(org.apache.hive.service.rpc.thrift.TFetchResultsReq) HiveSQLException(org.apache.hive.service.cli.HiveSQLException) TException(org.apache.thrift.TException)

Aggregations

TFetchResultsResp (org.apache.hive.service.rpc.thrift.TFetchResultsResp)4 TFetchResultsReq (org.apache.hive.service.rpc.thrift.TFetchResultsReq)3 TException (org.apache.thrift.TException)3 SQLException (java.sql.SQLException)2 SQLFeatureNotSupportedException (java.sql.SQLFeatureNotSupportedException)2 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)2 RowSet (org.apache.hive.service.cli.RowSet)2 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 SQLTimeoutException (java.sql.SQLTimeoutException)1 ArrayList (java.util.ArrayList)1 LoginException (javax.security.auth.login.LoginException)1 ServiceException (org.apache.hive.service.ServiceException)1 OperationHandle (org.apache.hive.service.cli.OperationHandle)1 TFetchOrientation (org.apache.hive.service.rpc.thrift.TFetchOrientation)1 TRowSet (org.apache.hive.service.rpc.thrift.TRowSet)1 TApplicationException (org.apache.thrift.TApplicationException)1