小智 7
使用 SQL 查询和内置 INFORMATION_SCHEMA 表:
SELECT count(distinct column_name)
FROM `project_id`.name_of_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = "name_of_table"
Run Code Online (Sandbox Code Playgroud)
有两种方法可以做到这一点:
bq --format=json show publicdata:samples.shakespeare | jq '.schema.fields | length'
Run Code Online (Sandbox Code Playgroud)
这个结果:
4
Run Code Online (Sandbox Code Playgroud)
B.使用REST API进行Tables:get调用
GET https://www.googleapis.com/bigquery/v2/projects/projectId/datasets/datasetId/tables/tableId
Run Code Online (Sandbox Code Playgroud)
这将返回完整的JSON,您可以解析和查询schema.field长度。
{
"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"
}
]
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2622 次 |
| 最近记录: |