如何将数据集id传递给python的bigquery客户端

如何将数据集id传递给python的bigquery客户端,python,google-bigquery,Python,Google Bigquery,我刚刚开始使用bigquery,我正在尝试将数据集id传递给python客户机。这应该是一个非常基本的操作,但我在其他线程上找不到它 实际上,我想举下面的例子 # import packages import os from google.cloud import bigquery # set current work directory to the one with this script. os.chdir(os.path.dirname(os.path.abspath(__file__

我刚刚开始使用bigquery,我正在尝试将数据集id传递给python客户机。这应该是一个非常基本的操作,但我在其他线程上找不到它

实际上,我想举下面的例子

# import packages
import os
from google.cloud import bigquery

# set current work directory to the one with this script.
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# initialize client object using the bigquery key I generated from Google clouds
google_credentials_path = 'bigquery-stackoverflow-DC-fdb49371cf87.json'
client = bigquery.Client.from_service_account_json(google_credentials_path)

# create simple query
query_job = client.query(
    """
    SELECT
      CONCAT(
        'https://stackoverflow.com/questions/',
        CAST(id as STRING)) as url,
      view_count
    FROM `bigquery-public-data.stackoverflow.posts_questions`
    WHERE tags like '%google-bigquery%'
    ORDER BY view_count DESC
    LIMIT 10"""
)

# store results in dataframe
dataframe_query = query_job.result().to_dataframe()
让它看起来像

# import packages
import os
from google.cloud import bigquery

# set current work directory to the one with this script.
os.chdir(os.path.dirname(os.path.abspath(__file__)))

# initialize client object using the bigquery key I generated from Google clouds
google_credentials_path = 'bigquery-stackoverflow-DC-fdb49371cf87.json'
client = bigquery.Client.from_service_account_json(google_credentials_path)\
                        .A_function_to_specify_id(bigquery-public-data.stackoverflow)

# create simple query
query_job = client.query(
    """
    SELECT
      CONCAT(
        'https://stackoverflow.com/questions/',
        CAST(id as STRING)) as url,
      view_count
    FROM `posts_questions` -- No dataset ID here anymore
    WHERE tags like 'google-bigquery'
    ORDER BY view_count DESC
    LIMIT 10"""
)

# store results in dataframe
dataframe_query = query_job.result().to_dataframe()

我找不到文档,因此非常感谢您的帮助。

最接近您所要求的是查询作业配置的
default\u dataset
()属性。查询作业配置是一个可选对象,可以传递到实例化的BigQuery客户端的
query()
方法中

您不会将默认数据集设置为实例化客户机的一部分,因为并非所有资源都是数据集范围的。在您的示例中,您隐式地处理一个查询作业,它是一个项目范围的资源

因此,为了稍微调整您的示例,它可能会如下所示:

# skip the irrelevant bits like imports and client construction

job_config = bigquery.QueryJobConfig(default_dataset="bigquery-public-data.stackoverflow")

sql = "SELECT COUNT(1) FROM posts_questions WHERE tags like 'google-bigquery'"

dataframe = client.query(sql, job_config=job_config).to_dataframe()

如果您对同一数据集发出多个查询,您当然可以通过多个查询调用重用同一个作业配置对象。

最接近您所要求的是查询作业配置的
default\u dataset
()属性。查询作业配置是一个可选对象,可以传递到实例化的BigQuery客户端的
query()
方法中

您不会将默认数据集设置为实例化客户机的一部分,因为并非所有资源都是数据集范围的。在您的示例中,您隐式地处理一个查询作业,它是一个项目范围的资源

因此,为了稍微调整您的示例,它可能会如下所示:

# skip the irrelevant bits like imports and client construction

job_config = bigquery.QueryJobConfig(default_dataset="bigquery-public-data.stackoverflow")

sql = "SELECT COUNT(1) FROM posts_questions WHERE tags like 'google-bigquery'"

dataframe = client.query(sql, job_config=job_config).to_dataframe()

如果您针对同一数据集发出多个查询,您当然可以通过多个查询调用重用同一作业配置对象。

谢谢您的回答。这正是我要找的!谢谢你的回答。这正是我要找的!