|
| 1 | +/* |
| 2 | + * Copyright 2019 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.google.cloud.vision.samples.automl; |
| 18 | + |
| 19 | +// [START automl_vision_object_detection_deploy_model_node_count] |
| 20 | +import com.google.api.gax.longrunning.OperationFuture; |
| 21 | +import com.google.cloud.automl.v1beta1.AutoMlClient; |
| 22 | +import com.google.cloud.automl.v1beta1.DeployModelRequest; |
| 23 | +import com.google.cloud.automl.v1beta1.ImageObjectDetectionModelDeploymentMetadata; |
| 24 | +import com.google.cloud.automl.v1beta1.ModelName; |
| 25 | +import com.google.cloud.automl.v1beta1.OperationMetadata; |
| 26 | +import com.google.protobuf.Empty; |
| 27 | + |
| 28 | +import java.io.IOException; |
| 29 | +import java.util.concurrent.ExecutionException; |
| 30 | + |
| 31 | +class DeployModelNodeCount { |
| 32 | + |
| 33 | + static void deployModelNodeCount(String projectId, String modelId) { |
| 34 | + // String projectId = "YOUR_PROJECT_ID"; |
| 35 | + // String modelId = "YOUR_MODEL_ID"; |
| 36 | + |
| 37 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 38 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 39 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 40 | + try (AutoMlClient client = AutoMlClient.create()) { |
| 41 | + // Get the full path of the model. |
| 42 | + ModelName modelFullId = ModelName.of(projectId, "us-central1", modelId); |
| 43 | + |
| 44 | + // Set how many nodes the model is deployed on |
| 45 | + ImageObjectDetectionModelDeploymentMetadata deploymentMetadata = |
| 46 | + ImageObjectDetectionModelDeploymentMetadata.newBuilder().setNodeCount(2).build(); |
| 47 | + |
| 48 | + DeployModelRequest request = DeployModelRequest.newBuilder() |
| 49 | + .setName(modelFullId.toString()) |
| 50 | + .setImageObjectDetectionModelDeploymentMetadata(deploymentMetadata) |
| 51 | + .build(); |
| 52 | + // Deploy the model |
| 53 | + OperationFuture<Empty, OperationMetadata> future = client.deployModelAsync(request); |
| 54 | + future.get(); |
| 55 | + System.out.println("Model deployment on 2 nodes finished"); |
| 56 | + } catch (IOException | InterruptedException | ExecutionException e) { |
| 57 | + e.printStackTrace(); |
| 58 | + } |
| 59 | + } |
| 60 | +} |
| 61 | +// [END automl_vision_object_detection_deploy_model_node_count] |
0 commit comments