如何使用下面的python bigqueryapi创建表?

2024-04-19 07:02:21 发布

您现在位置:Python中文网/ 问答频道 /正文

你好,我克隆了以下存储库:

https://github.com/tylertreat/BigQuery-Python

我正在阅读有关创建表的文档,如下所示:

schema = [
    {'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},
    {'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}
]
created = client.create_table('dataset', 'my_table', schema)

我尝试在控制台中执行相同的操作,如下所示:

>>> schema = [
...     {'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},
...     {'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}
... ]
>>> created = client.create_table('data_set_course', 'my_table_testing', schema)

我只得到'假',当我检查视觉界面没有表。你知道吗

>>> print(created)
False
>>> 

我没有任何关于这个问题的想法,我真的很沮丧,因为我只是什么创建一个表,所以我真的很想感谢支持克服这个任务。你知道吗


Tags: namehttpsclientstringfoomodeschemamy
1条回答
网友
1楼 · 发布于 2024-04-19 07:02:21

我已经测试了这个代码,它对我很有用:

from bigquery import get_client

# JSON key provided by Google
json_key = 'key.json'

client = get_client(json_key_file=json_key, readonly=False)

schema = [
    {'name': 'foo', 'type': 'STRING', 'mode': 'nullable'},
    {'name': 'bar', 'type': 'FLOAT', 'mode': 'nullable'}
]
created = client.create_table(dataset='yourDataset', table='tableToCreate', schema=schema)

你需要检查两件事:

  1. get_client方法中的readonly参数设置为False。否则,BigQuery访问是只读的。

  2. key.json文件中提供的服务帐户或用户已被授予permissions to create a BigQuery table。至少它必须被授予roles/bigquery.dataEditor角色。要执行此操作,请运行以下命令:

gcloud projects add-iam-policy-binding [PROJECT_ID]  member "serviceAccount:[NAME]@[PROJECT_ID].iam.gserviceaccount.com"  role "roles/bigquery.dataEditor"


无论如何,我建议您使用官方的Python Client for Google BigQuery,您将拥有更好的文档和功能。以及BigQuery official documentation中的大量代码示例。你知道吗

相关问题 更多 >