Search in sources :

Example 11 with ExploreExecutionResult

use of co.cask.cdap.explore.client.ExploreExecutionResult in project cdap by caskdata.

the class HiveExploreStructuredRecordTestRun method testSelectStar.

@Test
public void testSelectStar() throws Exception {
    List<ColumnDesc> expectedSchema = Lists.newArrayList(new ColumnDesc(MY_TABLE_NAME + ".id", "STRING", 1, null), new ColumnDesc(MY_TABLE_NAME + ".subject", "STRING", 2, null), new ColumnDesc(MY_TABLE_NAME + ".body", "STRING", 3, null), new ColumnDesc(MY_TABLE_NAME + ".sender", "STRING", 4, null));
    ExploreExecutionResult results = exploreClient.submit(NAMESPACE_ID, "select * from " + MY_TABLE_NAME).get();
    // check schema
    Assert.assertEquals(expectedSchema, results.getResultSchema());
    List<Object> columns = results.next().getColumns();
    // check results
    Assert.assertEquals("email1", columns.get(0));
    Assert.assertEquals("this is the subject", columns.get(1));
    Assert.assertEquals("this is the body", columns.get(2));
    Assert.assertEquals("sljackson@boss.com", columns.get(3));
    // should not be any more
    Assert.assertFalse(results.hasNext());
}
Also used : ColumnDesc(co.cask.cdap.proto.ColumnDesc) ExploreExecutionResult(co.cask.cdap.explore.client.ExploreExecutionResult) Test(org.junit.Test)

Example 12 with ExploreExecutionResult

use of co.cask.cdap.explore.client.ExploreExecutionResult in project cdap by caskdata.

the class ExecuteQueryCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String query = arguments.get(ArgumentName.QUERY.toString());
    long timeOutMins = arguments.getLongOptional(ArgumentName.TIMEOUT.toString(), DEFAULT_TIMEOUT_MIN);
    ListenableFuture<ExploreExecutionResult> future = queryClient.execute(cliConfig.getCurrentNamespace(), query);
    try {
        ExploreExecutionResult executionResult = future.get(timeOutMins, TimeUnit.MINUTES);
        if (!executionResult.canContainResults()) {
            output.println("SQL statement does not output any result.");
            executionResult.close();
            return;
        }
        final List<ColumnDesc> schema = executionResult.getResultSchema();
        String[] header = new String[schema.size()];
        for (int i = 0; i < header.length; i++) {
            ColumnDesc column = schema.get(i);
            // Hive columns start at 1
            int index = column.getPosition() - 1;
            header[index] = column.getName() + ": " + column.getType();
        }
        List<QueryResult> rows = Lists.newArrayList(executionResult);
        executionResult.close();
        QueryStatus.OpStatus opStatus = executionResult.getStatus().getStatus();
        if (opStatus != QueryStatus.OpStatus.FINISHED) {
            throw new SQLException(String.format("Query '%s' execution did not finish successfully. " + "Got final state - %s", query, opStatus));
        }
        Table table = Table.builder().setHeader(header).setRows(rows, new RowMaker<QueryResult>() {

            @Override
            public List<?> makeRow(QueryResult object) {
                return object.getColumns();
            }
        }).build();
        cliConfig.getTableRenderer().render(cliConfig, output, table);
        output.printf("Fetched %d rows", rows.size()).println();
    } catch (InterruptedException e) {
        Thread.currentThread().interrupt();
    } catch (ExecutionException e) {
        Throwable t = Throwables.getRootCause(e);
        if (t instanceof HandleNotFoundException) {
            throw Throwables.propagate(t);
        }
        throw new SQLException(Throwables.getRootCause(e));
    } catch (CancellationException e) {
        throw new RuntimeException("Query has been cancelled on ListenableFuture object.");
    } catch (TimeoutException e) {
        output.println("Couldn't obtain results after " + timeOutMins + "mins.");
    }
}
Also used : Table(co.cask.cdap.cli.util.table.Table) SQLException(java.sql.SQLException) RowMaker(co.cask.cdap.cli.util.RowMaker) ColumnDesc(co.cask.cdap.proto.ColumnDesc) QueryStatus(co.cask.cdap.proto.QueryStatus) HandleNotFoundException(co.cask.cdap.explore.service.HandleNotFoundException) QueryResult(co.cask.cdap.proto.QueryResult) CancellationException(java.util.concurrent.CancellationException) ExecutionException(java.util.concurrent.ExecutionException) ExploreExecutionResult(co.cask.cdap.explore.client.ExploreExecutionResult) TimeoutException(java.util.concurrent.TimeoutException)

