英文:
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.errorString
或bigquery.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
: columnMessage
: what failedReason
: this can simply beinvalid
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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论