大查询视图中的字段说明

时间:2018-08-30 09:34:39

标签: python-2.7 google-bigquery

我知道这个问题以前曾提出过,似乎已经解决了。 https://issuetracker.google.com/issues/35905194

但是这可以使用Python完成吗?

我有一个脚本可以在我要更新Big Query表上的字段描述时使用,但是当我在视图上运行完全相同的脚本时,什么都没有发生?

脚本运行了,我没有收到任何错误,但是只是没有更新Big Query中的视图

我正在使用Python 2.7.13

这是我的代码..

from google.cloud import bigquery
from datetime import datetime
import json
import sys

project='xxxx'
ds='xxxxx'
table_n='xxxxx'

startTime=datetime.now()
#Authorisation 
filename='xxxxxx.json'
client =  bigquery.Client.from_service_account_json(filename)

dataset_id = ds
table_id = table_n

table_schema= []
table_schema.append(bigquery.SchemaField('BWMI_ID', 'STRING', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('VP_VIN_PREFIX', 'STRING', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('CHASSIS_NUMBER', 'STRING', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('CAESAR_KEY', 'INTEGER', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('ID', 'INTEGER', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('VEH_ID', 'INTEGER', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('LOEV_ID', 'INTEGER', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('D42_LAST_UPDATED', 'TIMESTAMP', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('D42_END_DATE', 'TIMESTAMP', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('USER_1_ID', 'STRING', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('DESCRIPTION', 'STRING', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('DATE_ADDED', 'TIMESTAMP', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('DATE_ENDED', 'TIMESTAMP', mode='NULLABLE'))                            
table_schema.append(bigquery.SchemaField('SOURCE', 'STRING', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('CANCELLED', 'STRING', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('CANCELLED_DATE', 'TIMESTAMP', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('CRC32', 'STRING', mode='NULLABLE'))
table_schema.append(bigquery.SchemaField('CREATED_DATE', 'TIMESTAMP', mode='NULLABLE')) 

original_schema=table_schema
table_ref = client.dataset(dataset_id).table(table_id)
table = bigquery.Table(table_ref, schema=original_schema)

new_schema= []
new_schema.append(bigquery.SchemaField('BWMI_ID', 'STRING', mode='NULLABLE', description = 'Updated by python script'))
new_schema.append(bigquery.SchemaField('VP_VIN_PREFIX', 'STRING', mode='NULLABLE', description = 'Updated by python script'))
new_schema.append(bigquery.SchemaField('CHASSIS_NUMBER', 'STRING', mode='NULLABLE', description = 'Updated by python script'))
new_schema.append(bigquery.SchemaField('CAESAR_KEY', 'INTEGER', mode='NULLABLE', description = 'Updated by python script'))
new_schema.append(bigquery.SchemaField('ID', 'INTEGER', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('VEH_ID', 'INTEGER', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('LOEV_ID', 'INTEGER', mode='NULLABLE', description = 'Updated by python script'))
new_schema.append(bigquery.SchemaField('D42_LAST_UPDATED', 'TIMESTAMP', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('D42_END_DATE', 'TIMESTAMP', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('USER_1_ID', 'STRING', mode='NULLABLE', description = 'Updated by python script'))
new_schema.append(bigquery.SchemaField('DESCRIPTION', 'STRING', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('DATE_ADDED', 'TIMESTAMP', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('DATE_ENDED', 'TIMESTAMP', mode='NULLABLE', description = 'Updated by python script'))                            
new_schema.append(bigquery.SchemaField('SOURCE', 'STRING', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('CANCELLED', 'STRING', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('CANCELLED_DATE', 'TIMESTAMP', mode='NULLABLE', description = 'Updated by python script'))
new_schema.append(bigquery.SchemaField('CRC32', 'STRING', mode='NULLABLE'))
new_schema.append(bigquery.SchemaField('CREATED_DATE', 'TIMESTAMP', mode='NULLABLE'))

table.schema = new_schema
table = client.update_table(table, ['schema'])

3 个答案:

答案 0 :(得分:0)

我试图复制您的方案,但是,Python客户端库对我来说很好用

 #NEW VIEW
 view_ref = client.dataset(dataset_id).table(view_name)
 client.delete_table(view_ref)
 view = bigquery.Table(view_ref)
 sql_template = ('SELECT DISTINCT user,team FROM `{}.{}.{}` WHERE score = 19')
 view.view_query = sql_template.format(project,dataset_id,original_table)
 view = client.create_table(view)
 print('Successfully created view at {}'.format(view.full_table_id))
 #UPDATE VIEW
 view_schema= [
     bigquery.SchemaField('user', 'STRING', mode='NULLABLE',description='user name VIEW updated'),
     bigquery.SchemaField('team', 'STRING', mode='NULLABLE',description='team name VIEW updated'),
 ]
 viewU = bigquery.Table(view_ref,schema=view_schema)
 viewU = client.update_table(viewU,['schema'])
 print('UPDATED VIEW SCHEMA: {}').format(viewU.schema) #VERIFY THE SCHEMA

还有bq commandsbq show --format=prettyjson [PROJECT_ID]:[DATASET].[VIEW]

您是否尝试过在BQ UI的“查看详细信息”屏幕中单击Refresh按钮?

答案 1 :(得分:0)

好的,我现在发现了问题。 这是新的beta gui,没有显示说明。 Beta Gui ....

Beta Gui

Classic UI

Classic_UI ... 非常令人沮丧的是,我花了整整一天的时间摆弄我的代码,以尝试找出为什么它不起作用。

我将向Google提出一个错误。 无论如何,谢谢您的帮助。

答案 2 :(得分:0)

要更新视图架构不要使用

view.schema = new_schema
view = client.update_table(view, ['schema'])

相反

 view = bigquery.Table(view.reference, schema=new_schema)
 view = client.update_table(view, ['schema'])