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);
}
}
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());
}
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)));
}
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();
}
}
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());
}
Aggregations