|
| 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 | +packageaiplatform; |
| 23 | + |
| 24 | +// [START aiplatform_batch_create_features_sample] |
| 25 | + |
| 26 | +importcom.google.api.gax.longrunning.OperationFuture; |
| 27 | +importcom.google.cloud.aiplatform.v1.BatchCreateFeaturesOperationMetadata; |
| 28 | +importcom.google.cloud.aiplatform.v1.BatchCreateFeaturesRequest; |
| 29 | +importcom.google.cloud.aiplatform.v1.BatchCreateFeaturesResponse; |
| 30 | +importcom.google.cloud.aiplatform.v1.CreateFeatureRequest; |
| 31 | +importcom.google.cloud.aiplatform.v1.EntityTypeName; |
| 32 | +importcom.google.cloud.aiplatform.v1.Feature; |
| 33 | +importcom.google.cloud.aiplatform.v1.Feature.ValueType; |
| 34 | +importcom.google.cloud.aiplatform.v1.FeaturestoreServiceClient; |
| 35 | +importcom.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; |
| 36 | +importjava.io.IOException; |
| 37 | +importjava.util.ArrayList; |
| 38 | +importjava.util.List; |
| 39 | +importjava.util.concurrent.ExecutionException; |
| 40 | +importjava.util.concurrent.TimeUnit; |
| 41 | +importjava.util.concurrent.TimeoutException; |
| 42 | + |
| 43 | +publicclassBatchCreateFeaturesSample { |
| 44 | + |
| 45 | +publicstaticvoidmain(String[]args) |
| 46 | +throwsIOException,InterruptedException,ExecutionException,TimeoutException { |
| 47 | +// TODO(developer): Replace these variables before running the sample. |
| 48 | +Stringproject ="YOUR_PROJECT_ID"; |
| 49 | +StringfeaturestoreId ="YOUR_FEATURESTORE_ID"; |
| 50 | +StringentityTypeId ="YOUR_ENTITY_TYPE_ID"; |
| 51 | +Stringlocation ="us-central1"; |
| 52 | +Stringendpoint ="us-central1-aiplatform.googleapis.com:443"; |
| 53 | +inttimeout =300; |
| 54 | +batchCreateFeaturesSample(project,featurestoreId,entityTypeId,location,endpoint,timeout); |
| 55 | + } |
| 56 | + |
| 57 | +staticvoidbatchCreateFeaturesSample( |
| 58 | +Stringproject, |
| 59 | +StringfeaturestoreId, |
| 60 | +StringentityTypeId, |
| 61 | +Stringlocation, |
| 62 | +Stringendpoint, |
| 63 | +inttimeout) |
| 64 | +throwsIOException,InterruptedException,ExecutionException,TimeoutException { |
| 65 | +FeaturestoreServiceSettingsfeaturestoreServiceSettings = |
| 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 (FeaturestoreServiceClientfeaturestoreServiceClient = |
| 72 | +FeaturestoreServiceClient.create(featurestoreServiceSettings)) { |
| 73 | + |
| 74 | +List<CreateFeatureRequest>createFeatureRequests =newArrayList<>(); |
| 75 | + |
| 76 | +FeaturetitleFeature = |
| 77 | +Feature.newBuilder() |
| 78 | + .setDescription("The title of the movie") |
| 79 | + .setValueType(ValueType.STRING) |
| 80 | + .build(); |
| 81 | +FeaturegenresFeature = |
| 82 | +Feature.newBuilder() |
| 83 | + .setDescription("The genres of the movie") |
| 84 | + .setValueType(ValueType.STRING) |
| 85 | + .build(); |
| 86 | +FeatureaverageRatingFeature = |
| 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 | +BatchCreateFeaturesRequestbatchCreateFeaturesRequest = |
| 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 | +BatchCreateFeaturesResponsebatchCreateFeaturesResponse = |
| 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] |