0

Get Table Schema

by
Published today

Get a BigQuery table's column schema, size and partitioning.

Script bigquery Verified

The script

Submitted by hugo989 Bun
Verified 4 hours ago
1
import { BigQuery } from "@google-cloud/bigquery"
2

3
export type DynSelect_dataset_id = string
4

5
// Dropdown of the datasets in the service account's project.
6
export async function dataset_id(auth: RT.Bigquery) {
7
  const [datasets] = await client(auth).getDatasets()
8
  return datasets.map((d) => ({ value: d.id!, label: d.id! }))
9
}
10

11
function client(auth: RT.Bigquery) {
12
  return new BigQuery({ credentials: auth, projectId: auth.project_id })
13
}
14

15
/**
16
 * Get Table Schema
17
 * Get a BigQuery table's columns (name, type, mode, description, nested fields) along with its row count, size and partitioning.
18
 */
19
export async function main(
20
  auth: RT.Bigquery,
21
  dataset_id: DynSelect_dataset_id,
22
  table_id: string
23
) {
24
  const [metadata] = await client(auth)
25
    .dataset(dataset_id)
26
    .table(table_id)
27
    .getMetadata()
28
  return {
29
    table_id: metadata.tableReference.tableId,
30
    type: metadata.type,
31
    schema: metadata.schema,
32
    num_rows: metadata.numRows,
33
    num_bytes: metadata.numBytes,
34
    time_partitioning: metadata.timePartitioning,
35
    clustering: metadata.clustering,
36
    description: metadata.description,
37
  }
38
}
39