go - 在 Golang 中阅读 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, 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++
}
}
假设我有查询结果,
customer_name|customer_age
cat | 2
dog | 3
horse | 10
但是在将其分配给结构后,结果是
customer_name|customer_age
"" | 2
dog | ""
"" | ""
为什么会这样?我什至在我将限制设置为 1000 的块上对其进行了测试,结果仍然相同。但是查询编辑器中的查询结果是我所期望的
解决方案
使用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++
}
推荐阅读
- firebase - 如何将触发交易(前端)的 Paypal 用户与 IPN(后端)匹配?
- javascript - Qualtrics,视频开始后出现下一步按钮
- python - 在 python/bash 中打印带有条件的特定行范围
- javascript - 如何在 React JSX 中打印对象
- javascript - JQuery 无法获得最近的兄弟姐妹
- java - 方法应该返回不同的值
- asp.net - 如何确定您的 Web 应用程序是否获得了会话锁定?
- mongodb - 合并相同的结果猫鼬
- python - 熊猫,对于column1的每个唯一值,我想计算column2的每个唯一值出现的次数
- javascript - 画布上的多个数学公式