如何使用 Java SDK 在 Dataproc 的 GceClusterConfig 中指定 ZoneUri

How to Specify ZoneUri in Dataproc's GceClusterConfig using Java SDK

尝试使用 Dataproc 创建新的计算集群时,它会抱怨说当我指定 "real" 区域(例如 "us-west1-a" 或 "us-central1-f" 时,我收到一条错误消息此终结点不支持指定的区域。 (请参阅下面的错误文本)

如您所见,根据错误,它期望区域为"global"。但是,将其指定为全局会生成 "global" 不是有效区域的错误。做一些愚蠢的事情,比如将其指定为“[global]”只会生成无效的 URI 格式。不指定区域会导致它抱怨必须设置区域。

因此,所有可能的逻辑值都被消除了,这表明必须采取一些其他步骤来解决这个问题。

错误:

Reason: 400 Bad Request
{
   "errors" : [
      {
         "reason" : "badRequest",
         "domain" : "global",
         "message" : "Region us-central1-f invalid or not supported by this endpoint; permitted regions: [global]"
      }
   ],
   "status" : "INVALID_ARGUMENT",
   "code" : 400,
   "message" : "Region us-central1-f invalid or not supported by this endpoint; permitted regions: [global]"
}

生成此代码的代码片段:

Cluster cluster = createClusterSpec();
createOp = dataproc.projects().regions().clusters()
           .create(projectId, region, cluster);
createOp.setBearerToken(credentials.getAccessToken().getTokenValue());
createOp.execute();

// I'm cheating here: the actual code pulls the config from various
// inputs and properties, but we can replicate with hard-coded values.
private Cluster createClusterSpec() {
    GceClusterConfig computeEngineConfig = new GceClusterConfig();
    // ZONE_URI_FORMAT =
    //  "https://www.googleapis.com/compute/v1/projects/%s/zones/%s"
    computeEngineConfig.setZoneUri(
        String.format(ZONE_URI_FORMAT, "funny-project-001",
                                       "us-central1-f"));
    InstanceGroupConfig masterConfig = new InstanceGroupConfig();
    masterConfig.setMachineTypeUri(
        String.format(MACHINE_TYPE_URI_FORMAT,
                      "funny-project-001", "us-central1-f",
                      "n1-standard-1"))
       .setNumInstances(1);
    InstanceGroupConfig workerConfig = new InstanceGroupConfig();
    workerConfig.setMachineTypeUri(
        String.format(MACHINE_TYPE_URI_FORMAT,
                      "funny-project-001", "us-central1-f",
                      "n1-standard-1"))

       .setNumInstances(1);


    ClusterConfig googClusterConfig = new ClusterConfig();
    googClusterConfig.setGceClusterConfig(computeEngineConfig);
    googClusterConfig.setMasterConfig(masterConfig);
    googClusterConfig.setWorkerConfig(workerConfig);
    Cluster returnVal = new Cluster();
    returnVal.setProjectId("funny-project-001");
    returnVal.setConfig(googClusterConfig);
    returnVal.setClusterName("mrfoo");
    return returnVal;
}

Dataproc 区域是独立于 Compute Engine "zones" 指定的,尽管两者之间确实存在关系。目前,您确实只与 "global" Dataproc 区域对话,它知道如何为所有 GCE 区域提供服务。所以你只需要指定 "global" 作为参数:

createOp = dataproc.projects().regions().clusters()
       .create(projectId, "global", cluster);

然后将您的 GCE 区域指定为特定的 us-central1-f 或您想要的任何区域。如果您使用 Dataproc 的 UI 选择用于创建集群的选项,则可以在 console.cloud.google.com 中使用底层 JSON REST 表示,然后在最底部有一个 "Equivalent REST" link.

您会看到如下内容:

POST /v1/projects/foo-project/regions/global/clusters/
{
   ...
   "gceClusterConfig": {
      "zoneUri": "https://www.googleapis.com/compute/v1/projects/foo-project/zones/us-west1-a"
      ...
   }
   ...
}

POST URI 中的 regions/global 是区域参数在 SDK 的 create(project, region, cluster) 方法中的位置,而 gceClusterConfig 的正文是您设置的位置zoneUri 明确。