BigQuery Python 客户端库中查询结果的差异

2024-02-24

我想知道 query() 的返回值和 query().result() 之间的区别。

在 BigQuery Python 客户端库中,

bigquery_client = bigquery.Client()
myQuery = "SELECT * FROM `mytable`"

## NOTE: This query result has just 1 row.
job = bigquery_client.query(myQuery)
for row in job:
    val1 = row

result = job.result()
for row in result:
    val2 = row

print(job == result) # False. I know QueryJob object is different to RowIterator object.
print(val1 == val2) # True

为什么 val1 和 val2 相等? 对于非常大的查询,这些值可以不同吗?


这是一个长达一年的自我回答。

基本上,我的代码中的“工作”和“结果”是不同的。

bigquery_client.query() 返回 QueryJob 实例。 ( 看https://googleapis.dev/python/bigquery/latest/ generated/google.cloud.bigquery.client.Client.html#google.cloud.bigquery.client.Client.query https://googleapis.dev/python/bigquery/latest/generated/google.cloud.bigquery.client.Client.html#google.cloud.bigquery.client.Client.query )

但是 QueryJob 类有自己的 _iter_ 方法并返回 iter(self.result()) ( 看https://github.com/googleapis/python-bigquery/blob/main/google/cloud/bigquery/job/query.py#L1778 https://github.com/googleapis/python-bigquery/blob/main/google/cloud/bigquery/job/query.py#L1778 )

因此,“job”成为 for-in 循环的 result() 迭代器。

因此,job != result 但 val1 == val2。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

BigQuery Python 客户端库中查询结果的差异 的相关文章

随机推荐