简体   繁体   English

使用 go 更新/覆盖而不是插入插入 bigquery

[英]Inserting into bigquery using go updates / overwrites instead of inserts

I have an aws lambda written in Go that should insert into bigquery.我有一个用 Go 编写的 aws lambda,它应该插入到 bigquery 中。 It reference the cloud.google.com/go/bigquery package.它引用了 cloud.google.com/go/bigquery 包。

client, err := bigquery.NewClient(ctx, projectID, gcpOption)
if err != nil {
    println(fmt.Sprintf("error creating new client, %v", err))
    return fmt.Errorf("bigquery.NewClient: %v", err)
}
defer client.Close()

inserter := client.Dataset(datasetID).Table(tableID).Inserter()
if err := inserter.Put(ctx, items); err != nil {
    println(fmt.Sprintf("error inserting, %v", err))
    if multiError, ok := err.(bigquery.PutMultiError); ok {
        for _, err1 := range multiError {
            for _, err2 := range err1.Errors {
                fmt.Println(err2)
            }
        }
    } else {
        fmt.Println(err)
    }
    return err
} else {
    println("Inserted record")
}

When run, a record will be inserted, but running again will result in the previously inserted row being updated.运行时,将插入一条记录,但再次运行将导致更新先前插入的行。 This is not the behaviour I was expecting.这不是我期望的行为。 I am relatively new to Golang and GCP, so perhaps I have the wrong expectations?我对 Golang 和 GCP 比较陌生,所以也许我有错误的期望?

The table in big query is not partitioned.大查询中的表没有分区。 Items is an array of structs. Items 是一个结构数组。

The Inserter can be used to achieve at-least-once data insertion semantics. Inserter可用于实现至少一次数据插入语义。 The insert mechanism is not capable of upsert behavior, which is what you appear to be describing.插入机制不能进行 upsert 行为,这就是您所描述的。

It's unclear to me how you're validating this behavior, but I'd take another look at that as a starting point.我不清楚你是如何验证这种行为的,但我会再看看它作为起点。

More information about the tabledata.insertAll streaming API which underlies the go Inserter can be found here: https://cloud.google.com/bigquery/streaming-data-into-bigquery有关作为 go Inserter基础的tabledata.insertAll流 API 的更多信息,请访问: https : //cloud.google.com/bigquery/streaming-data-into-bigquery

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM