Skip to content

Instantly share code, notes, and snippets.

@ceoro9
Created April 29, 2019 14:44
Show Gist options
  • Save ceoro9/652a0fd4f5976be00f9f703bd99c9799 to your computer and use it in GitHub Desktop.
Save ceoro9/652a0fd4f5976be00f9f703bd99c9799 to your computer and use it in GitHub Desktop.
Insert data to big query
package main
import (
"fmt"
"context"
"cloud.google.com/go/bigquery"
)
type Item struct {
EventName string
PostUUID string
}
func (i *Item) Save() (map[string]bigquery.Value, string, error) {
return map[string]bigquery.Value{
"event_name": i.EventName,
"post_uuid": i.PostUUID,
}, "", nil
}
func main() {
ctx := context.Background()
client, err := bigquery.NewClient(ctx, "project_id")
if err != nil {
fmt.Println(err)
return
}
u := client.Dataset("dataset_name").Table("table_name").Uploader()
items := []*Item{
{EventName: "START_UPLOADING", PostUUID: "298dd3ed-3834-459e-9299-965243704ff3"},
}
if err = u.Put(ctx, items); err != nil {
fmt.Println("error")
fmt.Println(err)
return
}
fmt.Println("success")
}
@ceoro9
Copy link
Author

ceoro9 commented Apr 29, 2019

Authentication

export GOOGLE_APPLICATION_CREDENTIALS=[path_to_creds_file]

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