use of com.google.api.services.bigquery.model.GetQueryResultsResponse in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryImplTest method testGetQueryResults.
@Test
public void testGetQueryResults() {
JobId queryJob = JobId.of(JOB);
GetQueryResultsResponse responsePb = new GetQueryResultsResponse().setEtag("etag").setJobReference(queryJob.toPb()).setRows(ImmutableList.of(TABLE_ROW)).setJobComplete(true).setCacheHit(false).setPageToken(CURSOR).setTotalBytesProcessed(42L).setTotalRows(BigInteger.valueOf(1L));
EasyMock.expect(bigqueryRpcMock.getQueryResults(PROJECT, JOB, EMPTY_RPC_OPTIONS)).andReturn(responsePb);
EasyMock.replay(bigqueryRpcMock);
bigquery = options.getService();
QueryResponse response = bigquery.getQueryResults(queryJob);
assertEquals("etag", response.getEtag());
assertEquals(queryJob, response.getJobId());
assertEquals(true, response.jobCompleted());
assertEquals(false, response.getResult().cacheHit());
assertEquals(ImmutableList.<BigQueryError>of(), response.getExecutionErrors());
assertFalse(response.hasErrors());
assertEquals(null, response.getResult().getSchema());
assertEquals(42L, response.getResult().getTotalBytesProcessed());
assertEquals(1L, response.getResult().getTotalRows());
for (List<FieldValue> row : response.getResult().getValues()) {
assertEquals(false, row.get(0).getBooleanValue());
assertEquals(1L, row.get(1).getLongValue());
}
assertEquals(CURSOR, response.getResult().getNextPageToken());
}
use of com.google.api.services.bigquery.model.GetQueryResultsResponse in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryImplTest method testGetQueryResultsWithProject.
@Test
public void testGetQueryResultsWithProject() {
JobId queryJob = JobId.of(OTHER_PROJECT, JOB);
GetQueryResultsResponse responsePb = new GetQueryResultsResponse().setEtag("etag").setJobReference(queryJob.toPb()).setRows(ImmutableList.of(TABLE_ROW)).setJobComplete(true).setCacheHit(false).setPageToken(CURSOR).setTotalBytesProcessed(42L).setTotalRows(BigInteger.valueOf(1L));
EasyMock.expect(bigqueryRpcMock.getQueryResults(OTHER_PROJECT, JOB, EMPTY_RPC_OPTIONS)).andReturn(responsePb);
EasyMock.replay(bigqueryRpcMock);
bigquery = options.getService();
QueryResponse response = bigquery.getQueryResults(queryJob);
assertEquals("etag", response.getEtag());
assertEquals(queryJob, response.getJobId());
assertEquals(true, response.jobCompleted());
assertEquals(false, response.getResult().cacheHit());
assertEquals(ImmutableList.<BigQueryError>of(), response.getExecutionErrors());
assertFalse(response.hasErrors());
assertEquals(null, response.getResult().getSchema());
assertEquals(42L, response.getResult().getTotalBytesProcessed());
assertEquals(1L, response.getResult().getTotalRows());
for (List<FieldValue> row : response.getResult().getValues()) {
assertEquals(false, row.get(0).getBooleanValue());
assertEquals(1L, row.get(1).getLongValue());
}
assertEquals(CURSOR, response.getResult().getNextPageToken());
}
use of com.google.api.services.bigquery.model.GetQueryResultsResponse in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryImplTest method testGetQueryResultsWithOptions.
@Test
public void testGetQueryResultsWithOptions() {
JobId queryJob = JobId.of(PROJECT, JOB);
GetQueryResultsResponse responsePb = new GetQueryResultsResponse().setJobReference(queryJob.toPb()).setRows(ImmutableList.of(TABLE_ROW)).setJobComplete(true).setCacheHit(false).setPageToken(CURSOR).setTotalBytesProcessed(42L).setTotalRows(BigInteger.valueOf(1L));
EasyMock.expect(bigqueryRpcMock.getQueryResults(PROJECT, JOB, QUERY_RESULTS_OPTIONS)).andReturn(responsePb);
EasyMock.replay(bigqueryRpcMock);
bigquery = options.getService();
QueryResponse response = bigquery.getQueryResults(queryJob, QUERY_RESULTS_OPTION_TIME, QUERY_RESULTS_OPTION_INDEX, QUERY_RESULTS_OPTION_PAGE_SIZE, QUERY_RESULTS_OPTION_PAGE_TOKEN);
assertEquals(queryJob, response.getJobId());
assertEquals(true, response.jobCompleted());
assertEquals(false, response.getResult().cacheHit());
assertEquals(ImmutableList.<BigQueryError>of(), response.getExecutionErrors());
assertFalse(response.hasErrors());
assertEquals(null, response.getResult().getSchema());
assertEquals(42L, response.getResult().getTotalBytesProcessed());
assertEquals(1L, response.getResult().getTotalRows());
for (List<FieldValue> row : response.getResult().getValues()) {
assertEquals(false, row.get(0).getBooleanValue());
assertEquals(1L, row.get(1).getLongValue());
}
assertEquals(CURSOR, response.getResult().getNextPageToken());
}
use of com.google.api.services.bigquery.model.GetQueryResultsResponse in project google-cloud-java by GoogleCloudPlatform.
the class BigQueryImpl method getQueryResults.
private static QueryResponse getQueryResults(JobId jobId, final BigQueryOptions serviceOptions, final Map<BigQueryRpc.Option, ?> optionsMap) {
final JobId completeJobId = jobId.setProjectId(serviceOptions.getProjectId());
try {
GetQueryResultsResponse results = runWithRetries(new Callable<GetQueryResultsResponse>() {
@Override
public GetQueryResultsResponse call() {
return serviceOptions.getBigQueryRpcV2().getQueryResults(completeJobId.getProject(), completeJobId.getJob(), optionsMap);
}
}, serviceOptions.getRetrySettings(), EXCEPTION_HANDLER, serviceOptions.getClock());
QueryResponse.Builder builder = QueryResponse.newBuilder();
builder.setJobId(JobId.fromPb(results.getJobReference()));
builder.setEtag(results.getEtag());
builder.setJobCompleted(results.getJobComplete());
List<TableRow> rowsPb = results.getRows();
if (results.getJobComplete()) {
QueryResult.Builder resultBuilder = transformQueryResults(completeJobId, rowsPb, results.getPageToken(), serviceOptions, ImmutableMap.<BigQueryRpc.Option, Object>of());
resultBuilder.setTotalBytesProcessed(results.getTotalBytesProcessed());
resultBuilder.setCacheHit(results.getCacheHit());
if (results.getSchema() != null) {
resultBuilder.setSchema(Schema.fromPb(results.getSchema()));
}
if (results.getTotalRows() != null) {
resultBuilder.setTotalRows(results.getTotalRows().longValue());
}
builder.setResult(resultBuilder.build());
}
if (results.getErrors() != null) {
builder.setExecutionErrors(Lists.transform(results.getErrors(), BigQueryError.FROM_PB_FUNCTION));
}
return builder.build();
} catch (RetryHelper.RetryHelperException e) {
throw BigQueryException.translateAndThrow(e);
}
}
use of com.google.api.services.bigquery.model.GetQueryResultsResponse in project zeppelin by apache.
the class BigQueryInterpreter method executeSql.
//Function to call bigQuery to run SQL and return results to the Interpreter for output
private InterpreterResult executeSql(String sql) {
int counter = 0;
StringBuilder finalmessage = null;
finalmessage = new StringBuilder("%table ");
String projId = getProperty(PROJECT_ID);
long wTime = Long.parseLong(getProperty(WAIT_TIME));
long maxRows = Long.parseLong(getProperty(MAX_ROWS));
Iterator<GetQueryResultsResponse> pages;
try {
pages = run(sql, projId, wTime, maxRows);
} catch (IOException ex) {
logger.error(ex.getMessage());
return new InterpreterResult(Code.ERROR, ex.getMessage());
}
try {
while (pages.hasNext()) {
finalmessage.append(printRows(pages.next()));
}
return new InterpreterResult(Code.SUCCESS, finalmessage.toString());
} catch (NullPointerException ex) {
return new InterpreterResult(Code.ERROR, ex.getMessage());
}
}
Aggregations