Search in sources :

Example 11 with ExploreExecutionResult

use of io.cdap.cdap.explore.client.ExploreExecutionResult in project cdap by caskdata.

the class HiveExploreTableTestRun method testTableWithDateTimestamp.

@Test
public void testTableWithDateTimestamp() throws Exception {
    TimeZone.setDefault(TimeZone.getTimeZone("UTC"));
    DatasetId dtTsTable = NAMESPACE_ID.dataset("dt_ts_table");
    DatasetId otherDtTsTable = NAMESPACE_ID.dataset("other_dt_ts_table");
    Schema schema = Schema.recordOf("recordWithDateTimestamp", Schema.Field.of("int_field", Schema.of(Schema.Type.INT)), Schema.Field.of("string_field", Schema.nullableOf(Schema.of(Schema.Type.STRING))), Schema.Field.of("date_field", Schema.nullableOf(Schema.of(Schema.LogicalType.DATE))), Schema.Field.of("ts_millis_field", Schema.nullableOf(Schema.of(Schema.LogicalType.TIMESTAMP_MILLIS))), Schema.Field.of("ts_micros_field", Schema.nullableOf(Schema.of(Schema.LogicalType.TIMESTAMP_MICROS))));
    datasetFramework.addInstance(Table.class.getName(), dtTsTable, TableProperties.builder().setSchema(schema).setRowFieldName("int_field").setExploreTableName("dt_ts_table").build());
    datasetFramework.addInstance(Table.class.getName(), otherDtTsTable, TableProperties.builder().setSchema(schema).setRowFieldName("int_field").setExploreTableName("other_dt_ts_table").build());
    try {
        // Accessing dataset instance to perform data operations
        Table table = datasetFramework.getDataset(dtTsTable, DatasetDefinition.NO_ARGUMENTS, null);
        Assert.assertNotNull(table);
        Transaction tx = transactionManager.startShort(100);
        ((TransactionAware) table).startTx(tx);
        Put put = new Put(Bytes.toBytes("row1"));
        put.add("int_field", 1);
        put.add("string_field", "alice");
        put.add("date_field", 0);
        put.add("ts_millis_field", 1536336590595L);
        put.add("ts_micros_field", 1536336590595123L);
        table.put(put);
        put = new Put(Bytes.toBytes("row2"));
        put.add("int_field", 2);
        put.add("string_field", "bob");
        table.put(put);
        ((TransactionAware) table).commitTx();
        transactionManager.canCommit(tx.getTransactionId(), ((TransactionAware) table).getTxChanges());
        transactionManager.commit(tx.getTransactionId(), tx.getWritePointer());
        ((TransactionAware) table).postTxCommit();
        ExploreExecutionResult results = exploreClient.submit(NAMESPACE_ID, "select * from dt_ts_table").get();
        List<Object> columns = results.next().getColumns();
        Assert.assertEquals(5, columns.size());
        Assert.assertEquals("alice", columns.get(1));
        Assert.assertEquals("1970-01-01", columns.get(2));
        Assert.assertEquals("2018-09-07 16:09:50.595", columns.get(3));
        Assert.assertEquals("2018-09-07 16:09:50.595123", columns.get(4));
        columns = results.next().getColumns();
        Assert.assertEquals(5, columns.size());
        Assert.assertEquals("bob", columns.get(1));
        Assert.assertNull(columns.get(2));
        Assert.assertNull(columns.get(3));
        Assert.assertNull(columns.get(4));
        String command = "insert into other_dt_ts_table select int_field, string_field, date_field, ts_millis_field, " + "ts_micros_field from dt_ts_table";
        ExploreExecutionResult result = exploreClient.submit(NAMESPACE_ID, command).get();
        Assert.assertEquals(QueryStatus.OpStatus.FINISHED, result.getStatus().getStatus());
        command = "select string_field, date_field, ts_millis_field, ts_micros_field from other_dt_ts_table";
        runCommand(NAMESPACE_ID, command, true, Lists.newArrayList(new ColumnDesc("string_field", "STRING", 1, null), new ColumnDesc("date_field", "DATE", 2, null), new ColumnDesc("ts_millis_field", "TIMESTAMP", 3, null), new ColumnDesc("ts_micros_field", "TIMESTAMP", 4, null)), Lists.newArrayList(new QueryResult(Lists.newArrayList("alice", "1970-01-01", "2018-09-07 16:09:50.595", "2018-09-07 16:09:50.595123")), new QueryResult(Lists.newArrayList("bob", null, null, null))));
    } finally {
        datasetFramework.deleteInstance(dtTsTable);
        datasetFramework.deleteInstance(otherDtTsTable);
    }
}
Also used : QueryResult(io.cdap.cdap.proto.QueryResult) Table(io.cdap.cdap.api.dataset.table.Table) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) Schema(io.cdap.cdap.api.data.schema.Schema) ColumnDesc(io.cdap.cdap.proto.ColumnDesc) ExploreExecutionResult(io.cdap.cdap.explore.client.ExploreExecutionResult) Put(io.cdap.cdap.api.dataset.table.Put) DatasetId(io.cdap.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 12 with ExploreExecutionResult

use of io.cdap.cdap.explore.client.ExploreExecutionResult in project cdap by caskdata.

the class CLITestBase method assertExploreQuerySuccess.

private 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(io.cdap.cdap.explore.client.ExploreExecutionResult) QueryStatus(io.cdap.cdap.proto.QueryStatus)

Example 13 with ExploreExecutionResult

use of io.cdap.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(io.cdap.cdap.proto.QueryResult) ExploreExecutionResult(io.cdap.cdap.explore.client.ExploreExecutionResult)

Example 14 with ExploreExecutionResult

use of io.cdap.cdap.explore.client.ExploreExecutionResult in project cdap by cdapio.

the class GenerateClientUsageExample method queryClient.

public void queryClient() throws Exception {
    // Construct the client used to interact with CDAP
    QueryClient queryClient = new QueryClient(clientConfig);
    // Perform an ad-hoc query using the Purchase example
    ListenableFuture<ExploreExecutionResult> resultFuture = queryClient.execute(NamespaceId.DEFAULT, "SELECT * FROM dataset_history WHERE customer IN ('Alice','Bob')");
    ExploreExecutionResult results = resultFuture.get();
    // Fetch schema
    List<ColumnDesc> schema = results.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();
    }
}
Also used : ColumnDesc(io.cdap.cdap.proto.ColumnDesc) ExploreExecutionResult(io.cdap.cdap.explore.client.ExploreExecutionResult) QueryClient(io.cdap.cdap.client.QueryClient)

