Sunday, 22 June 2025

Quickly Explore Table Schema using bq CLI

The "bq show –schema" command is part of the bq CLI tool, used to display only the schema of a specific BigQuery table.

 

This is especially useful when you:

·      Want a quick overview of columns and data types.

·      Need to validate the structure of a table before writing queries.

·      Are scripting or automating metadata checks.

 

Syntax 

bq show --schema [PROJECT_ID].[DATASET].[TABLE]

 

Example with Full Resource Name

bq show --schema my_project.my_dataset.my_table

Omitting the Project ID

If you’ve already set the default project via gcloud, the command becomes simpler:

 

gcloud config set project my_project
bq show --schema my_dataset.my_table

This improves readability and reduces human error, especially in scripts.

 

$bq show --schema test_dataset.employees
[{"name":"EmployeeID","type":"INTEGER","mode":"NULLABLE"},{"name":"FirstName","type":"STRING","mode":"NULLABLE"},{"name":"LastName","type":"STRING","mode":"NULLABLE"},{"name":"Department","type":"STRING","mode":"NULLABLE"},{"name":"Salary","type":"INTEGER","mode":"NULLABLE"}]

Pipe the output through tools like jq for better formatting:

$bq show --schema test_dataset.employees | jq
[
  {
    "name": "EmployeeID",
    "type": "INTEGER",
    "mode": "NULLABLE"
  },
  {
    "name": "FirstName",
    "type": "STRING",
    "mode": "NULLABLE"
  },
  {
    "name": "LastName",
    "type": "STRING",
    "mode": "NULLABLE"
  },
  {
    "name": "Department",
    "type": "STRING",
    "mode": "NULLABLE"
  },
  {
    "name": "Salary",
    "type": "INTEGER",
    "mode": "NULLABLE"
  }
]

 

 

Previous                                                    Next                                                    Home

No comments:

Post a Comment