BigQuery - Get the total number of columns in a BigQuery table
There are a couple of ways to do this:
A. Using the BQ command line tool, and the JQ linux library to parse JSON.
bq --format=json show publicdata:samples.shakespeare | jq '.schema.fields | length'
This outpus:
4
B. Using the REST api to do a Tables:get call
GET https://www.googleapis.com/bigquery/v2/projects/projectId/datasets/datasetId/tables/tableId
this returns a full JSON, that you can parse and query for schema.field length.
{
"kind":"bigquery#table",
"description":"This dataset is a word index of the works of Shakespeare, giving the number of times each word appears in each corpus.",
"creationTime":"1335916045099",
"tableReference":{
"projectId":"publicdata",
"tableId":"shakespeare",
"datasetId":"samples"
},
"numRows":"164656",
"numBytes":"6432064",
"etag":"\"E7ZNanj79wmDHI9DmeCWoYoUpAE/MTQxMzkyNjgyNzI1Nw\"",
"lastModifiedTime":"1413926827257",
"type":"TABLE",
"id":"publicdata:samples.shakespeare",
"selfLink":"https://www.googleapis.com/bigquery/v2/projects/publicdata/datasets/samples/tables/shakespeare",
"schema":{
"fields":[
{
"description":"A single unique word (where whitespace is the delimiter) extracted from a corpus.",
"type":"STRING",
"name":"word",
"mode":"REQUIRED"
},
{
"description":"The number of times this word appears in this corpus.",
"type":"INTEGER",
"name":"word_count",
"mode":"REQUIRED"
},
{
"description":"The work from which this word was extracted.",
"type":"STRING",
"name":"corpus",
"mode":"REQUIRED"
},
{
"description":"The year in which this corpus was published.",
"type":"INTEGER",
"name":"corpus_date",
"mode":"REQUIRED"
}
]
}
}
Using SQL query & built-in INFORMATION_SCHEMA tables:
SELECT count(distinct column_name)
FROM `project_id`.name_of_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = "name_of_table"