BigQuery使用Go库进行行插入失败。

huangapple go评论83阅读模式
英文:

BigQuery row insertions failed with Go library

问题

我在尝试向BigQuery插入数据时遇到了"X行插入失败"的错误,其中X是插入失败的行数。

我在Golang中使用了"cloud.google.com/go/bigquery"这个库。

我在这里附上了代码:

    u := table.Uploader()

	var inserts []*bigquery.StructSaver
	for _, insert := range value {
		aux := bigquery.StructSaver{Struct: insert, Schema: schema}
		inserts = append(inserts, &aux)
	}

	err := u.Put(ctx, inserts)
	if err != nil {
	    fmt.Printf("%v\n", err)
	}

这个错误并不是每次都发生,我不知道是什么原因导致的。有人遇到过相同的错误吗?

英文:

I'm getting this error "X row insertions failed" where X is a number of rows when I try to insert in bigquery.

I use this library "cloud.google.com/go/bigquery" in Golang

I attach the code here:

    u := table.Uploader()

	var inserts []*bigquery.StructSaver
	for _, insert := range value {
		aux := bigquery.StructSaver{Struct: insert, Schema: schema}
		inserts = append(inserts, &aux)
	}

	err := u.Put(ctx, inserts)
	if err != nil {
	    fmt.Printf("%v\n", err)
	}

This doesn't happen in every try and I don't know what can produce it.
Anyone got the same error?

答案1

得分: 6

使用bigquery.PutMultiError,我得到了以下属性,其中Message描述了失败的原因。

  • Location: 列名
  • Message: 失败的具体信息
  • Reason: 可以简单地为invalid(无效)

以下是一个示例:

{Location: "speed"; Message: "无法将值转换为整数(错误值):foobar"; Reason: "invalid"}

这是一个使用PutMultiError的示例,正如1lann所提到的。需要注意的是,Put可能返回*errors.errorStringbigquery.PutMultiError,因此检查你拥有的是哪种类型是有用的。

err := u.Put(ctx, inserts)
if err != nil {
    if multiError, ok := err.(bigquery.PutMultiError); ok {
		for _, err1 := range multiError {
			for _, err2 := range err1.Errors {
	  	        fmt.Println(err2)
			}
		}
    } else {
        fmt.Println(err)
    }
}
英文:

Using bigquery.PutMultiError I get the following properties where Message profiles the failure reason.

  • Location: column
  • Message: what failed
  • Reason: this can simply be invalid

Here is an example:

{Location: "speed"; Message: "Cannot convert value to integer (bad value):foobar"; Reason: "invalid"}

Here's an example using PutMultiError as mentioned by 1lann. Of note, Put can return a *errors.errorString or bigquery.PutMultiError so it's useful to check which you have.

err := u.Put(ctx, inserts)
if err != nil {
    if multiError, ok := err.(bigquery.PutMultiError); ok {
		for _, err1 := range multiError {
			for _, err2 := range err1.Errors {
	  	        fmt.Println(err2)
			}
		}
    } else {
        fmt.Println(err)
    }
}

答案2

得分: 2

一个可能的修复方法是确保你的结构体字段是可导出的(例如,以大写字母开头)。

这只是一个非常糟糕的错误消息,这只是你可能会遇到该错误的原因之一。

英文:

One possible fix is to make sure the fields of your struct are exported (e.g. start with uppercase letters).

It's just a really bad error message, this is just one of the reasons you could be getting that error

huangapple
  • 本文由 发表于 2017年1月29日 21:42:21
  • 转载请务必保留本文链接:https://go.coder-hub.com/41921566.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定