Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Google bigquery BigQuery-获取BigQuery表中的列总数_Google Bigquery - Fatal编程技术网

Google bigquery BigQuery-获取BigQuery表中的列总数

Google bigquery BigQuery-获取BigQuery表中的列总数,google-bigquery,Google Bigquery,有没有办法查询BigQuery表中的总列数?我浏览了BigQuery文档,但没有发现任何相关内容 提前谢谢 有几种方法可以做到这一点: A.使用命令行工具和linux库解析JSON bq --format=json show publicdata:samples.shakespeare | jq '.schema.fields | length' 这表明: 4 B.使用RESTAPI进行调用 这将返回一个完整的JSON,您可以解析和查询schema.field长度 { "kind":"

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


提前谢谢

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

A.使用命令行工具和linux库解析JSON

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

4
B.使用RESTAPI进行调用

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

这里有一个不需要JQ的替代方案,但“成本”稍高一点:-):


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

只需添加一个片段即可获得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
从gcloud导入bigquery
client=bigquery.client(project=“project\u id”)
dataset=client.list_datasets()
标志=0
对于数据集[0]中的ds:
如果标志==1:
打破
如果ds.name==:
对于ds.list_tables()中的表[0]:
如果table.name==:
表1.reload()
no_columns=len(table.schema)
标志=1
打破
no_columns变量包含所需表的列长度。

这将非常有用

#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

在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对象)

您现在可以使用-一系列视图,提供对数据集、表和视图元数据的访问

比如说

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

当您需要记录(或结构)列中的所有嵌套字段时,视图也很有用

使用SQL查询和内置信息\u架构表:

SELECT count(distinct column_name) 
FROM  `project_id`.name_of_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = "name_of_table"
SELECT * EXCEPT(is_generated, generation_expression, is_stored, is_updatable)
FROM `bigquery-public-data.hacker_news.INFORMATION_SCHEMA.COLUMNS`
WHERE table_name = 'stories'
SELECT count(distinct column_name) 
FROM  `project_id`.name_of_dataset.INFORMATION_SCHEMA.COLUMNS
WHERE table_name = "name_of_table"