英文:
DEADLINE_EXCEEDED on Go Bigquery
问题
我们的应用程序在访问BigQuery时有时会出现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秒,但我找不到如何更改超时时间的方法。
我阅读了这篇帖子,并根据以下方式修改了我的源代码:
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中更改超时时间?
英文:
Our application sometimes see DEADLINE_EXCEEDED when accessing to Big Query.
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)
}
:
}
Sometimes we see this error.
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',)"
It looks timeout is 5 second, but I can' find how can I change timeout seconds.
I read this post, and I modified my source code as below.
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))
Then I met this error.
Post https://www.googleapis.com/bigquery/v2/projects/myproject/jobs?alt=json: oauth2: Transport's Source is nil
How can I change timeout in Go Bigquery?
答案1
得分: 1
在创建bigquery客户端的新实例时,也要使用ctx_with_deadline
:
client, err := bigquery.NewClient(ctx_with_deadline, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH), option.WithHTTPClient(httpClient))
请注意,PROJECT_ID
和SERVICE_ACCOUNT_JSON_FILE_PATH
应替换为实际的项目ID和服务账号JSON文件路径。
英文:
Use ctx_with_deadline
also when creating a new instance of the bigquery client:
client, err := bigquery.NewClient(ctx_with_deadline, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH), option.WithHTTPClient(httpClient))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论