阅读Golang中的BigQuery。并不是所有的预期结果都给出了。怎么办?
假设SQL在查询编辑器中运行良好。在将其分配给结构之后,数据似乎具有不同的值。为什么会这样阅读Golang中的BigQuery。并不是所有的预期结果都给出了。怎么办?,go,google-bigquery,Go,Google Bigquery,假设SQL在查询编辑器中运行良好。在将其分配给结构之后,数据似乎具有不同的值。为什么会这样 var RunQuery = func(req *http.Request, query string)(*bigquery.RowIterator, error){ ctx := appengine.NewContext(req) ctxWithDeadline, _ := context.WithTimeout(ctx, 30*time.Minute) bqClient, bq
var RunQuery = func(req *http.Request, query string)(*bigquery.RowIterator, error){
ctx := appengine.NewContext(req)
ctxWithDeadline, _ := context.WithTimeout(ctx, 30*time.Minute)
bqClient, bqErr := bigquery.NewClient(ctxWithDeadline, project, option.WithCredentialsFile(serviceAccount))
if bqErr != nil {
log.Errorf(ctx, "%v", bqErr)
return nil, bqErr
}
q := bqClient.Query(query)
job, err := q.Run(ctx)
if err != nil {
log.Errorf(ctx, "%v", err)
return nil, err
}
status, err := job.Wait(ctx)
if err != nil {
log.Errorf(ctx, "%v", err)
return nil, err
}
if err := status.Err(); err != nil {
log.Errorf(ctx, "%v", err)
return nil, err
}
it, err := job.Read(ctx)
if err != nil {
log.Errorf(ctx, "%v", err)
return nil, err
}
log.Infof(ctx, "Total Rows: %v", it.TotalRows)
return it, nil
}
type Customers struct {
CustomerName string `bigquery:"customer_name"`
CustomerAge int `bigquery:"customer_age"`
}
var rowsRead int
func main() {
query := `SELECT
name as customer_name,
age as customer_age
FROM customer_table
WHERE customerStatus = '0'`
customerInformation, customerInfoErr := RunQuery(req, query, false)
if customerInfoErr != nil {
log.Errorf(ctx, "Fetching customer information error :: %v", customerInfoErr)
return
}
for {
var row Customers
err := customerInformation.Next(&row)
log.Infof(ctx, "row %v", row)
if err == iterator.Done {
log.Infof(ctx, "ITERATION COMPLETE. Rows read %v", rowsRead)
break
}
rowsRead++
}
}
假设我有
客户|姓名|客户|年龄
第2类
狗| 3
马| 10
但将其分配给结构后,结果是
客户|姓名|客户|年龄
"" | 2
狗|“
"" | ""
为什么是这样?我甚至在chunk上测试了它,我将限制设置为1000,结果仍然相同。但是查询编辑器中的查询结果是我所期望的使用
值加载器
bigquery.Value
解决它。而不是在映射查询结果时使用预期的结构。已使用map[string]bigquery.Value
。仍然不知道为什么使用预期结构映射查询结果不能很好地工作。这是我的解决办法
for {
row := make(map[string]bigquery.Value)
err := customerInformation.Next(&row)
log.Infof(ctx, "row %v", row)
if err == iterator.Done {
log.Infof(ctx, "ITERATION COMPLETE. Rows read %v", rowsRead)
break
}
rowsRead++
}
使用
Value Loader
bigquery.Value
解决了这个问题。而不是在映射查询结果时使用预期的结构。已使用map[string]bigquery.Value
。仍然不知道为什么使用预期结构映射查询结果不能很好地工作。这是我的解决办法
for {
row := make(map[string]bigquery.Value)
err := customerInformation.Next(&row)
log.Infof(ctx, "row %v", row)
if err == iterator.Done {
log.Infof(ctx, "ITERATION COMPLETE. Rows read %v", rowsRead)
break
}
rowsRead++
}
如果这是你真正的要求,你写的是
age as cutomer\u age
所以我认为它不能给你customer\u age
no?no。更新了代码。如果这是你真正的要求,你写的age as cutomer\u age
所以我认为它不能给你customer\u age
no?no。更新了代码。