Search in sources :

Example 16 with QueryHandle

use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.

the class HiveExploreServiceTestRun method previewResultsTest.

@Test
public void previewResultsTest() throws Exception {
    DatasetId myTable2 = NAMESPACE_ID.dataset("my_table_2");
    DatasetId myTable3 = NAMESPACE_ID.dataset("my_table_3");
    DatasetId myTable4 = NAMESPACE_ID.dataset("my_table_4");
    DatasetId myTable5 = NAMESPACE_ID.dataset("my_table_5");
    DatasetId myTable6 = NAMESPACE_ID.dataset("my_table_6");
    datasetFramework.addInstance("keyStructValueTable", myTable2, DatasetProperties.EMPTY);
    datasetFramework.addInstance("keyStructValueTable", myTable3, DatasetProperties.EMPTY);
    datasetFramework.addInstance("keyStructValueTable", myTable4, DatasetProperties.EMPTY);
    datasetFramework.addInstance("keyStructValueTable", myTable5, DatasetProperties.EMPTY);
    datasetFramework.addInstance("keyStructValueTable", myTable6, DatasetProperties.EMPTY);
    try {
        QueryHandle handle = exploreService.execute(NAMESPACE_ID, "show tables");
        QueryStatus status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 50);
        Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
        List<QueryResult> firstPreview = exploreService.previewResults(handle);
        Assert.assertEquals(ImmutableList.of(new QueryResult(ImmutableList.<Object>of(MY_TABLE_NAME)), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable2))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable3))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable4))), new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable5)))), firstPreview);
        // test that preview results do not change even when the query cursor is updated by nextResults
        List<QueryResult> endResults = exploreService.nextResults(handle, 100);
        Assert.assertEquals(ImmutableList.of(new QueryResult(ImmutableList.<Object>of(getDatasetHiveName(myTable6)))), endResults);
        List<QueryResult> secondPreview = exploreService.previewResults(handle);
        Assert.assertEquals(firstPreview, secondPreview);
        Assert.assertEquals(ImmutableList.<QueryResult>of(), exploreService.nextResults(handle, 100));
        try {
            // All results are fetched, query should be inactive now
            exploreService.previewResults(handle);
            Assert.fail("HandleNotFoundException expected - query should be inactive.");
        } catch (HandleNotFoundException e) {
            Assert.assertTrue(e.isInactive());
        // Expected exception
        }
        // now test preview on a query that doesn't return any results
        handle = exploreService.execute(NAMESPACE_ID, "select * from " + getDatasetHiveName(myTable2));
        status = waitForCompletionStatus(handle, 200, TimeUnit.MILLISECONDS, 50);
        Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
        Assert.assertTrue(exploreService.previewResults(handle).isEmpty());
        // calling preview again should return the same thing. it should not throw an exception
        Assert.assertTrue(exploreService.previewResults(handle).isEmpty());
    } finally {
        datasetFramework.deleteInstance(myTable2);
        datasetFramework.deleteInstance(myTable3);
        datasetFramework.deleteInstance(myTable4);
        datasetFramework.deleteInstance(myTable5);
        datasetFramework.deleteInstance(myTable6);
    }
}
Also used : QueryResult(co.cask.cdap.proto.QueryResult) QueryHandle(co.cask.cdap.proto.QueryHandle) QueryStatus(co.cask.cdap.proto.QueryStatus) DatasetId(co.cask.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 17 with QueryHandle

use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.

the class InMemoryExploreServiceTest method runCommand.

private static void runCommand(String namespace, String command, boolean expectedHasResult, List<ColumnDesc> expectedColumnDescs, List<QueryResult> expectedResults) throws Exception {
    QueryHandle handle = exploreService.execute(new NamespaceId(namespace), command);
    QueryStatus status = waitForCompletionStatus(handle);
    Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
    Assert.assertEquals(expectedHasResult, status.hasResults());
    Assert.assertEquals(expectedColumnDescs, exploreService.getResultSchema(handle));
    Assert.assertEquals(expectedResults.toString(), trimColumnValues(exploreService.nextResults(handle, 100)).toString());
    exploreService.close(handle);
}
Also used : NamespaceId(co.cask.cdap.proto.id.NamespaceId) QueryHandle(co.cask.cdap.proto.QueryHandle) QueryStatus(co.cask.cdap.proto.QueryStatus)

Example 18 with QueryHandle

use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.

the class HiveExploreServiceTimeoutTest method testTimeoutRunning.

@Test
public void testTimeoutRunning() throws Exception {
    Set<Long> beforeTxns = transactionManager.getCurrentState().getInProgress().keySet();
    QueryHandle handle = exploreService.execute(NAMESPACE_ID, "select key, value from " + MY_TABLE_NAME);
    Set<Long> queryTxns = Sets.difference(transactionManager.getCurrentState().getInProgress().keySet(), beforeTxns);
    Assert.assertFalse(queryTxns.isEmpty());
    // Sleep for timeout to happen
    TimeUnit.SECONDS.sleep(ACTIVE_OPERATION_TIMEOUT_SECS + 3);
    try {
        exploreService.getStatus(handle);
        Assert.fail("Should throw HandleNotFoundException due to operation timeout");
    } catch (HandleNotFoundException e) {
    // Expected exception due to timeout
    }
    // Make sure that the transaction got closed
    Assert.assertEquals(ImmutableSet.<Long>of(), Sets.intersection(queryTxns, transactionManager.getCurrentState().getInProgress().keySet()).immutableCopy());
}
Also used : QueryHandle(co.cask.cdap.proto.QueryHandle) Test(org.junit.Test)

Example 19 with QueryHandle

use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.

the class AbstractExploreClient method getFutureResultsFromHandle.

/**
   * Create a {@link ListenableFuture} object by polling the Explore service using the
   * {@link ListenableFuture} containing a {@link QueryHandle}.
   */
private ListenableFuture<ExploreExecutionResult> getFutureResultsFromHandle(final ListenableFuture<QueryHandle> futureHandle) {
    final StatementExecutionFuture resultFuture = new StatementExecutionFuture(this, futureHandle);
    Futures.addCallback(futureHandle, new FutureCallback<QueryHandle>() {

        @Override
        public void onSuccess(final QueryHandle handle) {
            boolean mustCloseHandle;
            try {
                QueryStatus status = getStatus(handle);
                if (!status.getStatus().isDone()) {
                    executor.schedule(new Runnable() {

                        @Override
                        public void run() {
                            onSuccess(handle);
                        }
                    }, 300, TimeUnit.MILLISECONDS);
                    return;
                }
                if (QueryStatus.OpStatus.ERROR.equals(status.getStatus())) {
                    throw new SQLException(status.getErrorMessage(), status.getSqlState());
                }
                ExploreExecutionResult result = new ClientExploreExecutionResult(AbstractExploreClient.this, handle, status);
                mustCloseHandle = !resultFuture.set(result) || !status.hasResults();
            } catch (Exception e) {
                mustCloseHandle = true;
                resultFuture.setException(e);
            }
            if (mustCloseHandle) {
                try {
                    close(handle);
                } catch (Throwable t) {
                    LOG.warn("Failed to close handle {}", handle, t);
                }
            }
        }

        @Override
        public void onFailure(Throwable t) {
            resultFuture.setException(t);
        }
    }, executor);
    return resultFuture;
}
Also used : SQLException(java.sql.SQLException) QueryHandle(co.cask.cdap.proto.QueryHandle) QueryStatus(co.cask.cdap.proto.QueryStatus) HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) SQLException(java.sql.SQLException) ServiceUnavailableException(co.cask.cdap.common.ServiceUnavailableException) IOException(java.io.IOException) UnauthenticatedException(co.cask.cdap.common.UnauthenticatedException) ExploreException(co.cask.cdap.explore.service.ExploreException)

