Issue
I am using google.cloud.bigquery library to execute and create query using bigquery.query()
method. I want to fetch the Schema
details from the response but whenever the query returns no result, I am getting EmptyTableResult
instead of the response which should have Schema
and Fields listed inside it. I used another approach which creates job and then using the query job, I am calling bigquery.getQueryResults
which should return QueryResponse
object. Below is the code snippet.
QueryJobConfiguration queryConfig =
QueryJobConfiguration.newBuilder(queryString)
.setDefaultDataset(bqDatasetId).setUseLegacySql(false)
.setFlattenResults(true).build();
JobId jobId = JobId.of(UUID.randomUUID().toString());
Job queryJob = bigQuery.create(JobInfo.newBuilder(queryConfig).setJobId(jobId).build());
// Wait for the query to complete.
queryJob = queryJob.waitFor();
// Check for errors
if (queryJob == null) {
throw new RuntimeException("Job no longer exists");
} else if (queryJob.getStatus().getError() != null) {
throw new RuntimeException(queryJob.getStatus().getError().toString());
}
// Get the results.
QueryResponse response = bigQuery.getQueryResults(queryJob.getJobId());
System.out.println(response);
Here, in sysout
statement, I am getting the proper response, but whenever I am trying to use response.getSchema()
, it is giving me compilation error saying getSchema()
is not visible. Can anyone help me with this? Is this approach correct or there is any other approach which can do the same thing?
Solution
After searching a lot, I came to a conclusion that it is better to use reflections to invoke the invisible QueryRespons.getSchema() method and it worked like a charm. Though, reflection is not an ideal solution for this but it has resolved my problem.
Answered By - Manushi
Answer Checked By - Robin (JavaFixing Admin)