是否有办法将BigQuery表的模式导出为JSON?

时间:2021-07-06 15:29:18

A BigQuery table has schema which can be viewed in the web UI, updated, or used to load data with the bq tool 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 table具有模式,可以在web UI中查看、更新或使用bq工具作为JSON文件加载数据。但是,我无法找到将这个模式从现有表转储到JSON文件(最好是从命令行)的方法。这有可能吗?

2 个解决方案

#1


28  

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' 

#2


2  

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

您可以添加标志——模式[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

#1


28  

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' 

#2


2  

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

您可以添加标志——模式[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