Bigquery query to find the column names of a table

前端 未结 6 803
误落风尘
误落风尘 2021-01-17 08:59

I need a query to find column names of a table (table metadata) in Bigquery, like the following query in SQL:

SELECT column_name,data_type,data_length,data_p         


        
相关标签:
6条回答
  • 2021-01-17 09:25

    Yes you can get table metadata using INFORMATION_SCHEMA.

    One of the examples mentioned in the past link retrieves metadata from the INFORMATION_SCHEMA.COLUMN_FIELD_PATHS view for the commits table in the github_repos dataset, you just have to

    1. Open the BigQuery web UI in the GCP Console.

    2. Enter the following standard SQL query in the Query editor box. INFORMATION_SCHEMA requires standard SQL syntax. Standard SQL is the default syntax in the GCP Console.

      SELECT
       *
      FROM
       `bigquery-public-data`.github_repos.INFORMATION_SCHEMA.COLUMN_FIELD_PATHS
      WHERE
       table_name="commits"
       AND column_name="author"
       OR column_name="difference"
      

    Note: INFORMATION_SCHEMA view names are case-sensitive.

    1. Click Run.

    The results should look like the following

      +------------+-------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+-------------+
      | table_name | column_name |     field_path      |                                                                      data_type                                                                      | description |
      +------------+-------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+-------------+
      | commits    | author      | author              | STRUCT<name STRING, email STRING, time_sec INT64, tz_offset INT64, date TIMESTAMP>                                                                  | NULL        |
      | commits    | author      | author.name         | STRING                                                                                                                                              | NULL        |
      | commits    | author      | author.email        | STRING                                                                                                                                              | NULL        |
      | commits    | author      | author.time_sec     | INT64                                                                                                                                               | NULL        |
      | commits    | author      | author.tz_offset    | INT64                                                                                                                                               | NULL        |
      | commits    | author      | author.date         | TIMESTAMP                                                                                                                                           | NULL        |
      | commits    | difference  | difference          | ARRAY<STRUCT<old_mode INT64, new_mode INT64, old_path STRING, new_path STRING, old_sha1 STRING, new_sha1 STRING, old_repo STRING, new_repo STRING>> | NULL        |
      | commits    | difference  | difference.old_mode | INT64                                                                                                                                               | NULL        |
      | commits    | difference  | difference.new_mode | INT64                                                                                                                                               | NULL        |
      | commits    | difference  | difference.old_path | STRING                                                                                                                                              | NULL        |
      | commits    | difference  | difference.new_path | STRING                                                                                                                                              | NULL        |
      | commits    | difference  | difference.old_sha1 | STRING                                                                                                                                              | NULL        |
      | commits    | difference  | difference.new_sha1 | STRING                                                                                                                                              | NULL        |
      | commits    | difference  | difference.old_repo | STRING                                                                                                                                              | NULL        |
      | commits    | difference  | difference.new_repo | STRING                                                                                                                                              | NULL        |
      +------------+-------------+---------------------+-----------------------------------------------------------------------------------------------------------------------------------------------------+-------------+
    
    0 讨论(0)
  • 2021-01-17 09:27

    Update: This is now possible! See the INFORMATION SCHEMA docs and the answers below.

    Answer, circa 2012:

    It's not currently possible to retrieve table metadata (i.e. column names and types) via a query, though this isn't the first time it's been requested.

    Is there a reason you need to do this as a query? Table metadata is available via the tables API.

    0 讨论(0)
  • 2021-01-17 09:29

    For newbies like me, the above is of the following syntax:

    select * from project_name.dataset_name.INFORMATION_SCHEMA.COLUMNS where table_catalog=project_name and table_schema=dataset_name and table_name=table_name
    
    0 讨论(0)
  • 2021-01-17 09:36

    To Check column, You can access your table Through CLI Easy and simple to find

    bq query --use_legacy_sql=false 'select Hour, sum(column 1) as column from `project_id.dataset.table_name` where Date(Hour) = '2020-06-10';'
    
    0 讨论(0)
  • 2021-01-17 09:41

    BigQuery now supports information schema.

    Suppose you have a dataset named MY_PROJECT.MY_DATASET and a table named MY_TABLE, then you can run the following query:

    SELECT column_name
    FROM MY_PROJECT.MY_DATASET.INFORMATION_SCHEMA.COLUMNS
    WHERE table_name = 'MY_TABLE'
    
    0 讨论(0)
  • 2021-01-17 09:49

    Actually it is possible to do so using SQL. To do so you need to query the logging table for the last log of this particular table being created.

    For example, assuming the table is loaded/created daily:

        CREATE TEMP FUNCTION jsonSchemaStringToArray(jsonSchema String)
              RETURNS ARRAY<STRING> AS ((
                SELECT
                  SPLIT(
                    REGEXP_REPLACE(REPLACE(LTRIM(jsonSchema,'{ '),'"fields": [',''), r'{[^{]+"name": "([^\"]+)"[^}]+}[, ]*', '\\1,')
                  ,',')
              ));
        WITH valid_schema_columns AS (
          WITH array_output aS (SELECT
            jsonSchemaStringToArray(jsonSchema) AS column_names
          FROM (
            SELECT
              protoPayload.serviceData.jobInsertRequest.resource.jobConfiguration.load.schemaJson AS jsonSchema
              , ROW_NUMBER() OVER (ORDER BY metadata.timestamp DESC) AS record_count
            FROM `realself-main.bigquery_logging.cloudaudit_googleapis_com_data_access_20170101`
            WHERE
              protoPayload.serviceData.jobInsertRequest.resource.jobConfiguration.load.destinationTable.tableId = '<table_name>'
              AND
              protoPayload.serviceData.jobInsertRequest.resource.jobConfiguration.load.destinationTable.datasetId = '<schema_name>'
              AND
              protoPayload.serviceData.jobInsertRequest.resource.jobConfiguration.load.createDisposition = 'CREATE_IF_NEEDED'
          ) AS t
          WHERE
            t.record_count = 1 -- grab the latest entry
          )
          -- this is actually what UNNESTS the array into standard rows
          SELECT
            valid_column_name
          FROM array_output
          LEFT JOIN UNNEST(column_names) AS valid_column_name
    
        )
    
    0 讨论(0)
提交回复
热议问题