你可以试试UNNEST,BIGQUERY 中的查询是这样的:
SELECT * FROM `xx.mytable` WHERE items in UNNEST (['a','b','c'])
在您的代码中应该如下所示:
SELECT * FROM `xx.mytable` WHERE items in UNNEST (list)
编辑
我发现了两种在 Python 中传递变量的不同方法。
第一种方法如下。来自谷歌文档[1]。
from google.cloud import bigquery
# Construct a BigQuery client object.
client = bigquery.Client()
query = """
SELECT * FROM `xx.mytable` WHERE items in UNNEST (@list)
"""
job_config = bigquery.QueryJobConfig(
query_parameters=[
bigquery.ArrayQueryParameter("list", "STRING", ["a", "b", "c"]),
]
)
query_job = client.query(query, job_config=job_config) # Make an API request.
for row in query_job:
print("{}: \t{}".format(row.name, row.count))
第二种方法在下一个文档中[2]。在您的代码中应如下所示:
params = {'list': '[“a”,”b”,”c”]'}
%%bigquery df --params $params --project xxx query
select * from `xx.mytable`
where items in unnest (@list)
我还找到了一些文档[3],其中显示了 %%bigquery 魔术的参数。
[1]https://cloud.google.com/bigquery/docs/parameterized-queries#using_arrays_in_parameterized_queries
[2]https://notebook.community/GoogleCloudPlatform/python-docs-samples/notebooks/tutorials/bigquery/BigQuery%20query%20magic
[3]https://googleapis.dev/python/bigquery/latest/magics.html