鑒于 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 intfunc 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 }假設我有查詢結果, customer_name|customer_age cat | 2 dog | 3 horse | 10但是在將其分配給結構后,結果是 customer_name|customer_age "" | 2 dog | "" "" | ""為什么會這樣?我什至在我將限制設置為 1000 的塊上對其進行了測試,結果仍然相同。但是查詢編輯器中的查詢結果是我所期望的
2 回答
江戶川亂折騰
TA貢獻1851條經驗 獲得超5個贊
從文檔中:
如果 dst 是指向結構的指針,則架構中的每一列都將與結構中具有相同名稱的導出字段匹配,忽略大小寫。不匹配的架構列和結構字段將被忽略。
在這里,您嘗試將 customer_age 解析為名為 CustomerAge 的結構屬性。如果您將其更新為 Customer_Age 或 customer_age 它應該可以工作。
慕村225694
TA貢獻1880條經驗 獲得超4個贊
使用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++
}
- 2 回答
- 0 關注
- 178 瀏覽
添加回答
舉報
0/150
提交
取消
