Big Query 视图中的字段说明 [英] Descriptions on Fields in Big Query Views
问题描述
我知道这个问题之前已经提出过并且似乎已经解决了..https://issuetracker.google.com/issues/35905194
I know this question has been raised before and appeared to have been resolved.. https://issuetracker.google.com/issues/35905194
但是这可以使用 Python 完成吗?
But can this be done using Python yet ?
当我想更新 Big Query 表上的字段描述时,我有一个脚本可以工作,但是当我在视图上运行完全相同的脚本时,没有任何反应?
I have a script which works when I want to update field descriptions on a Big Query table but when I run the exact same script on a view nothing happens ?
脚本运行,我没有收到任何错误,但它只是没有更新 Big Query 中的视图
The script runs and I don't get any error but it is just not updating the view in Big Query
我使用的是 Python 2.7.13
I am using 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'])
推荐答案
我已尝试复制您的方案,但是,Python 客户端库对我来说运行良好
I've tried to replicate your scenario, however, the Python client libraries are working well for me
#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 命令:bq show --format=prettyjson [PROJECT_ID]:[DATASET].[VIEW]
您是否尝试过单击 BQ UI 中查看详细信息"屏幕中的 Refresh
按钮?
Have you tried clicking the Refresh
button within the "View Details" screen in the BQ UI?
这篇关于Big Query 视图中的字段说明的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!