python - 谷歌 BigQuery : creating a view via Python google-cloud-bigquery version 0. 27.0 与 0.28.0

标签 python python-2.7 google-bigquery

全部,

我在使用大约两周前发布的 0.28 版 bq 库在 python 中创建 Google BigQuery View 时遇到问题。我很确定问题出在我这边,我错过了一些东西,但我找不到问题所在。

请温柔点,我不会在网上问很多问题,但我很困惑。 我也不是完全无能,这里有一些细节:

  1. 我的 GOOGLE_APPLICATION_CREDENTIALS 设置正确
  2. 我通过 python 对 bq 运行的所有其他命令都很好
  3. 我已经查看了 https://cloud.google.com/bigquery/docs/python-client-migration

  4. 我认为问题在于“修复” https://github.com/GoogleCloudPlatform/google-cloud-python/pull/4038 BigQuery:用 client.create_table() 替换 table.create() #4038

  5. 我尝试过传统 sql 与标准 sql
  6. 我使用的是 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)

其他链接:

任何有用的想法将不胜感激。

谢谢并致以最诚挚的问候...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/

相关文章:

python - GQL 内部交易

python - 正确使用来自 tfds.load() 的 Cifar-10 数据集

python - 散布上的箭头

c# - 从 Iron Python 脚本(从 C# 脚本引擎调用)调用 Python 脚本时出现问题

在我从代码创建它之后,Golang 代码没有插入到 BigQuery 的表中

python - 根据值遍历决策树;迭代进入子词典?

setup.py 中的 Python 包名称与 pip 名称

python - 如何在 ReportLab 中将酒吧聊天中的每个单独的酒吧设置为不同的颜色?

json - 将 JSON 数据加载到大查询中时出错 : flat value specified for record field

google-bigquery - BigQuery ML 如何处理 NULL 数字特征?