Search code examples
javagoogle-bigquerygoogle-cloud-sdk

Ordering BigQuery Results in Java SDK


I am trying to get ordered results from a BigQuery with help of google cloud SDK.

The query looks like:

SELECT * FROM `table`
            |WHERE id = 111
            |ORDER BY time DESC

Then I create and run the Job:

Job job = QueryJobConfiguration.newBuilder(query)
            .setUseLegacySql(false)
            .build()

The issue, is when I actually fetch results, I receive them unordered:

TableResult results = job.getQueryResults()
results.iterateAll()

If I run the original query inside the BigQuery UI, everything seems to fine.

Any ideas, at which place and why the results being shuffled?


Solution

  • The issue was, that I've added ORDER BY clause later in query.

    Still, I was accessing the job with the same jobId. That made BigQuery to fetch previous results, which where unsorted. Updating JobId helped!