Example 15 with ExploreExecutionResult

use of io.cdap.cdap.explore.client.ExploreExecutionResult in project cdap by cdapio.

the class CLITestBase method assertExploreQuerySuccess.

private 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(io.cdap.cdap.explore.client.ExploreExecutionResult) QueryStatus(io.cdap.cdap.proto.QueryStatus)

Aggregations

ExploreExecutionResult (io.cdap.cdap.explore.client.ExploreExecutionResult)52 Test (org.junit.Test)36 ColumnDesc (io.cdap.cdap.proto.ColumnDesc)24 DatasetId (io.cdap.cdap.proto.id.DatasetId)20 QueryResult (io.cdap.cdap.proto.QueryResult)18 Transaction (org.apache.tephra.Transaction)14 Table (io.cdap.cdap.api.dataset.table.Table)10 Schema (io.cdap.cdap.api.data.schema.Schema)6 Put (io.cdap.cdap.api.dataset.table.Put)6 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)6 TransactionAware (org.apache.tephra.TransactionAware)6 ObjectMappedTable (io.cdap.cdap.api.dataset.lib.ObjectMappedTable)4 QueryClient (io.cdap.cdap.client.QueryClient)4 KeyExtendedStructValueTableDefinition (io.cdap.cdap.explore.service.datasets.KeyExtendedStructValueTableDefinition)4 KeyStructValueTableDefinition (io.cdap.cdap.explore.service.datasets.KeyStructValueTableDefinition)4 WritableKeyStructValueTableDefinition (io.cdap.cdap.explore.service.datasets.WritableKeyStructValueTableDefinition)4 NamespaceMeta (io.cdap.cdap.proto.NamespaceMeta)4 QueryStatus (io.cdap.cdap.proto.QueryStatus)4 TxRunnable (io.cdap.cdap.api.TxRunnable)2 DatasetContext (io.cdap.cdap.api.data.DatasetContext)2