Example 20 with QueryHandle

use of co.cask.cdap.proto.QueryHandle in project cdap by caskdata.

the class BaseHiveExploreService method createNamespace.

public QueryHandle createNamespace(NamespaceMeta namespaceMeta) throws ExploreException, SQLException {
    startAndWait();
    try {
        // This check prevents the extra warn log.
        if (NamespaceId.DEFAULT.equals(namespaceMeta.getNamespaceId())) {
            return QueryHandle.NO_OP;
        }
        Map<String, String> sessionConf = startSession();
        SessionHandle sessionHandle = null;
        OperationHandle operationHandle = null;
        try {
            sessionHandle = openHiveSession(sessionConf);
            QueryHandle handle;
            if (Strings.isNullOrEmpty(namespaceMeta.getConfig().getHiveDatabase())) {
                // if no custom hive database was provided get the hive database according to cdap format and create it
                // if one does not exists since cdap is responsible for managing the lifecycle of such databases
                String database = createHiveDBName(namespaceMeta.getName());
                // "IF NOT EXISTS" so that this operation is idempotent.
                String statement = String.format("CREATE DATABASE IF NOT EXISTS %s", database);
                operationHandle = executeAsync(sessionHandle, statement);
                handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, statement, database);
                LOG.info("Creating database {} with handle {}", database, handle);
            } else {
                // a custom database name was provided so check its existence
                // there is no way to check if a hive database exists or not other than trying to use it and see whether
                // it fails or not. So, run a USE databaseName command and see if it throws exception
                // Other way can be to list all database and check if the database exists or not but we are doing USE to
                // make sure that user can acutally use the database once we have impersonation.
                String statement = String.format("USE %s", namespaceMeta.getConfig().getHiveDatabase());
                // if the database does not exists the below line will throw exception from hive
                try {
                    operationHandle = executeAsync(sessionHandle, statement);
                } catch (HiveSQLException e) {
                    // then we will get an exception from Hive with error code 10072 which represent database was not found
                    if (e.toTStatus().getErrorCode() == ErrorMsg.DATABASE_NOT_EXISTS.getErrorCode()) {
                        //TODO: Add username here
                        throw new ExploreException(String.format("A custom Hive Database %s was provided for namespace %s " + "which does not exists. Please create the database in hive " + "for the user and try creating the namespace again.", namespaceMeta.getConfig().getHiveDatabase(), namespaceMeta.getName()), e);
                    } else {
                        // some other exception was generated while checking the existense of the database
                        throw new ExploreException(String.format("Failed to check existence of given custom hive database " + "%s for namespace %s", namespaceMeta.getConfig().getHiveDatabase(), namespaceMeta.getName()), e);
                    }
                }
                // if we didn't got an exception on the line above we know that the database exists
                handle = saveReadOnlyOperation(operationHandle, sessionHandle, sessionConf, statement, namespaceMeta.getConfig().getHiveDatabase());
                LOG.debug("Custom database {} existence verified with handle {}", namespaceMeta.getConfig().getHiveDatabase(), handle);
            }
            return handle;
        } catch (Throwable e) {
            closeInternal(getQueryHandle(sessionConf), new ReadOnlyOperationInfo(sessionHandle, operationHandle, sessionConf, "", ""));
            throw e;
        }
    } catch (HiveSQLException e) {
        throw getSqlException(e);
    } catch (Throwable e) {
        throw new ExploreException(e);
    }
}
Also used : HiveSQLException(org.apache.hive.service.cli.HiveSQLException) SessionHandle(org.apache.hive.service.cli.SessionHandle) QueryHandle(co.cask.cdap.proto.QueryHandle) OperationHandle(org.apache.hive.service.cli.OperationHandle) ExploreException(co.cask.cdap.explore.service.ExploreException)

