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

查看:217
本文介绍了Google BigQuery:通过Python创建视图google-cloud-bigquery版本0.27.0与0.28.0的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所有,

我在使用bq库版本0.28的python中创建Google BigQuery视图时遇到了困难,该库已于两周前发布。我很确定问题出在我身上,但我找不到这个问题。



请保持温柔,我不要问很多的在线问题,但我很难过。
我也不是完全无能,下面是一些详细信息:


  1. 我的GOOGLE_APPLICATION_CREDENTIALS设置正确

  2. 我通过python运行的所有其他命令都很好

  3. 我已经查看了
    https://cloud.google.com/bigquery/docs/python-client-migration https://github.com/GoogleCloudPlatform/google-cloud-python/pull/4038
    BigQuery:用客户端替换table.create()。 create_table()#4038

  4. 很快就会升级,企业版的东西)

问题在哪里?下面第二个代码块中的代码创建一个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)

其他链接:



任何有用的想法都将非常感激。



感谢和最诚挚的问候... 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:

  1. I have my GOOGLE_APPLICATION_CREDENTIALS set correctly
  2. All the other commands I've run against bq via python are fine
  3. I've reviewed the https://cloud.google.com/bigquery/docs/python-client-migration

  4. 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

  5. I've tried legacy vs. standard sql
  6. 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:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