use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.
the class ConnectionImpl method processQueryResponseResults.
@VisibleForTesting
BigQueryResult processQueryResponseResults(com.google.api.services.bigquery.model.QueryResponse results) {
Schema schema;
long numRows;
schema = Schema.fromPb(results.getSchema());
numRows = results.getTotalRows() == null ? 0 : // in case of DML or DDL
results.getTotalRows().longValue();
// QueryResponse only provides cache hits, dmlStats, and sessionInfo as query processing
// statistics
DmlStats dmlStats = results.getDmlStats() == null ? null : DmlStats.fromPb(results.getDmlStats());
Boolean cacheHit = results.getCacheHit();
QueryStatistics queryStatistics = QueryStatistics.newBuilder().setDmlStats(dmlStats).setCacheHit(cacheHit).build();
// We cannot directly set sessionInfo in QueryStatistics
SessionInfo sessionInfo = results.getSessionInfo() == null ? null : JobStatistics.SessionInfo.fromPb(results.getSessionInfo());
BigQueryResultStats bigQueryResultStats = new BigQueryResultStatsImpl(queryStatistics, sessionInfo);
bufferFvl = new LinkedBlockingDeque<>(getBufferSize());
BlockingQueue<Tuple<Iterable<FieldValueList>, Boolean>> pageCache = new LinkedBlockingDeque<>(getPageCacheSize(connectionSettings.getNumBufferedRows(), schema));
BlockingQueue<Tuple<TableDataList, Boolean>> rpcResponseQueue = new LinkedBlockingDeque<>(getPageCacheSize(connectionSettings.getNumBufferedRows(), schema));
JobId jobId = JobId.fromPb(results.getJobReference());
// Thread to make rpc calls to fetch data from the server
runNextPageTaskAsync(results.getPageToken(), getDestinationTable(jobId), rpcResponseQueue);
// Thread to parse data received from the server to client library objects
parseRpcDataAsync(results.getRows(), schema, pageCache, rpcResponseQueue);
// Thread to populate the buffer (a blocking queue) shared with the consumer
populateBufferAsync(rpcResponseQueue, pageCache, bufferFvl);
return new BigQueryResultImpl<AbstractList<FieldValue>>(schema, numRows, bufferFvl, bigQueryResultStats);
}
use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.
the class ITBigQueryTest method testConnectionImplDryRun.
@Test
public void testConnectionImplDryRun() throws SQLException {
String query = String.format("select StringField, BigNumericField, BooleanField, BytesField, IntegerField, TimestampField, FloatField, NumericField, TimeField, DateField, DateTimeField , GeographyField, RecordField.BytesField, RecordField.BooleanField, IntegerArrayField from %s where StringField = ? order by TimestampField", TABLE_ID_FASTQUERY_BQ_RESULTSET.getTable());
ConnectionSettings connectionSettings = ConnectionSettings.newBuilder().setDefaultDataset(DatasetId.of(DATASET)).setCreateSession(true).build();
Connection connection = bigquery.createConnection(connectionSettings);
BigQueryDryRunResult bigQueryDryRunResultSet = connection.dryRun(query);
assertNotNull(bigQueryDryRunResultSet.getSchema());
assertEquals(BQ_RESULTSET_EXPECTED_SCHEMA, // match the schema
bigQueryDryRunResultSet.getSchema());
List<Parameter> queryParameters = bigQueryDryRunResultSet.getQueryParameters();
assertEquals(StandardSQLTypeName.STRING, queryParameters.get(0).getValue().getType());
QueryStatistics queryStatistics = bigQueryDryRunResultSet.getStatistics().getQueryStatistics();
assertNotNull(queryStatistics);
SessionInfo sessionInfo = bigQueryDryRunResultSet.getStatistics().getSessionInfo();
assertNotNull(sessionInfo.getSessionId());
assertEquals(StatementType.SELECT, queryStatistics.getStatementType());
}
use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.
the class JobTest method testWaitForAndGetQueryResultsEmpty.
@Test
public void testWaitForAndGetQueryResultsEmpty() throws InterruptedException {
QueryJobConfiguration jobConfig = QueryJobConfiguration.newBuilder("CREATE VIEW").setDestinationTable(TABLE_ID1).build();
QueryStatistics jobStatistics = QueryStatistics.newBuilder().setCreationTimestamp(1L).setEndTime(3L).setStartTime(2L).build();
JobInfo jobInfo = JobInfo.newBuilder(jobConfig).setJobId(JOB_ID).setStatistics(jobStatistics).setJobId(JOB_ID).setEtag(ETAG).setGeneratedId(GENERATED_ID).setSelfLink(SELF_LINK).setUserEmail(EMAIL).setStatus(JOB_STATUS).build();
when(mockOptions.getClock()).thenReturn(CurrentMillisClock.getDefaultClock());
Job completedJob = expectedJob.toBuilder().setStatus(new JobStatus(JobStatus.State.RUNNING)).build();
QueryResponse completedQuery = QueryResponse.newBuilder().setCompleted(true).setTotalRows(0).setSchema(Schema.of()).setErrors(ImmutableList.<BigQueryError>of()).build();
when(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS)).thenReturn(completedQuery);
when(bigquery.getJob(JOB_INFO.getJobId())).thenReturn(completedJob);
when(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS)).thenReturn(completedQuery);
when(bigquery.getJob(JOB_INFO.getJobId())).thenReturn(completedJob);
job = this.job.toBuilder().setConfiguration(jobConfig).build();
assertThat(job.waitFor(TEST_RETRY_OPTIONS)).isSameInstanceAs(completedJob);
assertThat(job.getQueryResults().iterateAll()).isEmpty();
verify(bigquery, times(2)).getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS);
verify(bigquery).getJob(JOB_INFO.getJobId());
}
use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.
the class JobTest method testWaitForAndGetQueryResultsEmptyWithSchema.
@Test
public void testWaitForAndGetQueryResultsEmptyWithSchema() throws InterruptedException {
QueryJobConfiguration jobConfig = QueryJobConfiguration.newBuilder("CREATE VIEW").setDestinationTable(TABLE_ID1).build();
QueryStatistics jobStatistics = QueryStatistics.newBuilder().setCreationTimestamp(1L).setEndTime(3L).setStartTime(2L).build();
JobInfo jobInfo = JobInfo.newBuilder(jobConfig).setJobId(JOB_ID).setStatistics(jobStatistics).setJobId(JOB_ID).setEtag(ETAG).setGeneratedId(GENERATED_ID).setSelfLink(SELF_LINK).setUserEmail(EMAIL).setStatus(JOB_STATUS).build();
JobStatus status = mock(JobStatus.class);
when(bigquery.getOptions()).thenReturn(mockOptions);
when(mockOptions.getClock()).thenReturn(CurrentMillisClock.getDefaultClock());
Job completedJob = expectedJob.toBuilder().setStatus(new JobStatus(JobStatus.State.RUNNING)).build();
QueryResponse completedQuery = QueryResponse.newBuilder().setCompleted(true).setTotalRows(0).setSchema(Schema.of(Field.of("field1", LegacySQLTypeName.BOOLEAN))).setErrors(ImmutableList.<BigQueryError>of()).build();
when(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS)).thenReturn(completedQuery);
when(bigquery.getJob(JOB_INFO.getJobId())).thenReturn(completedJob);
when(bigquery.getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS)).thenReturn(completedQuery);
job = this.job.toBuilder().setConfiguration(jobConfig).build();
assertThat(job.waitFor(TEST_RETRY_OPTIONS)).isSameInstanceAs(completedJob);
assertThat(job.getQueryResults().getSchema()).isEqualTo(Schema.of(Field.of("field1", LegacySQLTypeName.BOOLEAN)));
verify(bigquery, times(2)).getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS);
verify(bigquery).getJob(JOB_INFO.getJobId());
}
use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.
the class ITBigQueryTest method testQuerySessionSupport.
@Test
public void testQuerySessionSupport() throws InterruptedException {
String query = "CREATE TEMPORARY TABLE temptable AS SELECT 17 as foo";
QueryJobConfiguration queryJobConfiguration = QueryJobConfiguration.newBuilder(query).setDefaultDataset(DatasetId.of(DATASET)).setCreateSession(true).build();
Job remoteJob = bigquery.create(JobInfo.of(queryJobConfiguration));
remoteJob = remoteJob.waitFor();
assertNull(remoteJob.getStatus().getError());
Job queryJob = bigquery.getJob(remoteJob.getJobId());
JobStatistics.QueryStatistics statistics = queryJob.getStatistics();
String sessionId = statistics.getSessionInfo().getSessionId();
assertNotNull(sessionId);
String queryTempTable = "SELECT * FROM temptable";
ConnectionProperty connectionProperty = ConnectionProperty.newBuilder().setKey("session_id").setValue(sessionId).build();
QueryJobConfiguration queryJobConfigurationWithSession = QueryJobConfiguration.newBuilder(queryTempTable).setDefaultDataset(DatasetId.of(DATASET)).setConnectionProperties(ImmutableList.of(connectionProperty)).build();
Job remoteJobWithSession = bigquery.create(JobInfo.of(queryJobConfigurationWithSession));
remoteJobWithSession = remoteJobWithSession.waitFor();
assertNull(remoteJobWithSession.getStatus().getError());
Job queryJobWithSession = bigquery.getJob(remoteJobWithSession.getJobId());
QueryStatistics statisticsWithSession = queryJobWithSession.getStatistics();
assertEquals(sessionId, statisticsWithSession.getSessionInfo().getSessionId());
}
Aggregations