4

I am trying to print count of rows available in table of bigquery in python,I have written below code:

from google.cloud import bigquery def main():
    myquery = "select count(*) from `myproject.mydataset.mytable`"
    client = bigquery.Client()
    job = client.query(myquery)
    result = job.result()
    print("Total rows available: ",result)

When I execute above code it gives me output as

"Total rows available: google.cloud.bigquery.table.RowIterator object at 0x00000247B65F29E8>".

It means I am getting object value as my output. But I want to print actual count of rows available in my table(query result).

1
  • It'd probably be better if you used the Table object to do so. See the example for connecting with your table then just print table.num_rows; this operation would be free of charge and much faster. Commented Oct 11, 2019 at 0:06

3 Answers 3

6

RowIterator has an attribute called total_rows.

Simply change your last statement to

 print("Total rows available: ", result.total_rows)
Sign up to request clarification or add additional context in comments.

4 Comments

I just tried this way but it gives me "Total rows available: None" as output. But in actual I have 634700 rows in that respective BQ table (output if i run same query from BQ console).
In any case, expected result with this change only should be 1. As count returns one row.
No. result.total_rows and result.num_results only report how many results have been iterated and show None or 0 before you start to iterate over the RowIterator
results.total_rows is perfect. cloud.google.com/bigquery/docs/samples/…
5

Try changing your query to

myquery = "select count(*) size from `myproject.mydataset.mytable`"
client = bigquery.Client()
job = client.query(myquery)
result = job.result()
for row in results:
    print("Total rows available: ",row.size)

Comments

0

BigQuery first returns a query object and from there you can get the result using .result(). But that is an Iterator object so you have to iterate to get the values. In a count (*) you know that there is only one row to iterate on so you can use "next". And that returns a row with one value in it and thus the [0].

    myquery = "select count(*) from `myproject.mydataset.mytable`"
    iterator = client.query(myquery).result()
    first_row = next(iterator)
    print("Total rows available: ",first_row[0])

1 Comment

Thank you for contributing to the Stack Overflow community. This may be a correct answer, but it’d be really useful to provide additional explanation of your code so developers can understand your reasoning. This is especially useful for new developers who aren’t as familiar with the syntax or struggling to understand the concepts. Would you kindly edit your answer to include additional details for the benefit of the community?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.