Search in sources :

Example 6 with QueryRequest

use of com.google.api.services.bigquery.model.QueryRequest in project beam by apache.

the class BigqueryMatcher method matchesSafely.

@Override
protected boolean matchesSafely(PipelineResult pipelineResult) {
    LOG.info("Verifying Bigquery data");
    Bigquery bigqueryClient = newBigqueryClient(applicationName);
    // execute query
    LOG.debug("Executing query: {}", query);
    try {
        QueryRequest queryContent = new QueryRequest();
        queryContent.setQuery(query);
        response = queryWithRetries(bigqueryClient, queryContent, Sleeper.DEFAULT, BackOffAdapter.toGcpBackOff(BACKOFF_FACTORY.backoff()));
    } catch (IOException | InterruptedException e) {
        if (e instanceof InterruptedIOException) {
            Thread.currentThread().interrupt();
        }
        throw new RuntimeException("Failed to fetch BigQuery data.", e);
    }
    if (!response.getJobComplete()) {
        // query job not complete, verification failed
        return false;
    } else {
        // compute checksum
        actualChecksum = generateHash(response.getRows());
        LOG.debug("Generated a SHA1 checksum based on queried data: {}", actualChecksum);
        return expectedChecksum.equals(actualChecksum);
    }
}
Also used : InterruptedIOException(java.io.InterruptedIOException) QueryRequest(com.google.api.services.bigquery.model.QueryRequest) Bigquery(com.google.api.services.bigquery.Bigquery) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException)

Example 7 with QueryRequest

use of com.google.api.services.bigquery.model.QueryRequest in project beam by apache.

the class BigqueryMatcherTest method testBigqueryMatcherFailsWhenQueryJobNotComplete.

@Test
public void testBigqueryMatcherFailsWhenQueryJobNotComplete() throws Exception {
    BigqueryMatcher matcher = spy(new BigqueryMatcher(appName, projectId, query, "some-checksum"));
    doReturn(mockBigqueryClient).when(matcher).newBigqueryClient(anyString());
    when(mockQuery.execute()).thenReturn(new QueryResponse().setJobComplete(false));
    thrown.expect(AssertionError.class);
    thrown.expectMessage("The query job hasn't completed.");
    thrown.expectMessage("jobComplete=false");
    try {
        assertThat(mockResult, matcher);
    } finally {
        verify(matcher).newBigqueryClient(eq(appName));
        verify(mockJobs).query(eq(projectId), eq(new QueryRequest().setQuery(query)));
    }
}
Also used : QueryRequest(com.google.api.services.bigquery.model.QueryRequest) QueryResponse(com.google.api.services.bigquery.model.QueryResponse) Test(org.junit.Test)

Aggregations

QueryRequest (com.google.api.services.bigquery.model.QueryRequest)7 Test (org.junit.Test)5 IOException (java.io.IOException)3 QueryResponse (com.google.api.services.bigquery.model.QueryResponse)2 Bigquery (com.google.api.services.bigquery.Bigquery)1 GetQueryResults (com.google.api.services.bigquery.Bigquery.Jobs.GetQueryResults)1 InterruptedIOException (java.io.InterruptedIOException)1