Java API用于列出数据流作业

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

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);

    };
  }

huangapple
  • 本文由 发表于 2020年8月18日 21:20:06
  • 转载请务必保留本文链接:https://go.coder-hub.com/63469551.html
匿名

发表评论

匿名网友

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

确定