|
| 1 | +/* |
| 2 | + * Copyright 2022 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 | + * Create an entity type so that you can create its related features. See |
| 18 | + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running |
| 19 | + * the code snippet |
| 20 | + */ |
| 21 | + |
| 22 | +package aiplatform; |
| 23 | + |
| 24 | +// [START aiplatform_create_entity_type_monitoring_sample] |
| 25 | + |
| 26 | +import com.google.api.gax.longrunning.OperationFuture; |
| 27 | +import com.google.cloud.aiplatform.v1.CreateEntityTypeOperationMetadata; |
| 28 | +import com.google.cloud.aiplatform.v1.CreateEntityTypeRequest; |
| 29 | +import com.google.cloud.aiplatform.v1.EntityType; |
| 30 | +import com.google.cloud.aiplatform.v1.FeaturestoreMonitoringConfig; |
| 31 | +import com.google.cloud.aiplatform.v1.FeaturestoreMonitoringConfig.SnapshotAnalysis; |
| 32 | +import com.google.cloud.aiplatform.v1.FeaturestoreName; |
| 33 | +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; |
| 34 | +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; |
| 35 | +import java.io.IOException; |
| 36 | +import java.util.concurrent.ExecutionException; |
| 37 | +import java.util.concurrent.TimeUnit; |
| 38 | +import java.util.concurrent.TimeoutException; |
| 39 | + |
| 40 | +public class CreateEntityTypeMonitoringSample { |
| 41 | + |
| 42 | + public static void main(String[] args) |
| 43 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 44 | + // TODO(developer): Replace these variables before running the sample. |
| 45 | + String project = "YOUR_PROJECT_ID"; |
| 46 | + String featurestoreId = "YOUR_FEATURESTORE_ID"; |
| 47 | + String entityTypeId = "YOUR_ENTITY_TYPE_ID"; |
| 48 | + String description = "YOUR_ENTITY_TYPE_DESCRIPTION"; |
| 49 | + int monitoringIntervalDays = 1; |
| 50 | + String location = "us-central1"; |
| 51 | + String endpoint = "us-central1-aiplatform.googleapis.com:443"; |
| 52 | + int timeout = 300; |
| 53 | + createEntityTypeMonitoringSample( |
| 54 | + project, |
| 55 | + featurestoreId, |
| 56 | + entityTypeId, |
| 57 | + description, |
| 58 | + monitoringIntervalDays, |
| 59 | + location, |
| 60 | + endpoint, |
| 61 | + timeout); |
| 62 | + } |
| 63 | + |
| 64 | + static void createEntityTypeMonitoringSample( |
| 65 | + String project, |
| 66 | + String featurestoreId, |
| 67 | + String entityTypeId, |
| 68 | + String description, |
| 69 | + int monitoringIntervalDays, |
| 70 | + String location, |
| 71 | + String endpoint, |
| 72 | + int timeout) |
| 73 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 74 | + |
| 75 | + FeaturestoreServiceSettings featurestoreServiceSettings = |
| 76 | + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); |
| 77 | + |
| 78 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 79 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 80 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 81 | + try (FeaturestoreServiceClient featurestoreServiceClient = |
| 82 | + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { |
| 83 | + |
| 84 | + FeaturestoreMonitoringConfig featurestoreMonitoringConfig = |
| 85 | + FeaturestoreMonitoringConfig.newBuilder() |
| 86 | + .setSnapshotAnalysis( |
| 87 | + SnapshotAnalysis.newBuilder().setMonitoringIntervalDays(monitoringIntervalDays)) |
| 88 | + .build(); |
| 89 | + |
| 90 | + EntityType entityType = |
| 91 | + EntityType.newBuilder() |
| 92 | + .setDescription(description) |
| 93 | + .setMonitoringConfig(featurestoreMonitoringConfig) |
| 94 | + .build(); |
| 95 | + |
| 96 | + CreateEntityTypeRequest createEntityTypeRequest = |
| 97 | + CreateEntityTypeRequest.newBuilder() |
| 98 | + .setParent(FeaturestoreName.of(project, location, featurestoreId).toString()) |
| 99 | + .setEntityType(entityType) |
| 100 | + .setEntityTypeId(entityTypeId) |
| 101 | + .build(); |
| 102 | + |
| 103 | + OperationFuture<EntityType, CreateEntityTypeOperationMetadata> entityTypeFuture = |
| 104 | + featurestoreServiceClient.createEntityTypeAsync(createEntityTypeRequest); |
| 105 | + System.out.format( |
| 106 | + "Operation name: %s%n", entityTypeFuture.getInitialFuture().get().getName()); |
| 107 | + System.out.println("Waiting for operation to finish..."); |
| 108 | + EntityType entityTypeResponse = entityTypeFuture.get(timeout, TimeUnit.SECONDS); |
| 109 | + System.out.println("Create Entity Type Monitoring Response"); |
| 110 | + System.out.format("Name: %s%n", entityTypeResponse.getName()); |
| 111 | + } |
| 112 | + } |
| 113 | +} |
| 114 | +// [END aiplatform_create_entity_type_monitoring_sample] |
0 commit comments