【问题标题】:Can we get Column Name from specific Table in Google BigQuery?我们可以从 Google BigQuery 中的特定表中获取列名吗?
【发布时间】:2013-12-25 14:14:53
【问题描述】:

我们可以从 Google BigQuery 中的特定表中获取列名吗?

让我知道对此活动的查询。

我试过了,但没有得到结果...

  SELECT column_name FROM publicdata:samples.shakespeare
  OR
  SELECT schema FROM publicdata:samples.shakespeare

【问题讨论】:

  • 所以你想做一些类似 SELECT column_name FROM all_tables where table = 'shakespeare' and schema = 'publicdata:samples' 的事情?

标签: google-bigquery


【解决方案1】:

1.您可以使用命令行工具(https://developers.google.com/bigquery/bq-command-line-tool#gettable): bq 秀:。

$ bq show publicdata:samples.shakespeare
tableId      Last modified                  Schema
------------- ----------------- ------------------------------------
shakespeare   01 Sep 13:46:28   |- word: string (required)
                              |- word_count: integer (required)
                              |- corpus: string (required)
                              |- corpus_date: integer (required)

2.BigQuery 浏览器工具:https://developers.google.com/bigquery/bigquery-browser-tool#examineschema

3.或者使用BigQuery API:https://developers.google.com/bigquery/docs/reference/v2/tables/get

【讨论】:

【解决方案2】:

我使用 Java 得到了结果:

Tables tableRequest = bigquery.tables();
Table table = tableRequest.get(projectName,datasetName,tableName).execute();
List<TableFieldSchema> fields = table.getSchema().getFields();

【讨论】:

    【解决方案3】:

    使用INFORMATION_SCHEMA 使用 SQL 获取列名:

    SELECT column_name, data_type
    FROM `bigquery-public-data.samples.INFORMATION_SCHEMA.COLUMNS` 
    WHERE table_name = 'shakespeare'
    

    它给你:

    +-------------+-----------+
    | column_name | data_type |
    +-------------+-----------+
    | word        | STRING    |
    | word_count  | INT64     |
    | corpus      | STRING    |
    | corpus_date | INT64     |
    +-------------+-----------+
    

    【讨论】:

      【解决方案4】:

      在 Jupyter 中使用 python 的示例:

      SERVICE_ACCOUNT = 'sa_bq.json'
      
      !pip install google-cloud
      !pip install google-api-python-client
      !pip install oauth2client    
      
      from google.cloud import bigquery
      
      client_bq = bigquery.Client.from_service_account_json(SERVICE_ACCOUNT)
      table = client_bq.get_table('bigquery-public-data.samples.shakespeare')
      print(list(c.name for c in table.schema))
      

      【讨论】:

      • 自最新库版本以来的更新,以使用该表:&gt;&gt; tr = bigquery.table.TableReference.from_string('bigquery-public-data.samples.shakespeare') &gt;&gt; table = client_bq.get_table(tr)
      【解决方案5】:

      无需任何查询,在经典 UI 上,您可以进行如下操作:

      • 点击左侧面板上的蓝色向下箭头
      • 切换到项目,然后显示项目...
      • 在项目 ID 上,写下项目的名称(在你的情况下,你有 publicdata:samples.shakespeare,你的项目是 publicdata
      • 现在,该项目出现在左侧面板上
      • 选择数据集(在您的情况下是样本)
      • 选择表格(在您的情况下是莎士比亚)
      • 最后,您应该在屏幕中间看到三个选项卡:Schema、Details、Preview。

      【讨论】:

        【解决方案6】:

        如果我对您的理解正确,您希望执行 tables.list 或 tables.get 而不是 jobs.query。

        这是它在谷歌应用脚​​本中的工作方式:

        var results = BigQuery.Tables.list(projectId, datasetId, optionalArgs);
        

        或通过 API:

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

        https://developers.google.com/bigquery/docs/reference/v2/tables/list

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

        https://developers.google.com/bigquery/docs/reference/v2/tables/get

        否则,您可以像这样查询 SELECT * FROM [] limit 0 并编写一些查看列名的过程。

        【讨论】:

        • 我虽然是选择限制 1 并且也只使用列名,但我得到的错误是“使用此查询,您将为表中的所有数据付费(即使您的查询包含一个 LIMIT 子句)。”
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-10-19
        • 2023-03-16
        • 1970-01-01
        • 2021-04-05
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多