Google BigQuery:通过Python创建视图google-cloud-bigquery版本0.27.0与0.28.0 [英] Google BigQuery: creating a view via Python google-cloud-bigquery version 0.27.0 vs. 0.28.0
问题描述
我在使用bq库版本0.28的python中创建Google BigQuery视图时遇到了困难,该库已于两周前发布。我很确定问题出在我身上,但我找不到这个问题。
请保持温柔,我不要问很多的在线问题,但我很难过。
我也不是完全无能,下面是一些详细信息:
- 我的GOOGLE_APPLICATION_CREDENTIALS设置正确
- 我通过python运行的所有其他命令都很好
-
我已经查看了
https://cloud.google.com/bigquery/docs/python-client-migration 我认为问题在于修复
https://github.com/GoogleCloudPlatform/google-cloud-python/pull/4038
BigQuery:用客户端替换table.create()。 create_table()#4038 很快就会升级,企业版的东西)
问题在哪里?下面第二个代码块中的代码创建一个TABLE,没有模式,也没有记录。它显然应该创建一个视图,对吗?
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
$ b $ table.create()
上述工作正常,视图已创建,很棒!
下面,只创建了一个表,没有行,没有模式,yuck!
sudo pip卸载google-cloud-bigquery
sudo pip install -Iv google-cloud-bigquery == 0.28.0
自google.cloud import bigquery
$ b project = None
dataset_name ='my_dataset_id'
view_name ='vw_dummy_data21'
sqlQuery ='select record_id as id,UPPER(first_name)作为first_name,从[my_project_code:my_dataset_id.dummy_data13]'作为姓氏的UPPER(last_name)''
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)
其他链接:
- 如何使用python API在bigquery中创建新视图?
- https://googlecloudplatform.github.io/google-cloud-python/latest/bigquery/usage.html
任何有用的想法都将非常感激。
感谢和最诚挚的问候... Rich
你太亲近了!
问题在于行
table_ref.view_query = sqlQuery
table_ref.view_use_legacy_sql = True
a TableReference
类不包含这些属性。相反,您必须将它们填充到 Table
类中,如
table = bigquery.Table(table_ref)
table.view_query = sqlQuery
table.view_use_legacy_sql = True
bigquery_client.create_table(table)
All,
I'm having trouble creating a Google BigQuery view in python with the version 0.28 of the bq library that has come out about two weeks ago. I'm quite certain the problem is on my side, something I'm missing, but I cannot find the issue.
Please be gentle, I don't ask lots of questions online but I'm quite stumped. I'm also not completely incompetent, here are some details:
- I have my GOOGLE_APPLICATION_CREDENTIALS set correctly
- All the other commands I've run against bq via python are fine
I've reviewed the https://cloud.google.com/bigquery/docs/python-client-migration
I think the issue is around the "fix" https://github.com/GoogleCloudPlatform/google-cloud-python/pull/4038 BigQuery: replaces table.create() with client.create_table() #4038
- I've tried legacy vs. standard sql
- I'm on python 2.7.12 (can't upgrade anytime soon, corporate version thing)
The issue? The code in the second block below creates a TABLE, with no schema and no records. It clearly should create a VIEW instead, right?
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()
the above works fine, view created, great!
the below, only a table is created, no rows, no schema, yuck!
sudo pip uninstall 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)
Other links:
- How can I create a new view in bigquery using the python API?
- https://googlecloudplatform.github.io/google-cloud-python/latest/bigquery/usage.html
Any useful thoughts would be very much appreciated.
Thanks and best regards...Rich
You were so close!
The issue is with the lines
table_ref.view_query = sqlQuery
table_ref.view_use_legacy_sql = True
a TableReference
class does not contain these properties. Instead, you must populate them on the Table
class, as in
table = bigquery.Table(table_ref)
table.view_query = sqlQuery
table.view_use_legacy_sql = True
bigquery_client.create_table(table)
这篇关于Google BigQuery:通过Python创建视图google-cloud-bigquery版本0.27.0与0.28.0的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!