【问题标题】:Java API to list dataflow job用于列出数据流作业的 Java API
【发布时间】:2020-08-18 13:20:06
【问题描述】:

我正在尝试编写 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 而不是凭据。

谁能帮助我如何使用凭据本身并解决此问题?

【问题讨论】:

  • HttpRequestInitializer 被强制设置是有原因的。是什么阻止您创建 HttpRequestInitializer?

标签: java google-cloud-platform google-cloud-dataflow


【解决方案1】:

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

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

    };
  }

【讨论】:

  • 感谢您的示例。我帮了我很多。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-12-02
  • 1970-01-01
  • 2018-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多