首页 > 解决方案 > 在 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 的块上对其进行了测试,结果仍然相同。但是查询编辑器中的查询结果是我所期望的

标签: gogoogle-bigquery

解决方案


使用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++
   }

推荐阅读