Arben John Avillanosa
Arben John Avillanosa

Reputation: 213

Inserting query results to an existing BigQuery Table

I have some concern regarding the QueryConfig in Go. It says that:

WriteDisposition specifies how existing data in the destination table is treated. By default is WriteEmpty.

So I assume whenever the destination table exists it automatically appends the result into the existing table instead of creating a new table. However, on my side it creates a new table which is going to raise the exemption tableID already exists.

Here is my sample code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID)
if err != nil {
   return fmt.Errorf("bigquery.NewClient: %v", err)
}
defer client.Close()
query := SELECT * FROM `projectID.datasetID.tableID`
q := client.Query(query)
q.QueryConfig.Dst = client.Dataset(datasetID).Table(tableID)
job, err := q.Run(ctx)
if err != nil {
return err
}
 status, err := job.Wait(ctx)
 if err != nil {
    return err
}
if err := status.Err(); err != nil {
   return err
}
it, err := job.Read(ctx)

What did I miss?

Upvotes: 0

Views: 502

Answers (1)

Arben John Avillanosa
Arben John Avillanosa

Reputation: 213

Got the answer by reading the bigquery Go docs. https://godoc.org/cloud.google.com/go/bigquery#TableWriteDisposition

Added this line of code for setting the QueryConfig WriteDisposition to append data to an existing table

q.QueryConfig.WriteDisposition = "WRITE_APPEND"

Hope this helps.

Upvotes: 2

Related Questions