|
| 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 features in bulk for an existing entity type. See |
| 18 | + * https://cloud.google.com/vertex-ai/docs/featurestore/setup |
| 19 | + * before running the code snippet |
| 20 | + */ |
| 21 | + |
| 22 | +package aiplatform; |
| 23 | + |
| 24 | +// [START aiplatform_batch_create_features_sample] |
| 25 | + |
| 26 | +import com.google.api.gax.longrunning.OperationFuture; |
| 27 | +import com.google.cloud.aiplatform.v1.BatchCreateFeaturesOperationMetadata; |
| 28 | +import com.google.cloud.aiplatform.v1.BatchCreateFeaturesRequest; |
| 29 | +import com.google.cloud.aiplatform.v1.BatchCreateFeaturesResponse; |
| 30 | +import com.google.cloud.aiplatform.v1.CreateFeatureRequest; |
| 31 | +import com.google.cloud.aiplatform.v1.EntityTypeName; |
| 32 | +import com.google.cloud.aiplatform.v1.Feature; |
| 33 | +import com.google.cloud.aiplatform.v1.Feature.ValueType; |
| 34 | +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; |
| 35 | +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; |
| 36 | +import java.io.IOException; |
| 37 | +import java.util.ArrayList; |
| 38 | +import java.util.List; |
| 39 | +import java.util.concurrent.ExecutionException; |
| 40 | +import java.util.concurrent.TimeUnit; |
| 41 | +import java.util.concurrent.TimeoutException; |
| 42 | + |
| 43 | +public class BatchCreateFeaturesSample { |
| 44 | + |
| 45 | + public static void main(String[] args) |
| 46 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 47 | + // TODO(developer): Replace these variables before running the sample. |
| 48 | + String project = "YOUR_PROJECT_ID"; |
| 49 | + String featurestoreId = "YOUR_FEATURESTORE_ID"; |
| 50 | + String entityTypeId = "YOUR_ENTITY_TYPE_ID"; |
| 51 | + String location = "us-central1"; |
| 52 | + String endpoint = "us-central1-aiplatform.googleapis.com:443"; |
| 53 | + int timeout = 300; |
| 54 | + batchCreateFeaturesSample(project, featurestoreId, entityTypeId, location, endpoint, timeout); |
| 55 | + } |
| 56 | + |
| 57 | + static void batchCreateFeaturesSample( |
| 58 | + String project, |
| 59 | + String featurestoreId, |
| 60 | + String entityTypeId, |
| 61 | + String location, |
| 62 | + String endpoint, |
| 63 | + int timeout) |
| 64 | + throws IOException, InterruptedException, ExecutionException, TimeoutException { |
| 65 | + FeaturestoreServiceSettings featurestoreServiceSettings = |
| 66 | + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); |
| 67 | + |
| 68 | + // Initialize client that will be used to send requests. This client only needs to be created |
| 69 | + // once, and can be reused for multiple requests. After completing all of your requests, call |
| 70 | + // the "close" method on the client to safely clean up any remaining background resources. |
| 71 | + try (FeaturestoreServiceClient featurestoreServiceClient = |
| 72 | + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { |
| 73 | + |
| 74 | + List<CreateFeatureRequest> createFeatureRequests = new ArrayList<>(); |
| 75 | + |
| 76 | + Feature titleFeature = |
| 77 | + Feature.newBuilder() |
| 78 | + .setDescription("The title of the movie") |
| 79 | + .setValueType(ValueType.STRING) |
| 80 | + .build(); |
| 81 | + Feature genresFeature = |
| 82 | + Feature.newBuilder() |
| 83 | + .setDescription("The genres of the movie") |
| 84 | + .setValueType(ValueType.STRING) |
| 85 | + .build(); |
| 86 | + Feature averageRatingFeature = |
| 87 | + Feature.newBuilder() |
| 88 | + .setDescription("The average rating for the movie, range is [1.0-5.0]") |
| 89 | + .setValueType(ValueType.DOUBLE) |
| 90 | + .build(); |
| 91 | + |
| 92 | + createFeatureRequests.add( |
| 93 | + CreateFeatureRequest.newBuilder().setFeature(titleFeature).setFeatureId("title").build()); |
| 94 | + |
| 95 | + createFeatureRequests.add( |
| 96 | + CreateFeatureRequest.newBuilder() |
| 97 | + .setFeature(genresFeature) |
| 98 | + .setFeatureId("genres") |
| 99 | + .build()); |
| 100 | + |
| 101 | + createFeatureRequests.add( |
| 102 | + CreateFeatureRequest.newBuilder() |
| 103 | + .setFeature(averageRatingFeature) |
| 104 | + .setFeatureId("average_rating") |
| 105 | + .build()); |
| 106 | + |
| 107 | + BatchCreateFeaturesRequest batchCreateFeaturesRequest = |
| 108 | + BatchCreateFeaturesRequest.newBuilder() |
| 109 | + .setParent( |
| 110 | + EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) |
| 111 | + .addAllRequests(createFeatureRequests) |
| 112 | + .build(); |
| 113 | + |
| 114 | + OperationFuture<BatchCreateFeaturesResponse, BatchCreateFeaturesOperationMetadata> |
| 115 | + batchCreateFeaturesFuture = |
| 116 | + featurestoreServiceClient.batchCreateFeaturesAsync(batchCreateFeaturesRequest); |
| 117 | + System.out.format( |
| 118 | + "Operation name: %s%n", batchCreateFeaturesFuture.getInitialFuture().get().getName()); |
| 119 | + System.out.println("Waiting for operation to finish..."); |
| 120 | + BatchCreateFeaturesResponse batchCreateFeaturesResponse = |
| 121 | + batchCreateFeaturesFuture.get(timeout, TimeUnit.SECONDS); |
| 122 | + System.out.println("Batch Create Features Response"); |
| 123 | + System.out.println(batchCreateFeaturesResponse); |
| 124 | + featurestoreServiceClient.close(); |
| 125 | + } |
| 126 | + } |
| 127 | +} |
| 128 | +// [END aiplatform_batch_create_features_sample] |
0 commit comments