全部,
我在使用大约两周前发布的 0.28 版 bq 库在 python 中创建 Google BigQuery View 时遇到问题。我很确定问题出在我这边,我错过了一些东西,但我找不到问题所在。
请温柔点,我不会在网上问很多问题,但我很困惑。 我也不是完全无能,这里有一些细节:
- 我的 GOOGLE_APPLICATION_CREDENTIALS 设置正确
- 我通过 python 对 bq 运行的所有其他命令都很好
我已经查看了 https://cloud.google.com/bigquery/docs/python-client-migration
我认为问题在于“修复” https://github.com/GoogleCloudPlatform/google-cloud-python/pull/4038 BigQuery:用 client.create_table() 替换 table.create() #4038
- 我尝试过传统 sql 与标准 sql
- 我使用的是 python 2.7.12(短期内无法升级,企业版本)
问题?下面第二 block 中的代码创建一个表,没有模式也没有记录。显然应该创建一个 VIEW,对吗?
sudo pip install -Iv google-cloud-bigquery==0.27.0
from google.cloud import bigquery
project=None
dataset_name = 'my_dataset_id'
view_name = 'vw_dummy_data20'
sqlQuery = 'select record_id as id, UPPER(first_name) as first_name, UPPER(last_name) as last_name from [my_project_code:my_dataset_id.dummy_data13]'
bigquery_client = bigquery.Client(project=project)
dataset = bigquery_client.dataset(dataset_name)
table = dataset.table(view_name)
table.view_query = sqlQuery
table.create()
以上工作正常, View 已创建,太棒了!
下面,仅创建了一个表,没有行,没有架构,糟糕!
sudo pip 卸载 google-cloud-bigquery
sudo pip install -Iv google-cloud-bigquery==0.28.0
from google.cloud import bigquery
project=None
dataset_name = 'my_dataset_id'
view_name = 'vw_dummy_data21'
sqlQuery = 'select record_id as id, UPPER(first_name) as first_name, UPPER(last_name) as last_name from [my_project_code:my_dataset_id.dummy_data13]'
bigquery_client = bigquery.Client(project=project)
dataset_ref = bigquery_client.dataset(dataset_name)
table_ref = dataset_ref.table(view_name)
table_ref.view_query = sqlQuery
table_ref.view_use_legacy_sql = True
table = bigquery.Table(table_ref)
bigquery_client.create_table(table)
其他链接:
- How can I create a new view in bigquery using the python API?
- https://googlecloudplatform.github.io/google-cloud-python/latest/bigquery/usage.html
任何有用的想法将不胜感激。
谢谢并致以最诚挚的问候...Rich
最佳答案
你太接近了!
问题在于线路
table_ref.view_query = sqlQuery
table_ref.view_use_legacy_sql = True
TableReference
类不包含这些属性。相反,您必须将它们填充到 Table
类中,如
table = bigquery.Table(table_ref)
table.view_query = sqlQuery
table.view_use_legacy_sql = True
bigquery_client.create_table(table)
关于python - 谷歌 BigQuery : creating a view via Python google-cloud-bigquery version 0. 27.0 与 0.28.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47273175/