如何在bigquery中从视图中获取保存的查询

问题描述 投票:1回答:1

使用Python API,有没有办法从Google Big Query检索当前保存的视图查询?我知道我可以获取视图元数据,但我需要能够保存实际查询。谢谢。

python python-3.x google-bigquery
1个回答
2
投票

您无需获取创建视图的作业的详细信息。有一个更简单的方法。只需获取Table对象并检查其view_query属性。

[..]
client = bigquery.Client('grey-sort-challenge')
dataset = client.dataset('apps_script_scheduler')
tables = list(client.list_dataset_tables(dataset))
for t in tables:
  table = client.get_table(t)
  print 'Table type: %s, SQL: %s' % (table.table_type, table.view_query)
[..]

输出(对于包含2个本机表和视图的数据集)

Table type: TABLE, SQL: None
Table type: TABLE, SQL: None
Table type: VIEW, SQL: SELECT
  SUM(views) AS total_views,
  title,
  LANGUAGE
FROM
  `bigquery-samples.wikipedia_benchmark.Wiki1M`
WHERE
  title LIKE '%Melbourne%'
GROUP BY
  title,
  LANGUAGE
ORDER BY
  total_views DESC;

文件 - > https://googlecloudplatform.github.io/google-cloud-python/latest/bigquery/reference.html#google.cloud.bigquery.table.Table.view_query

© www.soinside.com 2019 - 2024. All rights reserved.