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

后端 未结 3 1168
走了就别回头了
走了就别回头了 2020-12-22 23:24

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 du

相关标签:
3条回答
  • 2020-12-22 23:59

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

    try below

    bq show bigquery-public-data:samples.wikipedia  
    

    You can use –format flag to prettify output

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

    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)

    Below does what PO wanted

    bq show --format=prettyjson bigquery-public-data:samples.wikipedia | jq '.schema.fields' 
    
    0 讨论(0)
  • 2020-12-23 00:16

    You can use REST API call to get BigQuery table schema as JSON. Documentation link: https://cloud.google.com/bigquery/docs/reference/rest/v2/tables/get

    curl 'https://bigquery.googleapis.com/bigquery/v2/projects/project-name/datasets/dataset-name/tables/table-name' \
         --header 'Authorization: Bearer [YOUR_ACCESS_TOKEN]' \
         --header 'Accept: application/json' \
         --compressed
    
    0 讨论(0)
  • 2020-12-23 00:22

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

    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

    0 讨论(0)
提交回复
热议问题