Aggregations

QueryHandle (co.cask.cdap.proto.QueryHandle)36 ExploreException (co.cask.cdap.explore.service.ExploreException)23 SQLException (java.sql.SQLException)16 HiveSQLException (org.apache.hive.service.cli.HiveSQLException)11 OperationHandle (org.apache.hive.service.cli.OperationHandle)11 SessionHandle (org.apache.hive.service.cli.SessionHandle)11 IOException (java.io.IOException)10 Path (javax.ws.rs.Path)10 HandleNotFoundException (co.cask.cdap.explore.service.HandleNotFoundException)9 QueryStatus (co.cask.cdap.proto.QueryStatus)8 UnsupportedTypeException (co.cask.cdap.api.data.schema.UnsupportedTypeException)7 JsonObject (com.google.gson.JsonObject)7 POST (javax.ws.rs.POST)7 DatasetManagementException (co.cask.cdap.api.dataset.DatasetManagementException)6 BadRequestException (co.cask.cdap.common.BadRequestException)6 JsonSyntaxException (com.google.gson.JsonSyntaxException)6 Callable (java.util.concurrent.Callable)5 Test (org.junit.Test)5 AuditPolicy (co.cask.cdap.common.security.AuditPolicy)3 ColumnDesc (co.cask.cdap.proto.ColumnDesc)3