Example 13 with ExploreExecutionResult

use of co.cask.cdap.explore.client.ExploreExecutionResult in project cdap by caskdata.

the class CLIMainTest method assertExploreQuerySuccess.

private static void assertExploreQuerySuccess(ListenableFuture<ExploreExecutionResult> dbCreationFuture) throws Exception {
    ExploreExecutionResult exploreExecutionResult = dbCreationFuture.get(10, TimeUnit.SECONDS);
    QueryStatus status = exploreExecutionResult.getStatus();
    Assert.assertEquals(QueryStatus.OpStatus.FINISHED, status.getStatus());
}
Also used : ExploreExecutionResult(co.cask.cdap.explore.client.ExploreExecutionResult) QueryStatus(co.cask.cdap.proto.QueryStatus)

Example 14 with ExploreExecutionResult

use of co.cask.cdap.explore.client.ExploreExecutionResult in project cdap by caskdata.

the class QueryClientTest method executeBasicQuery.

private void executeBasicQuery(NamespaceId namespace, String instanceName) throws Exception {
    // Hive replaces the periods with underscores
    String query = "select * from dataset_" + instanceName.replace(".", "_");
    ExploreExecutionResult executionResult = queryClient.execute(namespace, query).get();
    Assert.assertNotNull(executionResult.getResultSchema());
    List<QueryResult> results = Lists.newArrayList(executionResult);
    Assert.assertNotNull(results);
    Assert.assertEquals(2, results.size());
    Assert.assertEquals("bob", Bytes.toString((byte[]) results.get(0).getColumns().get(0)));
    Assert.assertEquals("123", Bytes.toString((byte[]) results.get(0).getColumns().get(1)));
    Assert.assertEquals("joe", Bytes.toString((byte[]) results.get(1).getColumns().get(0)));
    Assert.assertEquals("321", Bytes.toString((byte[]) results.get(1).getColumns().get(1)));
}
Also used : QueryResult(co.cask.cdap.proto.QueryResult) ExploreExecutionResult(co.cask.cdap.explore.client.ExploreExecutionResult)

Example 15 with ExploreExecutionResult

use of co.cask.cdap.explore.client.ExploreExecutionResult in project cdap by caskdata.

the class HiveExploreServiceTestRun method testQueriesList.

