简体   繁体   English

如何使用以下python bigquery api创建表?

[英]how to create a table using the following python bigquery api?

Hello I cloned the following repository: 您好,我克隆了以下存储库:

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

I was reading the documentation about creating a table as follows: 我正在阅读有关创建表的文档,如下所示:

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

I tried to execute the same in my console as follows: 我尝试在控制台中执行如下操作:

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

I only got 'False' and when I check the visual interface there is no table. 我只得到“假”,当我检查可视界面时没有表格。

>>> print(created)
False
>>> 

I dont have any idea about the issue, I really frustrated since I just what to create one table, so I really would like to appreciate the support to overcome this task. 我对这个问题一无所知,我真的很沮丧,因为我只是要创建一个表,所以我真的很感谢能克服这一任务的支持。

I have tested this code and it works for me: 我已经测试了这段代码,它对我有用:

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)

You need to check 2 things: 您需要检查两件事:

  1. readonly parameter in the get_client method is set to False . get_client方法中的readonly参数设置为False Otherwise BigQuery access is read-only. 否则,BigQuery访问权限是只读的。

  2. The service account or user provided in the key.json file has been granted with the permissions to create a BigQuery table . key.json文件中提供的服务帐户或用户已被授予创建BigQuery表权限 At least it must have the roles/bigquery.dataEditor role granted. 至少它必须具有已授予的roles/bigquery.dataEditor角色。 To do it run the following command: 为此,请运行以下命令:

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


Anyway I recommend you to use the official Python Client for Google BigQuery , you will have better documentation and functionality. 无论如何,我建议您使用Google BigQuery的官方Python客户端 ,您将获得更好的文档和功能。 As well as a huge amount of code examples in the BigQuery official documentation . 以及BigQuery官方文档中的大量代码示例。

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

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