BigQuery - 获取BigQuery表中的列总数

时间:2015-05-20 22:23:50

标签: google-bigquery

有没有办法查询BigQuery表中的总列数?我浏览了BigQuery文档,但没有发现任何相关内容。

提前致谢!

7 个答案:

答案 0 :(得分:4)

有几种方法可以做到这一点:

一个。使用BQ命令行工具和JQ linux库来解析JSON。

bq --format=json show publicdata:samples.shakespeare | jq '.schema.fields | length'

这个结果:

4

B中。使用REST api进行Tables:get调用

GET https://www.googleapis.com/bigquery/v2/projects/projectId/datasets/datasetId/tables/tableId

这将返回一个完整的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"
         }
      ]
   }
}

答案 1 :(得分:4)

使用SQL查询和内置的INFORMATION_SCHEMA表:

SELECT count(distinct column_name) 
FROM  `project_id`.name_of_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = "name_of_table"

答案 2 :(得分:1)

这很有用

#standardSQL
with table1 as(
select "somename1" as name, "someaddress1" adrs union all
select "somename2" as name, "someaddress2" adrs union all
select "somename3" as name, "someaddress3" adrs
)
select  array_length(regexp_extract_all(to_json_string(table1),"\":"))total_columns from table1 limit 1

答案 3 :(得分:0)

这是一个不需要JQ的替代方案,但是它的成本更高,而且成本更高。" ; - ):

bq --format=csv query "select * FROM publicdata:samples.shakespeare LIMIT 1"|tail -n1|sed 's/[^,]//g' | wc -c

注意:我怀疑这适用于包含多个重复/嵌套列的表。

答案 4 :(得分:0)

只需添加一个片段即可在python中获取架构:

from gcloud import bigquery

client = bigquery.Client(project="project_id")
dataset = client.list_datasets()
flag=0
for ds in dataset[0]:
    if flag==1:
        break
    if ds.name==<<dataset_name>>:
        for table in ds.list_tables()[0]:
            if table.name==<<table_name>>:
                table.reload()
                no_columns = len(table.schema)
                flag=1
                break

no_columns变量包含所需表的列长度。

答案 5 :(得分:0)

在node.js中,我使用以下代码来获取长度:

const { BigQuery } = require('@google-cloud/bigquery');

var params= {bq_project_id : "my_project_id"};//YOUR PROJECT ID
params.bq_dataset_id = "my_dataset_id"; //YOUR DATASET ID
params.bq_table_id = "my_table_id"; //YOUR TABLE ID
params.bq_keyFilename = './my_bq_key.json';//YOUR KEY PATH

const bigquery = new BigQuery({
    projectId: params.bq_project_id,
    keyFilename: params.bq_keyFilename,
});
async function colNums() {
    let resp = await bigquery.dataset(params.bq_dataset_id).table(params.bq_table_id).get();
    console.log(resp[1].schema.fields.length)
}
colNums();

我不确定“ resp [1]”是否适用于所有人(如果遇到问题,请尝试查看resp对象)

答案 6 :(得分:0)

您现在可以使用INFORMATION_SCHEMA-一系列视图,这些视图提供对有关数据集,表和视图的元数据的访问权限

例如

SELECT * EXCEPT(is_generated, generation_expression, is_stored, is_updatable)
FROM `bigquery-public-data.hacker_news.INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = 'stories'

在需要RECORD(或STRUCT)列中的所有嵌套字段时,INFORMATION_SCHEMA.COLUMN_FIELD_PATHS视图也很有用。