列出数据流作业的javaapi

huwehgph  于 2021-07-12  发布在  Java
关注(0)|答案(1)|浏览(334)

我正在尝试编写java代码来列出数据流作业。我引用了https://cloud.google.com/dataflow/docs/reference/rest/v1b3/projects.jobs/list 但“googlecredential”类已被弃用。我试着替换 GoogleCredentialGoogleCredentials 但在这条线下

  1. Dataflow dataflowService = new Dataflow.Builder(httpTransport, jsonFactory, credential)
  2. .setApplicationName("Google Cloud Platform Sample")
  3. .build();

它需要httprequestinitializer而不是credential。
有人能帮助我如何使用凭证本身并解决此问题吗?

xzlaal3s

xzlaal3s1#

HttpRequestInitializer 需要在失败时执行自动重试。一个简单的 HttpRequestInitializer 可以使用 com.google.api.client.auth.oauth2.Credential 具体如下:

  1. private HttpRequestInitializer buildInitializer(Credential credential) {
  2. return httpRequest -> {
  3. // handles abnormal HTTP responses (non 2XX responses)
  4. final HttpUnsuccessfulResponseHandler unsuccessfulResponseHandler =
  5. new HttpBackOffUnsuccessfulResponseHandler(new ExponentialBackOff())
  6. .setSleeper(Sleeper.DEFAULT);
  7. /*
  8. this is designed to work with only one HttpRequest at a time.
  9. Hence, a new instance of HttpBackOffIOExceptionHandler with new instance of
  10. BackOff is created for each instance of HttpRequest
  11. */
  12. HttpBackOffIOExceptionHandler httpBackOffIOExceptionHandler =
  13. new HttpBackOffIOExceptionHandler(new ExponentialBackOff()).setSleeper(Sleeper.DEFAULT);
  14. httpRequest.setInterceptor(credential).setUnsuccessfulResponseHandler(unsuccessfulResponseHandler)
  15. .setIOExceptionHandler(httpBackOffIOExceptionHandler)
  16. .setConnectTimeout((int) TimeUnit.MINUTES.toMillis(3))
  17. .setReadTimeout((int) TimeUnit.MINUTES.toMillis(3))
  18. .setThrowExceptionOnExecuteError(false).setSuppressUserAgentSuffix(true);
  19. };
  20. }
展开查看全部

相关问题