@Test
public void testQueriesList() throws Exception {
    ListenableFuture<ExploreExecutionResult> future;
    ExploreExecutionResult results;
    List<QueryInfo> queries;
    // Use different namespaces than the other tests so that when its run in a suite there isn't a chance of
    // stray queries polluting this test
    NamespaceId testNamespace1 = new NamespaceId("test1");
    NamespaceId testNamespace2 = new NamespaceId("test2");
    NamespaceMeta testNamespace1Meta = new NamespaceMeta.Builder().setName(testNamespace1).build();
    namespaceAdmin.create(testNamespace1Meta);
    NamespaceMeta testNamespace2Meta = new NamespaceMeta.Builder().setName(testNamespace2).build();
    namespaceAdmin.create(testNamespace2Meta);
    exploreClient.addNamespace(testNamespace1Meta).get();
    exploreClient.addNamespace(testNamespace2Meta).get();
    exploreClient.submit(testNamespace1, "create table my_table (first INT, second STRING)").get();
    future = exploreClient.submit(testNamespace1, "show tables");
    future.get();
    future = exploreClient.submit(testNamespace2, "show tables");
    future.get();
    future = exploreClient.submit(testNamespace1, "select * from my_table");
    results = future.get();
    queries = exploreService.getQueries(testNamespace1);
    Assert.assertEquals(2, queries.size());
    Assert.assertEquals("select * from my_table", queries.get(0).getStatement());
    Assert.assertEquals("FINISHED", queries.get(0).getStatus().toString());
    Assert.assertTrue(queries.get(0).isHasResults());
    Assert.assertTrue(queries.get(0).isActive());
    Assert.assertEquals("show tables", queries.get(1).getStatement());
    Assert.assertEquals("FINISHED", queries.get(1).getStatus().toString());
    Assert.assertTrue(queries.get(1).isHasResults());
    Assert.assertTrue(queries.get(1).isActive());
    // Make the last query inactive
    while (results.hasNext()) {
        results.next();
    }
    queries = exploreService.getQueries(testNamespace1);
    Assert.assertEquals(2, queries.size());
    Assert.assertEquals("select * from my_table", queries.get(0).getStatement());
    Assert.assertEquals("FINISHED", queries.get(0).getStatus().toString());
    Assert.assertTrue(queries.get(0).isHasResults());
    Assert.assertFalse(queries.get(0).isActive());
    Assert.assertEquals("show tables", queries.get(1).getStatement());
    Assert.assertEquals("FINISHED", queries.get(1).getStatus().toString());
    Assert.assertTrue(queries.get(1).isHasResults());
    Assert.assertTrue(queries.get(1).isActive());
    // Close last query
    results.close();
    queries = exploreService.getQueries(testNamespace1);
    Assert.assertEquals(1, queries.size());
    Assert.assertEquals("show tables", queries.get(0).getStatement());
    queries = exploreService.getQueries(testNamespace2);
    Assert.assertEquals(1, queries.size());
    Assert.assertEquals("show tables", queries.get(0).getStatement());
    Assert.assertEquals("FINISHED", queries.get(0).getStatus().toString());
    Assert.assertTrue(queries.get(0).isHasResults());
    Assert.assertTrue(queries.get(0).isActive());
    // Make sure queries are reverse ordered by timestamp
    exploreClient.submit(testNamespace1, "show tables").get();
    exploreClient.submit(testNamespace1, "show tables").get();
    exploreClient.submit(testNamespace1, "show tables").get();
    exploreClient.submit(testNamespace1, "show tables").get();
    queries = exploreService.getQueries(testNamespace1);
    List<Long> timestamps = Lists.newArrayList();
    Assert.assertEquals(5, queries.size());
    for (QueryInfo queryInfo : queries) {
        Assert.assertNotNull(queryInfo.getStatement());
        Assert.assertNotNull(queryInfo.getQueryHandle());
        Assert.assertTrue(queryInfo.isActive());
        Assert.assertEquals("FINISHED", queryInfo.getStatus().toString());
        Assert.assertEquals("show tables", queryInfo.getStatement());
        timestamps.add(queryInfo.getTimestamp());
    }
    // verify the ordering
    Assert.assertTrue(Ordering.natural().reverse().isOrdered(timestamps));
    exploreClient.submit(testNamespace1, "drop table if exists my_table").get();
    exploreClient.removeNamespace(testNamespace1).get();
    exploreClient.removeNamespace(testNamespace2).get();
}
Also used : NamespaceMeta(co.cask.cdap.proto.NamespaceMeta) NamespaceId(co.cask.cdap.proto.id.NamespaceId) QueryInfo(co.cask.cdap.proto.QueryInfo) ExploreExecutionResult(co.cask.cdap.explore.client.ExploreExecutionResult) Test(org.junit.Test)

Aggregations

ExploreExecutionResult (co.cask.cdap.explore.client.ExploreExecutionResult)25 Test (org.junit.Test)16 ColumnDesc (co.cask.cdap.proto.ColumnDesc)13 QueryResult (co.cask.cdap.proto.QueryResult)9 DatasetId (co.cask.cdap.proto.id.DatasetId)7 Transaction (org.apache.tephra.Transaction)5 Schema (co.cask.cdap.api.data.schema.Schema)4 Table (co.cask.cdap.api.dataset.table.Table)3 NamespaceId (co.cask.cdap.proto.id.NamespaceId)3 StreamId (co.cask.cdap.proto.id.StreamId)3 FormatSpecification (co.cask.cdap.api.data.format.FormatSpecification)2 KeyExtendedStructValueTableDefinition (co.cask.cdap.explore.service.datasets.KeyExtendedStructValueTableDefinition)2 KeyStructValueTableDefinition (co.cask.cdap.explore.service.datasets.KeyStructValueTableDefinition)2 WritableKeyStructValueTableDefinition (co.cask.cdap.explore.service.datasets.WritableKeyStructValueTableDefinition)2 NamespaceMeta (co.cask.cdap.proto.NamespaceMeta)2 QueryStatus (co.cask.cdap.proto.QueryStatus)2 StreamProperties (co.cask.cdap.proto.StreamProperties)2 Put (co.cask.cdap.api.dataset.table.Put)1 RowMaker (co.cask.cdap.cli.util.RowMaker)1 Table (co.cask.cdap.cli.util.table.Table)1