在Go BigQuery上出现了DEADLINE_EXCEEDED错误。

huangapple go评论86阅读模式
英文:

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 (
	&quot;cloud.google.com/go/bigquery&quot;
	&quot;golang.org/x/net/context&quot;
	&quot;google.golang.org/api/option&quot;
)

func MyFunc(ctx context.Context) {
			:
	client, err := bigquery.NewClient(ctx, PROJECT_ID, option.WithServiceAccountFile(SERVICE_ACCOUNT_JSON_FILE_PATH))
	query := client.Query(&quot;SELECT * FROM ....&quot;)
	it, err := query.Read(ctx)
	var list []MyStruct
	for {
		var m MyStruct
		err := it.Next(&amp;m)
		if err == iterator.Done {
			break
		}
		if err != nil {
           &lt;Error Handling&gt;
		}
		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): (&#39;The read operation timed out&#39;,)&quot;

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 := &amp;http.Client{
	Transport: &amp;oauth2.Transport{
		Base: &amp;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&#39;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_IDSERVICE_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))

huangapple
  • 本文由 发表于 2017年4月11日 15:17:05
  • 转载请务必保留本文链接:https://go.coder-hub.com/43339181.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定