有没有办法将 BigQuery 表的架构导出为 JSON?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/43195143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 18:49:27  来源:igfitidea点击:

Is there a way to export a BigQuery table's schema as JSON?

jsongoogle-bigquery

提问by Daniel Waechter

A BigQuery tablehas schema which can be viewed in the web UI, updated, or used to load datawith the bqtool as a JSON file. However, I can't find a way to dump this schema from an existing table to a JSON file (preferably from the command-line). Is that possible?

BigQuery具有可在 Web UI 中查看、更新或用于使用该工具将数据加载bq为 JSON 文件的架构。但是,我找不到将此架构从现有表转储到 JSON 文件(最好从命令行)的方法。那可能吗?

回答by Mikhail Berlyant

a way to dump schema from an existing table to a JSON file (preferably from the command-line). Is that possible?

一种将模式从现有表转储到 JSON 文件的方法(最好从命令行)。那可能吗?

try below

试试下面

bq show bigquery-public-data:samples.wikipedia  

You can use –format flag to prettify output

您可以使用 –format 标志来美化输出

--format: none|json|prettyjson|csv|sparse|pretty:

--格式:无|json|prettyjson|csv|稀疏|漂亮:

Format for command output. Options include:

命令输出的格式。选项包括:

none:       ...
pretty:     formatted table output  
sparse:     simpler table output  
prettyjson: easy-to-read JSON format  
json:       maximally compact JSON  
csv:        csv format with header   

The first three are intended to be human-readable, and the latter three are for passing to another program. If no format is selected, one will be chosen based on the command run.

前三个用于人类可读,后三个用于传递给另一个程序。如果没有选择格式,将根据命令运行选择一种格式。

Realized I provided partial answer :o)

意识到我提供了部分答案:o)

Below does what PO wanted

下面做 PO 想要的

bq show --format=prettyjson bigquery-public-data:samples.wikipedia | jq '.schema.fields' 

回答by bsmarcosj

You can add the flag --schema[1] in order to avoid table data information.

您可以添加标志--schema[1] 以避免表数据信息。

bq show --schema --format=prettyjson [PROJECT_ID]:[DATASET].[TABLE] > [SCHEMA_FILE]

bq show --schema --format=prettyjson mydataset.mytable > /tmp/myschema.json

[1] https://cloud.google.com/bigquery/docs/managing-table-schemas

[1] https://cloud.google.com/bigquery/docs/managing-table-schemas