Search in sources :

Example 6 with QueryStatistics

use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.

the class JobTest method testWaitForAndGetQueryResults.

@Test
public void testWaitForAndGetQueryResults() throws InterruptedException {
    QueryJobConfiguration jobConfig = QueryJobConfiguration.newBuilder("SELECT 1").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(bigquery.getOptions()).thenReturn(mockOptions);
    when(mockOptions.getClock()).thenReturn(CurrentMillisClock.getDefaultClock());
    Job completedJob = expectedJob.toBuilder().setStatus(new JobStatus(JobStatus.State.RUNNING)).build();
    Page<FieldValueList> singlePage = Pages.empty();
    TableResult result = new TableResult(Schema.of(), 1, singlePage);
    QueryResponse completedQuery = QueryResponse.newBuilder().setCompleted(true).setTotalRows(// Lies to force call of listTableData().
    1).setSchema(Schema.of(Field.of("_f0", LegacySQLTypeName.INTEGER))).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.listTableData(eq(TABLE_ID1), any(Schema.class))).thenReturn(result);
    job = this.job.toBuilder().setConfiguration(jobConfig).build();
    assertThat(job.waitFor(TEST_RETRY_OPTIONS)).isSameInstanceAs(completedJob);
    assertThat(job.getQueryResults().iterateAll()).hasSize(0);
    verify(bigquery, times(2)).getQueryResults(jobInfo.getJobId(), Job.DEFAULT_QUERY_WAIT_OPTIONS);
    verify(bigquery).getJob(JOB_INFO.getJobId());
}
Also used : QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) Test(org.junit.Test)

Example 7 with QueryStatistics

use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.

the class ConnectionImpl method dryRun.

/**
 * This method runs a dry run query
 *
 * @param sql SQL SELECT statement
 * @return BigQueryDryRunResult containing List<Parameter> and Schema
 * @throws BigQuerySQLException
 */
@BetaApi
@Override
public BigQueryDryRunResult dryRun(String sql) throws BigQuerySQLException {
    com.google.api.services.bigquery.model.Job dryRunJob = createDryRunJob(sql);
    Schema schema = Schema.fromPb(dryRunJob.getStatistics().getQuery().getSchema());
    List<QueryParameter> queryParametersPb = dryRunJob.getStatistics().getQuery().getUndeclaredQueryParameters();
    List<Parameter> queryParameters = Lists.transform(queryParametersPb, QUERY_PARAMETER_FROM_PB_FUNCTION);
    QueryStatistics queryStatistics = JobStatistics.fromPb(dryRunJob);
    SessionInfo sessionInfo = queryStatistics.getSessionInfo() == null ? null : queryStatistics.getSessionInfo();
    BigQueryResultStats bigQueryResultStats = new BigQueryResultStatsImpl(queryStatistics, sessionInfo);
    return new BigQueryDryRunResultImpl(schema, queryParameters, bigQueryResultStats);
}
Also used : QueryParameter(com.google.api.services.bigquery.model.QueryParameter) ArrowSchema(com.google.cloud.bigquery.storage.v1.ArrowSchema) SessionInfo(com.google.cloud.bigquery.JobStatistics.SessionInfo) QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) QueryParameter(com.google.api.services.bigquery.model.QueryParameter) BetaApi(com.google.api.core.BetaApi)

Example 8 with QueryStatistics

use of com.google.cloud.bigquery.JobStatistics.QueryStatistics in project java-bigquery by googleapis.

the class ConnectionImpl method getBigQueryResultSetStats.

@VisibleForTesting
BigQueryResultStats getBigQueryResultSetStats(JobId jobId) {
    // Create GetQueryResultsResponse query statistics
    Job queryJob = getQueryJobRpc(jobId);
    QueryStatistics queryStatistics = queryJob.getStatistics();
    SessionInfo sessionInfo = queryStatistics.getSessionInfo() == null ? null : queryStatistics.getSessionInfo();
    return new BigQueryResultStatsImpl(queryStatistics, sessionInfo);
}
Also used : QueryStatistics(com.google.cloud.bigquery.JobStatistics.QueryStatistics) SessionInfo(com.google.cloud.bigquery.JobStatistics.SessionInfo) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Aggregations

QueryStatistics (com.google.cloud.bigquery.JobStatistics.QueryStatistics)8 Test (org.junit.Test)5 SessionInfo (com.google.cloud.bigquery.JobStatistics.SessionInfo)4 ArrowSchema (com.google.cloud.bigquery.storage.v1.ArrowSchema)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 BetaApi (com.google.api.core.BetaApi)1 QueryParameter (com.google.api.services.bigquery.model.QueryParameter)1 Tuple (com.google.cloud.Tuple)1 BigQueryDryRunResult (com.google.cloud.bigquery.BigQueryDryRunResult)1 Connection (com.google.cloud.bigquery.Connection)1 ConnectionProperty (com.google.cloud.bigquery.ConnectionProperty)1 ConnectionSettings (com.google.cloud.bigquery.ConnectionSettings)1 Job (com.google.cloud.bigquery.Job)1 JobStatistics (com.google.cloud.bigquery.JobStatistics)1 Parameter (com.google.cloud.bigquery.Parameter)1 QueryJobConfiguration (com.google.cloud.bigquery.QueryJobConfiguration)1 LinkedBlockingDeque (java.util.concurrent.LinkedBlockingDeque)1