简体   繁体   English

使用 Python 在 BigQuery 中创建数据集 | 接收类型错误

[英]Create a dataset in BigQuery with Python | Receiving TypeError

I am brand new to programming so here it goes.我是编程的新手,所以就这样了。

My goal : Create a dataset in an existing project in BigQuery (using python rather than UI)我的目标:在 BigQuery 的现有项目中创建数据集(使用 python 而不是 UI)

The docs show how to do this: [https://cloud.google.com/bigquery/docs/datasets#create-dataset]文档显示了如何执行此操作:[https://cloud.google.com/bigquery/docs/datasets#create-dataset]

My code :我的代码

from google.cloud import bigquery

os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="apikey.json"

# Construct BigQuery client object
client = bigquery.Client    

# Set dataset_id
dataset_id = 'myProject.myDataset'

# Construct a full Dataset object to send to the API.
dataset = bigquery.Dataset(dataset_id)

# Specify the geographic location
dataset.location = "US"

# # Send the dataset to the API for creation, with an explicit timeout. 
dataset = client.create_dataset(dataset, timeout=30)  # Make an API request.
print("Created dataset")

My error: TypeError: Client.create_dataset() missing 1 required positional argument: 'dataset'我的错误: TypeError: Client.create_dataset() missing 1 required positional argument: 'dataset'

I refer to the relevant documentation below ([https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client#google_cloud_bigquery_client_Client_create_dataset] )我参考了下面的相关文档([https://cloud.google.com/python/docs/reference/bigquery/latest/google.cloud.bigquery.client.Client#google_cloud_bigquery_client_Client_create_dataset])

but I'm struggling to understand how that first positional argument should look .但我很难理解第一个位置参数应该是什么样子 Thanks for any explanation!感谢您的任何解释!

I tested the script and it worked well:我测试了脚本并且效果很好:

def create_dataset():
    from google.cloud import bigquery

    # Construct a BigQuery client object.
    client = bigquery.Client()

    project_id = "{project_id}"
    dataset_id = f"{project_id}.mazlum_dataset_test"

    # Construct a full Dataset object to send to the API.
    dataset = bigquery.Dataset(dataset_id)

    # TODO(developer): Specify the geographic location where the dataset should reside.
    dataset.location = "EU"

    # Send the dataset to the API for creation, with an explicit timeout.
    # Raises google.api_core.exceptions.Conflict if the Dataset already
    # exists within the project.
    dataset = client.create_dataset(dataset, timeout=30)  # Make an API request.
    print("Created dataset {}.{}".format(project_id, dataset.dataset_id))


if __name__ == '__main__':
    create_dataset()

Your script seems to be correct.您的脚本似乎是正确的。

You have to check if you are used the correct Python package in your virtual env:您必须检查您是否在虚拟环境中使用了正确的Python package

requirements.txt file: requirements.txt文件:

google-cloud-bigquery==3.4.1

To check if your virtual env was correctly created, enter this command:要检查您的虚拟环境是否已正确创建,请输入以下命令:

which python3

It should display the python path in the virtual env.它应该在虚拟环境中显示python路径。

Launch the following command to install the package in your virtual env:启动以下命令以在您的虚拟环境中安装 package:

pip install -r requirements.txt

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM