Skip to content

Instantly share code, notes, and snippets.

@brunopk
Created July 11, 2022 18:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save brunopk/04afbba26b5d24aecc7777c7c325792d to your computer and use it in GitHub Desktop.
Save brunopk/04afbba26b5d24aecc7777c7c325792d to your computer and use it in GitHub Desktop.
Bigquery

Set project

gcloud config set project project_name

Show table information:

bq show dataset.table

or:

bq show --format=prettyjson dataset.table

to get information in JSON format.

Creating a table from a JSON

bq mk -t \
  --schema 'schema.json' \
  --time_partitioning_field partitioned_field \
  --time_partitioning_type DAY \
  --clustering_fields field_c \
  project:dataset.table

The schema should be a JSON list:

[{
  "description": "description",
  "maxLength": "150",
  "mode": "REQUIRED",
  "name": "field_c",
  "type": "STRING"
}, {
  "description": "description",
  "mode": "REQUIRED",
  "name": "partitioned_field",
  "type": "TIMESTAMP"
}]

Copying tables

First set the project:

gcloud config set project project_name
bq query --destination_table dataset.destination_table \
    --use_legacy_sql=false \
    'SELECT * FROM other_project.other_dataset.other_table'

More information

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment