繁体   English   中英

BigQuery:创建视图并将注释添加到查询顶部

[英]BigQuery: create view with comments added to the top of the query

我想在 BigQuery 中创建一个视图,该视图还显示作者、创建日期等评论。

但是如果我在 UI 中尝试这样做,评论就会被排除在外。
有没有办法在 BigQuery UI 中执行此操作?
或者还有其他使用 bq 客户端或 python 的方法,或者......?

例如,如果我运行这个:

CREATE OR REPLACE VIEW `my_project_id.my_dataset.my_view_name`
AS
-- this is my important comment. This will be a long and extensive comment.
SELECT 1 as column_a
;

当使用 UI 创建视图时,BigQuery 不会在视图中显示评论:

bq 查询不在查询顶部显示评论

Bigquery 正在跳过“----这是我的重要评论。这将是一个冗长而广泛的评论。” 因为它被视为对 SQL 查询的注释,而不是被视为要包含在视图创建中的单独字符串。

另一种选择是使用bq命令,如下所示。

bq mk \
--use_legacy_sql=false \
--expiration 3600 \
--description "This is my view" \
--label organization:development \
--view \
'-- this is my important comment. This will be a long and extensive comment.
SELECT 1 as column_a ' \
your-dataset.your-view

我的示例输出: 在此处输入图像描述

我不知道如何在 UI 中执行此操作,但使用 python API 可以执行以下操作:

from google.cloud import bigquery

bq_client = bigquery.Client()

view_id = "my_project_id.my_dataset.my_view_name"
view = bigquery.Table(view_id)

query = """
-- this is my important comment. This will be a long and extensive comment.
SELECT 1 as column_a
"""

view.view_use_legacy_sql = False
view.view_query = query

# if your view already exists
bq_client.delete_table(view)

# your query will now show the comment at the top
bq_client.create_table(view)

这导致以下视图:

使用 python 创建的 bigquery 视图,顶部有注释

另请参阅: https ://cloud.google.com/bigquery/docs/views#python

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM