英文:
Reuse bigquery queryJob as base query to use for further operation
问题
The translated content is as follows:
正如标题所说,我不知道是否可以重复使用通过执行查询获得的queryjob来执行其他SQL操作。以下是我所指的示例:
from google.cloud import bigquery
client = bigquery.Client()
myquery = "SELECT * FROM mytable"
qjob = client.query(myquery)
qjob包含了查询的结果。我想要使用这个结果来执行额外的筛选,在伪代码中 ...
...
mynewquery = "SELECT c1 FROM <qjob> WHERE C1=1"
qjob2 = client.query(mynewquery)
希望这样清楚了。这与以下构造有些类似
WITH <query name> AS (SELECT ...)
SELECT a FROM <query name> WHERE...
英文:
As the title says, I don't know if it is possible to reuse the queryjob obtained by the execution of a query to perform additional SQL operations. Below an example of what I mean
from google.cloud import bigquery
client = bigquery.Client()
myquery = "SELECT * FROM mytable"
qjob = client.query(myquery)
qjob contains the query results. I want to use this result to perform additional filtering, in pseudo code ...
...
mynewquery = "SELECT c1 FROM <qjob> WHERE C1=1"
qjob2 = client.query(mynewquery)
Hope it is clear. This is somehow similar to the construct
WITH <query name> AS (SELECT ...)
SELECT a FROM <query name> WHERE...
答案1
得分: 1
我相信使用pandas是处理你的查询的正确方法。你可以将一个queryjob对象转换成数据帧,一旦存储到数据帧中,你可以对其进行操作。
参考代码示例:
from google.cloud import bigquery
client = bigquery.Client()
sql = """
SELECT name, SUM(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_current`
GROUP BY name
ORDER BY count DESC
LIMIT 10
"""
df = client.query(sql).to_dataframe()
参考链接: https://cloud.google.com/bigquery/docs/samples/bigquery-query-results-dataframe
Pandas: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html
英文:
I believe pandas is the way to go to your inquiry. You can convert a queryjob object into dataframes, once stored into a dataframe you can manipulate it.
See Code Sample:
from google.cloud import bigquery
client = bigquery.Client()
sql = """
SELECT name, SUM(number) as count
FROM `bigquery-public-data.usa_names.usa_1910_current`
GROUP BY name
ORDER BY count DESC
LIMIT 10
"""
df = client.query(sql).to_dataframe()
Reference: https://cloud.google.com/bigquery/docs/samples/bigquery-query-results-dataframe
Pandas: https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.html
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论