Created
April 11, 2022 20:58
-
-
Save stephen-soltesz/48bae5fb32eb57949c684dd54089430b to your computer and use it in GitHub Desktop.
Example updating BigQuery view or table descriptions using an M-Lab YAML description file.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
"flag" | |
"io/ioutil" | |
"cloud.google.com/go/bigquery" | |
"github.com/m-lab/go/cloud/bqx" | |
"github.com/m-lab/go/flagx" | |
"github.com/m-lab/go/rtx" | |
) | |
var ( | |
view string | |
project string | |
dataset string | |
schemas = flagx.StringArray{} | |
ctx = context.Background() | |
) | |
func init() { | |
flag.StringVar(&view, "view", "", "name of the view in bigquery") | |
flag.StringVar(&project, "project", "", "GCP project") | |
flag.StringVar(&dataset, "dataset", "", "Dataset name within project") | |
flag.Var(&schemas, "yaml", "Name of yaml description files to merge and apply to named view or table") | |
} | |
func main() { | |
flag.Parse() | |
// Create bigquery client for given project. | |
c, err := bigquery.NewClient(ctx, project) | |
rtx.Must(err, "failed to get client.") | |
// Get reference to view/table and get metadata for it. | |
t := c.Dataset(dataset).Table(view) | |
md, err := t.Metadata(ctx) | |
rtx.Must(err, "failed to read table metadata") | |
b, err := ioutil.ReadFile(schemas[0]) // TODO: combine all schema docs. | |
rtx.Must(err, "failed to read ") | |
doc := bqx.NewSchemaDoc(b) | |
// Update the md.Schema in-place using the local docs. | |
bqx.UpdateSchemaDescription(md.Schema, doc) | |
// Update the original view/table descriptions using the modified schema. | |
_, err = t.Update(ctx, bigquery.TableMetadataToUpdate{Schema: md.Schema}, md.ETag) | |
rtx.Must(err, "failed to update view schema") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment