我们的应用程序在访问Big Query时有时会看到DEADLINE_EXCEEDED。
import (
"cloud.google.com/go/bigquery"
"golang.org/x/net/context"
"google.golang.org/api/option"
)
func MyFunc(ctx context.Context) {
:
client, err := bigquery.NewClient(ctx, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH))
query := client.Query("SELECT * FROM ....")
it, err := query.Read(ctx)
var list []MyStruct
for {
var m MyStruct
err := it.Next(&m)
if err == iterator.Done {
break
}
if err != nil {
<Error Handling>
}
list = append(list, m)
}
:
}
有时候我们会看到这个错误。
Get https://www.googleapis.com/bigquery/v2/projects/myproject/queries/job_zandIeLwH0s8f3FAQ_ORC0zau14?alt=json\u0026startIndex=0\u0026timeoutMs=60000: API error 5 (urlfetch: DEADLINE_EXCEEDED): ('The read operation timed out',)"
看起来超时是5秒,但我可以'找到如何更改超时秒。
我看了this post,并修改了我的源代码,如下所示。
ctx_with_deadline, _ := context.WithTimeout(ctx, 1*time.Minute)
httpClient := &http.Client{
Transport: &oauth2.Transport{
Base: &urlfetch.Transport{Context: ctx_with_deadline},
},
}
client, err := bigquery.NewClient(ctx, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH), option.WithHTTPClient(httpClient))
然后我遇到了这个错误。
Post https://www.googleapis.com/bigquery/v2/projects/myproject/jobs?alt=json: oauth2: Transport's Source is nil
如何在Go Bigquery中更改超时?
答案 0 :(得分:1)
在创建bigquery客户端的新实例时也使用ctx_with_deadline
:
client, err := bigquery.NewClient(ctx_with_deadline, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH), option.WithHTTPClient(httpClient))