英文:
Java API to list dataflow job
问题
我正在尝试编写 Java 代码来列出数据流作业。
我参考了 https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.jobs/list。
但是 'GoogleCredential' 类已被弃用。我尝试将 GoogleCredential
替换为 GoogleCredentials
,但在下面这行代码:
Dataflow dataflowService = new Dataflow.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Google Cloud Platform Sample")
.build();
它期望使用 HttpRequestInitializer
替代凭据。
有人能帮我如何继续使用凭据并解决这个问题吗?
英文:
I am trying to write java code to list the dataflow job.
I have taken reference from https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.jobs/list
But the 'GoogleCredential' Class is deprecated. I tried replacing GoogleCredential
to GoogleCredentials
but at this below line
Dataflow dataflowService = new Dataflow.Builder(httpTransport, jsonFactory, credential)
.setApplicationName("Google Cloud Platform Sample")
.build();
it expects HttpRequestInitializer instead of credential.
Can anyone help me how can I use credential itself and fix this issue?
答案1
得分: 1
HttpRequestInitializer
需要在发生失败时执行自动重试。可以使用com.google.api.client.auth.oauth2.Credential
构造一个简单的HttpRequestInitializer
,如下所示:
private HttpRequestInitializer buildInitializer(Credential credential) {
return httpRequest -> {
// 处理异常的HTTP响应(非2XX响应)
final HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler =
new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())
.setSleeper(Sleeper.DEFAULT);
/*
这个设计只适用于一次只有一个HttpRequest。
因此,为每个HttpRequest实例创建一个新的具有新的BackOff实例的HttpBackOffIOExceptionHandler实例
*/
HttpBackOffIOExceptionHandler httpBackOffIOExceptionHandler =
new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(Sleeper.DEFAULT);
httpRequest.setInterceptor(credential).setUnsuccessfulResponseHandler(unsuccessfulResponseHandler)
.setIOExceptionHandler(httpBackOffIOExceptionHandler)
.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(3))
.setReadTimeout((int) TimeUnit.MINUTES.toMillis(3))
.setThrowExceptionOnExecuteError(false).setSuppressUserAgentSuffix(true);
};
}
英文:
HttpRequestInitializer
is required to perform automatic retry upon failures. A simple HttpRequestInitializer
can be constructed using com.google.api.client.auth.oauth2.Credential
as follows:
private HttpRequestInitializer buildInitializer(Credential credential) {
return httpRequest -> {
// handles abnormal HTTP responses (non 2XX responses)
final HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler =
new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())
.setSleeper(Sleeper.DEFAULT);
/*
this is designed to work with only one HttpRequest at a time.
Hence, a new instance of HttpBackOffIOExceptionHandler with new instance of
BackOff is created for each instance of HttpRequest
*/
HttpBackOffIOExceptionHandler httpBackOffIOExceptionHandler =
new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(Sleeper.DEFAULT);
httpRequest.setInterceptor(credential).setUnsuccessfulResponseHandler(unsuccessfulResponseHandler)
.setIOExceptionHandler(httpBackOffIOExceptionHandler)
.setConnectTimeout((int) TimeUnit.MINUTES.toMillis(3))
.setReadTimeout((int) TimeUnit.MINUTES.toMillis(3))
.setThrowExceptionOnExecuteError(false).setSuppressUserAgentSuffix(true);
};
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论