douyuan4825 2018-06-25 09:00
浏览 39
已采纳

如何在Golang中更新BigQuery行

I have a go program connected to a bigquery table. This is the table's schema:

name    STRING  NULLABLE    
age     INTEGER NULLABLE    
amount  INTEGER NULLABLE

I have succeded at queryng the data of this table and printing all rows on console with this code:

ctx := context.Background()
client, err := bigquery.NewClient(ctx, projectID)

q := client.Query("SELECT * FROM test.test_user LIMIT 1000")

it, err := q.Read(ctx)
if err != nil {
    log.Fatal(err)
}
    for {
        var values []bigquery.Value
        err := it.Next(&values)
        if err == iterator.Done {
            break
        }
        if err != nil {
            // TODO: Handle error.
        }
            fmt.Println(values)
    }

And I also have succeded to insert data on the table from a struct using this code:

type test struct {
    Name   string
    Age    int
    Amount int
}
u := client.Dataset("testDS").Table("test_user").Uploader()

savers := []*bigquery.StructSaver{
    {Struct: test{Name: "Jack", Age: 23, Amount:123}, InsertID: "id1"},
}

if err := u.Put(ctx, savers); err != nil {
    log.Fatal(err)
}
fmt.Printf("rows inserted!!")

Now, what I am failing to do is updating rows. What I want to do is selecting all the rows and update all of them with an operation (for example: amount = amount * 2)

How can I achieve this using golang?

  • 写回答

1条回答 默认 最新

  • dongsui5464 2018-06-25 10:12
    关注

    Updating rows is not specific to Go, or any other client library. If you want to update data in BigQuery, you need to use DML (Data Manipulation Language) via SQL. So, essentially you already have the main part working (running a query) - you just need to change this SQL to use DML.

    But, a word of caution: BigQuery is a OLAP service. Don't use it for OLTP. Also, there are quotas with using DML. Make sure you familiarise yourself with them.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?