Echo
Echo

Reputation: 405

Add column description to BiqQuery table?

need to add descriptions to each column of a BigQuery table, seems I can do it manually, how to do it programmatically?

Upvotes: 11

Views: 17876

Answers (3)

krishna mahesh dalli
krishna mahesh dalli

Reputation: 89

BigQuery now supports ALTER COLUMN SET OPTIONS statement, which can be used to update the description of a column

example:

ALTER TABLE mydataset.mytable
ALTER COLUMN price
SET OPTIONS (
  description="Price per unit"
)

Documentation:

https://cloud.google.com/bigquery/docs/reference/standard-sql/data-definition-language#alter_column_set_options_statement

Upvotes: 8

Jordan Tigani
Jordan Tigani

Reputation: 26617

As Adam mentioned, you can use the table PATCH method on the API to update the schema columns. The other method is to use bq.

You can first get the schema by doing the following:

1: Get the JSON schema:

TABLE=publicdata:samples.shakespeare

bq show --format=prettyjson ${TABLE} > table.txt

Then copy the schema from table.txt to schema.txt ... it will look something like:

[
  {
    "description": "A single unique word (where whitespace is the delimiter) extracted from a corpus.",
    "mode": "REQUIRED",
    "name": "word",
    "type": "STRING"
  },
  {
    "description": "The number of times this word appears in this corpus.",
    "mode": "REQUIRED",
    "name": "word_count",
    "type": "INTEGER"
  },
  ....
]

2: Set the description field to whatever you want (if it is not there, add it).

3: Tell BigQuery to update the schema with the added columns. Note that schema.txt must contain the complete schema.

 bq update --schema schema.txt -t ${TABLE}

Upvotes: 7

Adam
Adam

Reputation: 5985

You can use the REST API to create or update a table, and specify a field desciption (schema.fields[].description) in your schema.

https://cloud.google.com/bigquery/docs/reference/v2/tables#methods

Upvotes: 0

Related Questions