forked fromeinride/protobuf-bigquery-go
- Notifications
You must be signed in to change notification settings - Fork0
Seamlessly save and load protocol buffers to and from BigQuery using Go.
License
NotificationsYou must be signed in to change notification settings
ThreeDotsLabs/protobuf-bigquery-go
Folders and files
| Name | Name | Last commit message | Last commit date | |
|---|---|---|---|---|
Repository files navigation
Seamlessly save and load protocol buffers to and from BigQuery using Go.
This library provides add-ons tocloud.google.com/bigquery forfirst-class protobuf support usingprotobuf reflection.
$ go get -u go.einride.tech/protobuf-bigquery
BigQuery schema inference for arbitrary protobuf messages.
funcExampleInferSchema() {msg:=&library.Book{}schema:=protobq.InferSchema(msg)expected:= bigquery.Schema{{Name:"name",Type:bigquery.StringFieldType},{Name:"author",Type:bigquery.StringFieldType},{Name:"title",Type:bigquery.StringFieldType},{Name:"read",Type:bigquery.BooleanFieldType},}fmt.Println(cmp.Equal(expected,schema))// Output: true}
An implementation ofbigquery.ValueSaverthat saves arbitrary protobuf messages to BigQuery.
funcExampleMessageSaver() {ctx:=context.Background()// Write protobuf messages to a BigQuery table.projectID:=flag.String("project","","BigQuery project to write to.")datasetID:=flag.String("dataset","","BigQuery dataset to write to.")tableID:=flag.String("table","","BigQuery table to write to.")create:=flag.Bool("create",false,"Flag indicating whether to create the table.")flag.Parse()// Connect to BigQuery.client,err:=bigquery.NewClient(ctx,*projectID)iferr!=nil {panic(err)// TODO: Handle error.}table:=client.Dataset(*datasetID).Table(*tableID)// Create the table by inferring the BigQuery schema from the protobuf schema.if*create {iferr:=table.Create(ctx,&bigquery.TableMetadata{Schema:protobq.InferSchema(&publicv1.FilmLocation{}),});err!=nil {panic(err)// TODO: Handle error.}}// Insert the protobuf messages.inserter:=table.Inserter()fori,filmLocation:=range []*publicv1.FilmLocation{{Title:"Dark Passage",ReleaseYear:1947,Locations:"Filbert Steps"},{Title:"D.O.A",ReleaseYear:1950,Locations:"Union Square"},{Title:"Flower Drum Song",ReleaseYear:1961,Locations:"Chinatown"},} {iferr:=inserter.Put(ctx,&protobq.MessageSaver{Message:filmLocation,InsertID:strconv.Itoa(i),// include an optional insert ID});err!=nil {panic(err)// TODO: Handle error.}}}
An implementation ofbigquery.ValueLoaderthat loads arbitrary protobuf messages from BigQuery.
funcExampleMessageLoader() {ctx:=context.Background()// Read from the public "film locations" BigQuery dataset into a proto message.const (project="bigquery-public-data"dataset="san_francisco_film_locations"table="film_locations")// Connect to BigQuery.client,err:=bigquery.NewClient(ctx,project)iferr!=nil {panic(err)// TODO: Handle error.}// Load BigQuery rows into a FilmLocation message.messageLoader:=&protobq.MessageLoader{Message:&publicv1.FilmLocation{},}// Iterate rows in table.rowIterator:=client.Dataset(dataset).Table(table).Read(ctx)for {// Load next row into the FilmLocation message.iferr:=rowIterator.Next(messageLoader);err!=nil {iferrors.Is(err,iterator.Done) {break}panic(err)// TODO: Handle error.}// Print the message.fmt.Println(prototext.Format(messageLoader.Message))}}
| Protobuf | BigQuery |
|---|---|
| google.protobuf.Timestamp | TIMESTAMP |
| google.protobuf.Duration | FLOAT (seconds) |
| google.protobuf.DoubleValue | FLOAT |
| google.protobuf.FloatValue | FLOAT |
| google.protobuf.Int32Value | INTEGER |
| google.protobuf.Int64Value | INTEGER |
| google.protobuf.Uint32Value | INTEGER |
| google.protobuf.Uint64Value | INTEGER |
| google.protobuf.BoolValue | BOOLEAN |
| google.protobuf.StringValue | STRING |
| google.protobuf.BytesValue | BYTES |
| google.protobuf.StructValue | STRING (JSON) |
| Protobuf | BigQuery |
|---|---|
| google.type.Date | DATE |
| google.type.DateTime | RECORD (or DATETIME) |
| google.type.LatLng | GEOGRAPHY |
| google.type.TimeOfDay | TIME |
About
Seamlessly save and load protocol buffers to and from BigQuery using Go.
Resources
License
Code of conduct
Security policy
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Releases
No releases published
Packages0
No packages published
Uh oh!
There was an error while loading.Please reload this page.
Languages
- Go97.1%
- Makefile2.9%