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.

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

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)