Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigQuery async query job - the fetch_results() method returns wrong number of values

I am writing Python code with the BigQuery Client API, and attempting to use the async query code (written everywhere as a code sample), and it is failing at the fetch_data() method call. Python errors out with the error:

ValueError: too many values to unpack

So, the 3 return values (rows, total_count, page_token) seem to be the incorrect number of return values. But, I cannot find any documentation about what this method is supposed to return -- besides the numerous code examples that only show these 3 return results.

Here is a snippet of code that shows what I'm doing (not including the initialization of the 'client' variable or the imported libraries, which happen earlier in my code).

#---> Set up and start the async query job
job_id = str(uuid.uuid4())
job = client.run_async_query(job_id, query)
job.destination = temp_tbl
job.write_disposition = 'WRITE_TRUNCATE'
job.begin()
print 'job started...'

#---> Monitor the job for completion
retry_count = 360
while retry_count > 0 and job.state != 'DONE':
    print 'waiting for job to complete...'
    retry_count -= 1
    time.sleep(1)
    job.reload()


if job.state == 'DONE':
     print 'job DONE.'
     page_token = None
     total_count = None
     rownum = 0
     job_results = job.results()

     while True:

         # ---- Next line of code errors out...
         rows, total_count, page_token = job_results.fetch_data( max_results=10, page_token=page_token )

         for row in rows:
             rownum += 1
             print "Row number %d" % rownum

             if page_token is None:
                 print 'end of batch.'
                 break

What are the specific return results I should expect from the job_results.fetch_data(...) method call on an async query job?

like image 385
phil_scott_a_person Avatar asked Jul 04 '17 01:07

phil_scott_a_person


1 Answers

Looks like you are right! The code no longer return these 3 parameters.

As you can see in this commit from the public repository, fetch_data now returns an instance of the HTTPIterator class (guess I didn't realize this before as I have a docker image with an older version of the bigquery client installed where it does return the 3 values).

The only way that I found to return the results was doing something like this:

iterator = job_results.fetch_data()
data = []
for page in iterator._page_iter(False):
    data.extend([page.next() for i in range(page.num_items)])

Notice that now we don't have to manage pageTokens anymore, it's been automated for the most part.

[EDIT]:

I just realized you can get results by doing:

results = list(job_results.fetch_data())

Got to admit it's way easier now then it was before!

like image 162
Willian Fuks Avatar answered Oct 22 '22 02:10

Willian Fuks