使用BigQuery的go客户端库的以下代码返回错误
type GceQuotaRow struct {
CustomerProjectNumber int64
ExecutionProjectNumber int64
Region string
Metric string
Limit float64
}
ds := b.bq.Dataset(task.Options.Dataset)
table := ds.Table(task.Options.Table)
u := table.Uploader()
rows := []bigquery.StructSaver{}
// rows := []*GceQuotaRow{}
schema, err := bigquery.InferSchema(GceQuotaRow{})
if err != nil {
log.Errorf("Inferring BigQuery scheme failed; %v", util.Pformat(err))
return backfill.TaskPermanentError
}
// Construct an insertID to dedupe insertions on a best effort basis.
for _, region := range res.GetRegionalQuotas() {
for _, q := range region.GetQuotas() {
r := &GceQuotaRow{
CustomerProjectNumber: task.CustomerProjectNumber,
ExecutionProjectNumber: res.GetExecutionProjectNumber(),
Region: region.GetRegion(),
Metric: q.GetMetric(),
Limit: q.GetLimit(),
}
h := sha256.New()
h.Write([]byte(util.Pformat(r)))
// Use the sha256 of the row as the InsertId to avoid duplicates.
insertId := fmt.Sprintf("%x", h.Sum(nil))
rows = append(rows, bigquery.StructSaver{
Schema: schema,
InsertID: insertId,
Struct: r,
})
// rows = append(rows, r)
}
}
err = u.Put(ctx, rows)
if err != nil {
putErr, isErr := err.(bigquery.PutMultiError)
if isErr {
for _, e := range putErr {
log.Errorf("There was a problem writing Row for customer project: %v, Error: %v", task.CustomerProjectNumber, util.Pformat(e))
}
} else {
log.Errorf("There was a problem writing GCE Quota Rows for customer project: %v, Error: %v", task.CustomerProjectNumber, err)
}
}
对Put的调用返回错误:
Error: bigquery: schema inference for recursive type *bigquery.FieldSchema
我不明白为什么Put会返回一个关于推断模式的错误,因为InferSchema调用成功,所以应该将模式传递给结构化器。
我想使用StructSaver,因此我可以提供插入ID以重复删除行。
答案 0 :(得分:0)
我只是碰到了这个,然后感到困惑。发生这种情况的原因是,具体的INSERT INTO payment_issue (id, status)
SELECT s.id, s.status
FROM subscriptions s LEFT JOIN
subscriptionlog l
ON s.id = l.subscription_id
WHERE l.event LIKE 'payment_error_mode_triggered %' AND
l.date >= 1514764800 AND
l.date <= 1569888000 and
s.status = 'payment-issue'
ON DUPLICATE KEY UPDATE id = VALUES(id);
没有实现bigquery.StructSaver
接口,因为它的方法对指针类型bigquery.ValueSaver
进行操作。这会导致golang客户端库退回到结构上的通用模式推断逻辑(在本例中为*bigquery.StructSaver
),然后该逻辑失败,因为bigquery.StructSaver
嵌套了其他结构,因此本身本身不是有效的对象用于保存在BigQuery中。您可以按以下步骤修改代码:
bigquery.StructSaver
我希望这会有所帮助(也许如果不是您,事实发生两年之后,那么其他偶然发现此问题的人)!