From bf572d956cccc2945b6607e98bcef5304994a10d Mon Sep 17 00:00:00 2001 From: sai-chaithu Date: Tue, 28 Jun 2022 14:15:12 +0530 Subject: [PATCH 1/6] feat(samples): add all feature values samples --- samples/snippets/pom.xml | 6 +- .../aiplatform/BatchCreateFeaturesSample.java | 103 +++++++ .../BatchReadFeatureValuesSample.java | 111 +++++++ .../aiplatform/ExportFeatureValuesSample.java | 98 +++++++ .../ExportFeatureValuesSnapshotSample.java | 99 +++++++ .../aiplatform/ImportFeatureValuesSample.java | 100 +++++++ .../aiplatform/FeatureValuesSamplesTest.java | 275 ++++++++++++++++++ 7 files changed, 791 insertions(+), 1 deletion(-) create mode 100644 samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java create mode 100644 samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java create mode 100644 samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java create mode 100644 samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java create mode 100644 samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java create mode 100644 samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java diff --git a/samples/snippets/pom.xml b/samples/snippets/pom.xml index 53509874d..6c4e0afae 100644 --- a/samples/snippets/pom.xml +++ b/samples/snippets/pom.xml @@ -62,6 +62,10 @@ proto-google-cloud-aiplatform-v1beta1 0.15.7 - + + com.google.cloud + google-cloud-bigquery + 2.13.6 + diff --git a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java new file mode 100644 index 000000000..afb07eaad --- /dev/null +++ b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java @@ -0,0 +1,103 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Create features in bulk for an existing entity type. See + * https://cloud.google.com/vertex-ai/docs/featurestore/setup + * before running the code snippet + */ + +package aiplatform; + +// [START aiplatform_batch_create_features_sample] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.aiplatform.v1.BatchCreateFeaturesOperationMetadata; +import com.google.cloud.aiplatform.v1.BatchCreateFeaturesRequest; +import com.google.cloud.aiplatform.v1.BatchCreateFeaturesResponse; +import com.google.cloud.aiplatform.v1.CreateFeatureRequest; +import com.google.cloud.aiplatform.v1.EntityTypeName; +import com.google.cloud.aiplatform.v1.Feature; +import com.google.cloud.aiplatform.v1.Feature.ValueType; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class BatchCreateFeaturesSample { + + public static void main(String[] args) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String project = "YOUR_PROJECT_ID"; + String featurestoreId = "YOUR_FEATURESTORE_ID"; + String entityTypeId = "YOUR_ENTITY_TYPE_ID"; + String location = "us-central1"; + String endpoint = "us-central1-aiplatform.googleapis.com:443"; + int timeout = 300; + batchCreateFeaturesSample(project, featurestoreId, entityTypeId, location, endpoint, timeout); + } + + static void batchCreateFeaturesSample(String project, String featurestoreId, String entityTypeId, + String location, String endpoint, int timeout) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + FeaturestoreServiceSettings featurestoreServiceSettings = + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (FeaturestoreServiceClient featurestoreServiceClient = + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { + + List createFeatureRequests = new ArrayList<>(); + + Feature titleFeature = Feature.newBuilder().setDescription("The title of the movie") + .setValueType(ValueType.valueOf("STRING")).build(); + Feature genresFeature = Feature.newBuilder().setDescription("The genres of the movie") + .setValueType(ValueType.valueOf("STRING")).build(); + Feature averageRatingFeature = Feature.newBuilder() + .setDescription("The average rating for the movie, range is [1.0-5.0]") + .setValueType(ValueType.valueOf("DOUBLE")).build(); + + createFeatureRequests.add( + CreateFeatureRequest.newBuilder().setFeature(titleFeature).setFeatureId("title").build()); + + createFeatureRequests.add(CreateFeatureRequest.newBuilder().setFeature(genresFeature) + .setFeatureId("genres").build()); + + createFeatureRequests.add(CreateFeatureRequest.newBuilder().setFeature(averageRatingFeature) + .setFeatureId("average_rating").build()); + + BatchCreateFeaturesRequest request = BatchCreateFeaturesRequest.newBuilder() + .setParent(EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) + .addAllRequests(createFeatureRequests).build(); + + OperationFuture future = + featurestoreServiceClient.batchCreateFeaturesAsync(request); + System.out.format("Operation name: %s%n", future.getInitialFuture().get().getName()); + System.out.println("Waiting for operation to finish..."); + BatchCreateFeaturesResponse batchCreateFeaturesResponse = + future.get(timeout, TimeUnit.SECONDS); + System.out.println("Batch Create Features Response"); + System.out.println(batchCreateFeaturesResponse); + } + } +} +// [END aiplatform_batch_create_features_sample] diff --git a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java new file mode 100644 index 000000000..88cc74c8d --- /dev/null +++ b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java @@ -0,0 +1,111 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Batch read feature values from a featurestore, as determined by your + * read instances list file, to export data. See + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +package aiplatform; + +// [START aiplatform_batch_read_feature_values_sample] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.aiplatform.v1.BatchReadFeatureValuesOperationMetadata; +import com.google.cloud.aiplatform.v1.BatchReadFeatureValuesRequest; +import com.google.cloud.aiplatform.v1.BatchReadFeatureValuesRequest.EntityTypeSpec; +import com.google.cloud.aiplatform.v1.BatchReadFeatureValuesResponse; +import com.google.cloud.aiplatform.v1.BigQueryDestination; +import com.google.cloud.aiplatform.v1.CsvSource; +import com.google.cloud.aiplatform.v1.FeatureSelector; +import com.google.cloud.aiplatform.v1.FeatureValueDestination; +import com.google.cloud.aiplatform.v1.FeaturestoreName; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; +import com.google.cloud.aiplatform.v1.GcsSource; +import com.google.cloud.aiplatform.v1.IdMatcher; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class BatchReadFeatureValuesSample { + + public static void main(String[] args) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String project = "YOUR_PROJECT_ID"; + String featurestoreId = "YOUR_FEATURESTORE_ID"; + String entityTypeId = "YOUR_ENTITY_TYPE_ID"; + String inputCsvFile = "YOU_INPUT_CSV_FILE"; + String destinationTableUri = "YOUR_DESTINATION_TABLE_URI"; + String location = "us-central1"; + String endpoint = "us-central1-aiplatform.googleapis.com:443"; + int timeout = 300; + batchReadFeatureValuesSample(project, featurestoreId, entityTypeId, inputCsvFile, + destinationTableUri, location, endpoint, timeout); + } + + static void batchReadFeatureValuesSample(String project, String featurestoreId, + String entityTypeId, String inputCsvFile, String destinationTableUri, String location, + String endpoint, int timeout) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + FeaturestoreServiceSettings featurestoreServiceSettings = + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (FeaturestoreServiceClient featurestoreServiceClient = + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { + + List entityTypeSpecs = new ArrayList<>(); + + List ids = new ArrayList<>(); + ids.add("*"); + FeatureSelector featureSelector = FeatureSelector.newBuilder() + .setIdMatcher(IdMatcher.newBuilder().addAllIds(ids).build()).build(); + EntityTypeSpec entityTypeSpec = EntityTypeSpec.newBuilder().setEntityTypeId(entityTypeId) + .setFeatureSelector(featureSelector).build(); + entityTypeSpecs.add(entityTypeSpec); + + BigQueryDestination bigQueryDestination = + BigQueryDestination.newBuilder().setOutputUri(destinationTableUri).build(); + GcsSource gcsSource = GcsSource.newBuilder().addUris(inputCsvFile).build(); + BatchReadFeatureValuesRequest batchReadFeatureValuesRequest = + BatchReadFeatureValuesRequest.newBuilder() + .setFeaturestore(FeaturestoreName.of(project, location, featurestoreId).toString()) + .setCsvReadInstances(CsvSource.newBuilder().setGcsSource(gcsSource)) + .setDestination( + FeatureValueDestination.newBuilder().setBigqueryDestination(bigQueryDestination)) + // .addAllPassThroughFields(passThroughFieldsList) + .addAllEntityTypeSpecs(entityTypeSpecs).build(); + + OperationFuture brf = + featurestoreServiceClient.batchReadFeatureValuesAsync(batchReadFeatureValuesRequest); + System.out.format("Operation name: %s%n", brf.getInitialFuture().get().getName()); + System.out.println("Waiting for operation to finish..."); + BatchReadFeatureValuesResponse batchReadFeatureValuesResponse = + brf.get(timeout, TimeUnit.SECONDS); + System.out.println("Batch Read Feature Values Response"); + System.out.println(batchReadFeatureValuesResponse); + } + } +} +// [END aiplatform_batch_read_feature_values_sample] diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java new file mode 100644 index 000000000..7e749a267 --- /dev/null +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java @@ -0,0 +1,98 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Bulk export feature values from a featurestore. See + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +package aiplatform; + +// [START aiplatform_export_feature_values_sample] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.aiplatform.v1.BigQueryDestination; +import com.google.cloud.aiplatform.v1.EntityTypeName; +import com.google.cloud.aiplatform.v1.ExportFeatureValuesOperationMetadata; +import com.google.cloud.aiplatform.v1.ExportFeatureValuesRequest; +import com.google.cloud.aiplatform.v1.ExportFeatureValuesRequest.FullExport; +import com.google.cloud.aiplatform.v1.ExportFeatureValuesResponse; +import com.google.cloud.aiplatform.v1.FeatureSelector; +import com.google.cloud.aiplatform.v1.FeatureValueDestination; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; +import com.google.cloud.aiplatform.v1.IdMatcher; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class ExportFeatureValuesSample { + + public static void main(String[] args) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String project = "YOUR_PROJECT_ID"; + String featurestoreId = "YOUR_FEATURESTORE_ID"; + String entityTypeId = "YOUR_ENTITY_TYPE_ID"; + String destinationTableUri = "YOUR_DESTINATION_TABLE_URI"; + String location = "us-central1"; + String endpoint = "us-central1-aiplatform.googleapis.com:443"; + int timeout = 300; + exportFeatureValuesSample(project, featurestoreId, entityTypeId, destinationTableUri, location, + endpoint, timeout); + } + + static void exportFeatureValuesSample(String project, String featurestoreId, String entityTypeId, + String destinationTableUri, String location, String endpoint, int timeout) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + FeaturestoreServiceSettings featurestoreServiceSettings = + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (FeaturestoreServiceClient featurestoreServiceClient = + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { + + List ids = new ArrayList<>(); + ids.add("*"); + FeatureSelector featureSelector = FeatureSelector.newBuilder() + .setIdMatcher(IdMatcher.newBuilder().addAllIds(ids).build()).build(); + + ExportFeatureValuesRequest exportFeatureValuesRequest = + ExportFeatureValuesRequest.newBuilder() + .setEntityType( + EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) + .setDestination(FeatureValueDestination.newBuilder().setBigqueryDestination( + BigQueryDestination.newBuilder().setOutputUri(destinationTableUri))) + .setFeatureSelector(featureSelector).setFullExport(FullExport.newBuilder()).build(); + + OperationFuture future = + featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest); + System.out.format("Operation name: %s%n", + future.getInitialFuture().get().getName()); + System.out.println("Waiting for operation to finish..."); + ExportFeatureValuesResponse exportFeatureValuesResponse = + future.get(timeout, TimeUnit.SECONDS); + System.out.println("Export Feature Values Response"); + System.out.println(exportFeatureValuesResponse); + } + } +} +// [END aiplatform_export_feature_values_sample] diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java new file mode 100644 index 000000000..fcdeeed09 --- /dev/null +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java @@ -0,0 +1,99 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Bulk export feature values from a featurestore. See + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +package aiplatform; + +// [START aiplatform_export_feature_values_snapshot_sample] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.aiplatform.v1.BigQueryDestination; +import com.google.cloud.aiplatform.v1.EntityTypeName; +import com.google.cloud.aiplatform.v1.ExportFeatureValuesOperationMetadata; +import com.google.cloud.aiplatform.v1.ExportFeatureValuesRequest; +import com.google.cloud.aiplatform.v1.ExportFeatureValuesRequest.SnapshotExport; +import com.google.cloud.aiplatform.v1.ExportFeatureValuesResponse; +import com.google.cloud.aiplatform.v1.FeatureSelector; +import com.google.cloud.aiplatform.v1.FeatureValueDestination; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; +import com.google.cloud.aiplatform.v1.IdMatcher; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class ExportFeatureValuesSnapshotSample { + + public static void main(String[] args) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String project = "YOUR_PROJECT_ID"; + String featurestoreId = "YOUR_FEATURESTORE_ID"; + String entityTypeId = "YOUR_ENTITY_TYPE_ID"; + String destinationTableUri = "YOUR_DESTINATION_TABLE_URI"; + String location = "us-central1"; + String endpoint = "us-central1-aiplatform.googleapis.com:443"; + int timeout = 300; + exportFeatureValuesSnapshotSample(project, featurestoreId, entityTypeId, destinationTableUri, + location, endpoint, timeout); + } + + static void exportFeatureValuesSnapshotSample(String project, String featurestoreId, + String entityTypeId, String destinationTableUri, String location, String endpoint, + int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { + FeaturestoreServiceSettings featurestoreServiceSettings = + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (FeaturestoreServiceClient featurestoreServiceClient = + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { + + List ids = new ArrayList<>(); + ids.add("*"); + FeatureSelector featureSelector = FeatureSelector.newBuilder() + .setIdMatcher(IdMatcher.newBuilder().addAllIds(ids).build()).build(); + + ExportFeatureValuesRequest exportFeatureValuesRequest = + ExportFeatureValuesRequest.newBuilder() + .setEntityType( + EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) + .setDestination(FeatureValueDestination.newBuilder().setBigqueryDestination( + BigQueryDestination.newBuilder().setOutputUri(destinationTableUri))) + .setFeatureSelector(featureSelector).setSnapshotExport(SnapshotExport.newBuilder()) + .build(); + + OperationFuture future = + featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest); + System.out.format("Operation name: %s%n", + future.getInitialFuture().get().getName()); + System.out.println("Waiting for operation to finish..."); + ExportFeatureValuesResponse exportFeatureValuesResponse = + future.get(timeout, TimeUnit.SECONDS); + System.out.println("Snapshot Export Feature Values Response"); + System.out.println(exportFeatureValuesResponse); + } + } +} +// [END aiplatform_export_feature_values_snapshot_sample] diff --git a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java new file mode 100644 index 000000000..78d84f549 --- /dev/null +++ b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java @@ -0,0 +1,100 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + * + * Bulk import values into a featurestore for existing features. See + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * the code snippet + */ + +package aiplatform; + +// [START aiplatform_import_feature_values_sample] + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.aiplatform.v1.AvroSource; +import com.google.cloud.aiplatform.v1.EntityTypeName; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; +import com.google.cloud.aiplatform.v1.GcsSource; +import com.google.cloud.aiplatform.v1.ImportFeatureValuesOperationMetadata; +import com.google.cloud.aiplatform.v1.ImportFeatureValuesRequest; +import com.google.cloud.aiplatform.v1.ImportFeatureValuesRequest.FeatureSpec; +import com.google.cloud.aiplatform.v1.ImportFeatureValuesResponse; +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; + +public class ImportFeatureValuesSample { + + public static void main(String[] args) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // TODO(developer): Replace these variables before running the sample. + String project = "YOUR_PROJECT_ID"; + String featurestoreId = "YOUR_FEATURESTORE_ID"; + String entityTypeId = "YOUR_ENTITY_TYPE_ID"; + String entityIdField = "YOUR_ENTITY_FIELD_ID"; + String featureTimeField = "YOUR_FEATURE_TIME_FIELD"; + String gcsSourceUri = "YOUR_GCS_SOURCE_URI"; + int workerCount = 2; + String location = "us-central1"; + String endpoint = "us-central1-aiplatform.googleapis.com:443"; + int timeout = 300; + importFeatureValuesSample(project, featurestoreId, entityTypeId, gcsSourceUri, entityIdField, + featureTimeField, workerCount, location, endpoint, timeout); + } + + static void importFeatureValuesSample(String project, String featurestoreId, String entityTypeId, + String gcsSourceUri, String entityIdField, String featureTimeField, int workerCount, + String location, String endpoint, int timeout) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + FeaturestoreServiceSettings featurestoreServiceSettings = + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (FeaturestoreServiceClient featurestoreServiceClient = + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { + List featureSpecs = new ArrayList<>(); + + featureSpecs.add(FeatureSpec.newBuilder().setId("title").build()); + featureSpecs.add(FeatureSpec.newBuilder().setId("genres").build()); + featureSpecs.add(FeatureSpec.newBuilder().setId("average_rating").build()); + ImportFeatureValuesRequest importFeatureValuesRequest = ImportFeatureValuesRequest + .newBuilder() + .setEntityType( + EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) + .setEntityIdField(entityIdField).setFeatureTimeField(featureTimeField) + .addAllFeatureSpecs(featureSpecs).setWorkerCount(workerCount) + .setAvroSource( + AvroSource.newBuilder().setGcsSource(GcsSource.newBuilder().addUris(gcsSourceUri))) + .build(); + OperationFuture future = + featurestoreServiceClient.importFeatureValuesAsync(importFeatureValuesRequest); + System.out.format("Operation name: %s%n", + future.getInitialFuture().get().getName()); + System.out.println("Waiting for operation to finish..."); + ImportFeatureValuesResponse importFeatureValuesResponse = + future.get(timeout, TimeUnit.SECONDS); + System.out.println("Import Feature Values Response"); + System.out.println(importFeatureValuesResponse); + } + } +} +// [END aiplatform_import_feature_values_sample] diff --git a/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java b/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java new file mode 100644 index 000000000..c4adb3462 --- /dev/null +++ b/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java @@ -0,0 +1,275 @@ +/* + * Copyright 2022 Google LLC + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package aiplatform; + +import static com.google.common.truth.Truth.assertThat; +import static junit.framework.TestCase.assertNotNull; + +import com.google.api.gax.longrunning.OperationFuture; +import com.google.cloud.aiplatform.v1.CreateEntityTypeOperationMetadata; +import com.google.cloud.aiplatform.v1.CreateEntityTypeRequest; +import com.google.cloud.aiplatform.v1.EntityType; +import com.google.cloud.aiplatform.v1.FeaturestoreName; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; +import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; +import com.google.cloud.bigquery.BigQuery; +import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; +import com.google.cloud.bigquery.BigQueryException; +import com.google.cloud.bigquery.BigQueryOptions; +import com.google.cloud.bigquery.Dataset; +import com.google.cloud.bigquery.DatasetId; +import com.google.cloud.bigquery.DatasetInfo; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.PrintStream; +import java.text.SimpleDateFormat; +import java.util.Date; +import java.util.UUID; +import java.util.concurrent.ExecutionException; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.TimeoutException; +import org.junit.After; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.junit.runners.JUnit4; + +@RunWith(JUnit4.class) +public class FeatureValuesSamplesTest { + + private static final String PROJECT_ID = System.getenv("UCAIP_PROJECT_ID"); + private static final int MIN_NODE_COUNT = 1; + private static final int MAX_NODE_COUNT = 5; + private static final String DESCRIPTION = "Test Description"; + private static final boolean USE_FORCE = true; + private static final String ENTITY_ID_FIELD = "movie_id"; + private static final String FEATURE_TIME_FIELD = "update_time"; + private static final String GCS_SOURCE_URI = + "gs://cloud-samples-data-us-central1/vertex-ai/feature-store/datasets/movies.avro"; + private static final int WORKER_COUNT = 2; + private static final String INPUT_CSV_FILE = + "gs://cloud-samples-data-us-central1/vertex-ai/feature-store/datasets/movie_prediction.csv"; + private static final String LOCATION = "us-central1"; + private static final String ENDPOINT = "us-central1-aiplatform.googleapis.com:443"; + private static final int TIMEOUT = 900; + private ByteArrayOutputStream bout; + private PrintStream out; + private PrintStream originalPrintStream; + private String featurestoreId; + private String destinationTableUri; + private Date date; + private SimpleDateFormat dateFormat; + private String datasetName; + private String destinationTableName; + + private static void requireEnvVar(String varName) { + String errorMessage = + String.format("Environment variable '%s' is required to perform these tests.", varName); + assertNotNull(errorMessage, System.getenv(varName)); + } + + @BeforeClass + public static void checkRequirements() { + requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS"); + requireEnvVar("UCAIP_PROJECT_ID"); + } + + @Before + public void setUp() { + date = new Date(); + dateFormat = new SimpleDateFormat("yyyyMMddHHmmSSS"); + datasetName = "movie_predictions" + dateFormat.format(date); + destinationTableName = "training_data"; + bout = new ByteArrayOutputStream(); + out = new PrintStream(bout); + originalPrintStream = System.out; + System.setOut(out); + } + + static void createEntityTypeSample(String project, String featurestoreId, String entityTypeId, + String description, String location, String endpoint, int timeout) + throws IOException, InterruptedException, ExecutionException, TimeoutException { + + FeaturestoreServiceSettings featurestoreServiceSettings = + FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); + + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. After completing all of your requests, call + // the "close" method on the client to safely clean up any remaining background resources. + try (FeaturestoreServiceClient featurestoreServiceClient = + FeaturestoreServiceClient.create(featurestoreServiceSettings)) { + + EntityType entityType = EntityType.newBuilder().setDescription(description).build(); + + CreateEntityTypeRequest createEntityTypeRequest = CreateEntityTypeRequest.newBuilder() + .setParent(FeaturestoreName.of(project, location, featurestoreId).toString()) + .setEntityType(entityType).setEntityTypeId(entityTypeId).build(); + + OperationFuture entityTypeFuture = + featurestoreServiceClient.createEntityTypeAsync(createEntityTypeRequest); + System.out.format("Operation name: %s%n", + entityTypeFuture.getInitialFuture().get().getName()); + System.out.println("Waiting for operation to finish..."); + EntityType entityTypeResponse = entityTypeFuture.get(timeout, TimeUnit.SECONDS); + System.out.println("Create Entity Type Response"); + System.out.format("Name: %s%n", entityTypeResponse.getName()); + } + } + + static void createBigQueryDataset(String projectId, String datasetName, String location) { + try { + // Initialize client that will be used to send requests. This client only needs + // to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.newBuilder().setLocation(location).setProjectId(projectId) + .build().getService(); + DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build(); + + Dataset newDataset = bigquery.create(datasetInfo); + String newDatasetName = newDataset.getDatasetId().getDataset(); + System.out.println(newDatasetName + " created successfully"); + } catch (BigQueryException e) { + System.out.format("Dataset was not created. %n%s", e.toString()); + } + } + + static void deleteBigQueryDataset(String projectId, String datasetName, String location) { + try { + // Initialize client that will be used to send requests. This client only needs to be created + // once, and can be reused for multiple requests. + BigQuery bigquery = BigQueryOptions.newBuilder().setLocation(location).setProjectId(projectId) + .build().getService(); + + DatasetId datasetId = DatasetId.of(projectId, datasetName); + boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); + if (success) { + System.out.println("Dataset deleted successfully"); + } else { + System.out.println("Dataset was not found"); + } + } catch (BigQueryException e) { + System.out.format("Dataset was not deleted. %n%s", e.toString()); + } + } + + @After + public void tearDown() + throws InterruptedException, ExecutionException, IOException, TimeoutException { + + // Delete the featurestore + DeleteFeaturestoreSample.deleteFeaturestoreSample(PROJECT_ID, featurestoreId, USE_FORCE, + LOCATION, ENDPOINT, 60); + + // Assert + String deleteFeaturestoreResponse = bout.toString(); + assertThat(deleteFeaturestoreResponse).contains("Deleted Featurestore"); + + // Delete the big query dataset + deleteBigQueryDataset(PROJECT_ID, datasetName, LOCATION); + + // Assert + String deleteBigQueryResponse = bout.toString(); + assertThat(deleteBigQueryResponse).contains("Dataset deleted successfully"); + + System.out.flush(); + System.setOut(originalPrintStream); + } + + @Test + public void testFeatureValuesSamples() + throws IOException, InterruptedException, ExecutionException, TimeoutException { + // Create the featurestore + String tempUuid = UUID.randomUUID().toString().replaceAll("-", "_").substring(0, 26); + String id = String.format("temp_create_featurestore_test_%s", tempUuid); + CreateFeaturestoreSample.createFeaturestoreSample(PROJECT_ID, id, MIN_NODE_COUNT, + MAX_NODE_COUNT, LOCATION, ENDPOINT, 900); + + // Assert + String createFeaturestoreResponse = bout.toString(); + assertThat(createFeaturestoreResponse).contains("Create Featurestore Response"); + featurestoreId = + createFeaturestoreResponse.split("Name: ")[1].split("featurestores/")[1].split("\n")[0] + .trim(); + + // Create the entity type + String entityTypeId = "movies"; + createEntityTypeSample(PROJECT_ID, featurestoreId, entityTypeId, DESCRIPTION, LOCATION, + ENDPOINT, TIMEOUT); + + // Assert + String createEntityTypeResponse = bout.toString(); + assertThat(createEntityTypeResponse).contains("Create Entity Type Response"); + + // Batch create features + BatchCreateFeaturesSample.batchCreateFeaturesSample(PROJECT_ID, featurestoreId, entityTypeId, + LOCATION, ENDPOINT, TIMEOUT); + + // Assert + String batchCreateFeaturesResponse = bout.toString(); + assertThat(batchCreateFeaturesResponse).contains("Batch Create Features Response"); + + // Import feature values + ImportFeatureValuesSample.importFeatureValuesSample(PROJECT_ID, featurestoreId, entityTypeId, + GCS_SOURCE_URI, ENTITY_ID_FIELD, FEATURE_TIME_FIELD, WORKER_COUNT, LOCATION, ENDPOINT, + TIMEOUT); + + // Assert + String importFeatureValuesResponse = bout.toString(); + assertThat(importFeatureValuesResponse).contains("Import Feature Values Response"); + + // Create the big query dataset + createBigQueryDataset(PROJECT_ID, datasetName, LOCATION); + destinationTableUri = + String.format("bq://%s.%s.%s_full", PROJECT_ID, datasetName, destinationTableName); + + // Assert + String createBigQueryDatasetResponse = bout.toString(); + assertThat(createBigQueryDatasetResponse).contains(datasetName + " created successfully"); + + // Export feature values + ExportFeatureValuesSample.exportFeatureValuesSample(PROJECT_ID, featurestoreId, entityTypeId, + destinationTableUri, LOCATION, ENDPOINT, TIMEOUT); + + // Assert + String exportFeatureValuesResponse = bout.toString(); + assertThat(exportFeatureValuesResponse).contains("Export Feature Values Response"); + + destinationTableUri = + String.format("bq://%s.%s.%s_snapshot", PROJECT_ID, datasetName, destinationTableName); + + // Snapshot export feature values + ExportFeatureValuesSnapshotSample.exportFeatureValuesSnapshotSample(PROJECT_ID, featurestoreId, + entityTypeId, destinationTableUri, LOCATION, ENDPOINT, TIMEOUT); + + // Assert + String snapshotResponse = bout.toString(); + assertThat(snapshotResponse).contains("Snapshot Export Feature Values Response"); + + destinationTableUri = + String.format("bq://%s.%s.%s_batchRead", PROJECT_ID, datasetName, destinationTableName); + + // Batch read feature values + BatchReadFeatureValuesSample.batchReadFeatureValuesSample(PROJECT_ID, featurestoreId, + entityTypeId, INPUT_CSV_FILE, destinationTableUri, LOCATION, ENDPOINT, TIMEOUT); + + // Assert + String batchReadFeatureValuesResponse = bout.toString(); + assertThat(batchReadFeatureValuesResponse).contains("Batch Read Feature Values Response"); + + } +} From bc3e57c64c0268ff9dc4b35a8b1fd8904908509f Mon Sep 17 00:00:00 2001 From: sai-chaithu Date: Tue, 28 Jun 2022 14:47:31 +0530 Subject: [PATCH 2/6] feat(samples): update all feature values samples --- .../src/main/java/aiplatform/BatchCreateFeaturesSample.java | 1 + .../src/main/java/aiplatform/BatchReadFeatureValuesSample.java | 1 + .../src/main/java/aiplatform/ExportFeatureValuesSample.java | 1 + .../main/java/aiplatform/ExportFeatureValuesSnapshotSample.java | 1 + .../src/main/java/aiplatform/ImportFeatureValuesSample.java | 1 + .../src/test/java/aiplatform/FeatureValuesSamplesTest.java | 1 + 6 files changed, 6 insertions(+) diff --git a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java index afb07eaad..acbd6ce22 100644 --- a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java +++ b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java @@ -101,3 +101,4 @@ static void batchCreateFeaturesSample(String project, String featurestoreId, Str } } // [END aiplatform_batch_create_features_sample] + diff --git a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java index 88cc74c8d..fddf8f443 100644 --- a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java @@ -109,3 +109,4 @@ static void batchReadFeatureValuesSample(String project, String featurestoreId, } } // [END aiplatform_batch_read_feature_values_sample] + diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java index 7e749a267..5eeeac996 100644 --- a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java @@ -96,3 +96,4 @@ static void exportFeatureValuesSample(String project, String featurestoreId, Str } } // [END aiplatform_export_feature_values_sample] + diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java index fcdeeed09..2bd8289f8 100644 --- a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java @@ -97,3 +97,4 @@ static void exportFeatureValuesSnapshotSample(String project, String featurestor } } // [END aiplatform_export_feature_values_snapshot_sample] + diff --git a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java index 78d84f549..529b86c57 100644 --- a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java @@ -98,3 +98,4 @@ static void importFeatureValuesSample(String project, String featurestoreId, Str } } // [END aiplatform_import_feature_values_sample] + diff --git a/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java b/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java index c4adb3462..8374a443d 100644 --- a/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java +++ b/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java @@ -273,3 +273,4 @@ public void testFeatureValuesSamples() } } + From 1a6146c25295312c0026452fde5015ef76fd6798 Mon Sep 17 00:00:00 2001 From: sai-chaithu Date: Wed, 29 Jun 2022 15:23:50 +0530 Subject: [PATCH 3/6] feat(samples): update big query dependency in pom.xml --- samples/install-without-bom/pom.xml | 5 +++++ samples/snapshot/pom.xml | 5 +++++ 2 files changed, 10 insertions(+) diff --git a/samples/install-without-bom/pom.xml b/samples/install-without-bom/pom.xml index ab713f9b9..3b002dc0c 100644 --- a/samples/install-without-bom/pom.xml +++ b/samples/install-without-bom/pom.xml @@ -54,6 +54,11 @@ 1.1.3 test + + com.google.cloud + google-cloud-bigquery + 2.13.6 + diff --git a/samples/snapshot/pom.xml b/samples/snapshot/pom.xml index 8d3ceef37..afc5cadcc 100644 --- a/samples/snapshot/pom.xml +++ b/samples/snapshot/pom.xml @@ -54,6 +54,11 @@ 1.1.3 test + + com.google.cloud + google-cloud-bigquery + 2.13.6 + From 71bdbce7e52c8f15cd093dd5496f87b5a117979f Mon Sep 17 00:00:00 2001 From: sai-chaithu Date: Fri, 8 Jul 2022 12:38:28 +0530 Subject: [PATCH 4/6] feat(samples): code review changes --- .../aiplatform/BatchCreateFeaturesSample.java | 19 +++++++++++-------- .../BatchReadFeatureValuesSample.java | 11 ++++++----- .../aiplatform/ExportFeatureValuesSample.java | 19 +++++++++---------- .../ExportFeatureValuesSnapshotSample.java | 19 +++++++++---------- .../aiplatform/ImportFeatureValuesSample.java | 9 +++++---- 5 files changed, 40 insertions(+), 37 deletions(-) diff --git a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java index acbd6ce22..8a99237bc 100644 --- a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java +++ b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java @@ -69,12 +69,12 @@ static void batchCreateFeaturesSample(String project, String featurestoreId, Str List createFeatureRequests = new ArrayList<>(); Feature titleFeature = Feature.newBuilder().setDescription("The title of the movie") - .setValueType(ValueType.valueOf("STRING")).build(); + .setValueType(ValueType.STRING).build(); Feature genresFeature = Feature.newBuilder().setDescription("The genres of the movie") - .setValueType(ValueType.valueOf("STRING")).build(); + .setValueType(ValueType.STRING).build(); Feature averageRatingFeature = Feature.newBuilder() .setDescription("The average rating for the movie, range is [1.0-5.0]") - .setValueType(ValueType.valueOf("DOUBLE")).build(); + .setValueType(ValueType.DOUBLE).build(); createFeatureRequests.add( CreateFeatureRequest.newBuilder().setFeature(titleFeature).setFeatureId("title").build()); @@ -85,16 +85,19 @@ static void batchCreateFeaturesSample(String project, String featurestoreId, Str createFeatureRequests.add(CreateFeatureRequest.newBuilder().setFeature(averageRatingFeature) .setFeatureId("average_rating").build()); - BatchCreateFeaturesRequest request = BatchCreateFeaturesRequest.newBuilder() + BatchCreateFeaturesRequest batchCreateFeaturesRequest = BatchCreateFeaturesRequest + .newBuilder() .setParent(EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) .addAllRequests(createFeatureRequests).build(); - OperationFuture future = - featurestoreServiceClient.batchCreateFeaturesAsync(request); - System.out.format("Operation name: %s%n", future.getInitialFuture().get().getName()); + OperationFuture + batchCreateFeaturesFuture = + featurestoreServiceClient.batchCreateFeaturesAsync(batchCreateFeaturesRequest); + System.out.format("Operation name: %s%n", + batchCreateFeaturesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); BatchCreateFeaturesResponse batchCreateFeaturesResponse = - future.get(timeout, TimeUnit.SECONDS); + batchCreateFeaturesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Batch Create Features Response"); System.out.println(batchCreateFeaturesResponse); } diff --git a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java index fddf8f443..ebc535214 100644 --- a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java @@ -94,15 +94,16 @@ static void batchReadFeatureValuesSample(String project, String featurestoreId, .setCsvReadInstances(CsvSource.newBuilder().setGcsSource(gcsSource)) .setDestination( FeatureValueDestination.newBuilder().setBigqueryDestination(bigQueryDestination)) - // .addAllPassThroughFields(passThroughFieldsList) .addAllEntityTypeSpecs(entityTypeSpecs).build(); - OperationFuture brf = - featurestoreServiceClient.batchReadFeatureValuesAsync(batchReadFeatureValuesRequest); - System.out.format("Operation name: %s%n", brf.getInitialFuture().get().getName()); + OperationFuture + batchReadFeatureValuesFuture = + featurestoreServiceClient.batchReadFeatureValuesAsync(batchReadFeatureValuesRequest); + System.out.format("Operation name: %s%n", + batchReadFeatureValuesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); BatchReadFeatureValuesResponse batchReadFeatureValuesResponse = - brf.get(timeout, TimeUnit.SECONDS); + batchReadFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Batch Read Feature Values Response"); System.out.println(batchReadFeatureValuesResponse); } diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java index 5eeeac996..990a92788 100644 --- a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java @@ -36,8 +36,7 @@ import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; import com.google.cloud.aiplatform.v1.IdMatcher; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -70,10 +69,9 @@ static void exportFeatureValuesSample(String project, String featurestoreId, Str try (FeaturestoreServiceClient featurestoreServiceClient = FeaturestoreServiceClient.create(featurestoreServiceSettings)) { - List ids = new ArrayList<>(); - ids.add("*"); - FeatureSelector featureSelector = FeatureSelector.newBuilder() - .setIdMatcher(IdMatcher.newBuilder().addAllIds(ids).build()).build(); + FeatureSelector featureSelector = + FeatureSelector.newBuilder().setIdMatcher(IdMatcher.newBuilder() + .addAllIds(Arrays.asList("title", "genres", "average_rating")).build()).build(); ExportFeatureValuesRequest exportFeatureValuesRequest = ExportFeatureValuesRequest.newBuilder() @@ -83,13 +81,14 @@ static void exportFeatureValuesSample(String project, String featurestoreId, Str BigQueryDestination.newBuilder().setOutputUri(destinationTableUri))) .setFeatureSelector(featureSelector).setFullExport(FullExport.newBuilder()).build(); - OperationFuture future = - featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest); + OperationFuture + exportFeatureValuesFuture = + featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest); System.out.format("Operation name: %s%n", - future.getInitialFuture().get().getName()); + exportFeatureValuesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); ExportFeatureValuesResponse exportFeatureValuesResponse = - future.get(timeout, TimeUnit.SECONDS); + exportFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Export Feature Values Response"); System.out.println(exportFeatureValuesResponse); } diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java index 2bd8289f8..a5a623e2b 100644 --- a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java @@ -36,8 +36,7 @@ import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; import com.google.cloud.aiplatform.v1.IdMatcher; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; +import java.util.Arrays; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -70,10 +69,9 @@ static void exportFeatureValuesSnapshotSample(String project, String featurestor try (FeaturestoreServiceClient featurestoreServiceClient = FeaturestoreServiceClient.create(featurestoreServiceSettings)) { - List ids = new ArrayList<>(); - ids.add("*"); - FeatureSelector featureSelector = FeatureSelector.newBuilder() - .setIdMatcher(IdMatcher.newBuilder().addAllIds(ids).build()).build(); + FeatureSelector featureSelector = + FeatureSelector.newBuilder().setIdMatcher(IdMatcher.newBuilder() + .addAllIds(Arrays.asList("title", "genres", "average_rating")).build()).build(); ExportFeatureValuesRequest exportFeatureValuesRequest = ExportFeatureValuesRequest.newBuilder() @@ -84,13 +82,14 @@ static void exportFeatureValuesSnapshotSample(String project, String featurestor .setFeatureSelector(featureSelector).setSnapshotExport(SnapshotExport.newBuilder()) .build(); - OperationFuture future = - featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest); + OperationFuture + exportFeatureValuesFuture = + featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest); System.out.format("Operation name: %s%n", - future.getInitialFuture().get().getName()); + exportFeatureValuesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); ExportFeatureValuesResponse exportFeatureValuesResponse = - future.get(timeout, TimeUnit.SECONDS); + exportFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Snapshot Export Feature Values Response"); System.out.println(exportFeatureValuesResponse); } diff --git a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java index 529b86c57..28e3e43df 100644 --- a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java @@ -85,13 +85,14 @@ static void importFeatureValuesSample(String project, String featurestoreId, Str .setAvroSource( AvroSource.newBuilder().setGcsSource(GcsSource.newBuilder().addUris(gcsSourceUri))) .build(); - OperationFuture future = - featurestoreServiceClient.importFeatureValuesAsync(importFeatureValuesRequest); + OperationFuture + importFeatureValuesFuture = + featurestoreServiceClient.importFeatureValuesAsync(importFeatureValuesRequest); System.out.format("Operation name: %s%n", - future.getInitialFuture().get().getName()); + importFeatureValuesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); ImportFeatureValuesResponse importFeatureValuesResponse = - future.get(timeout, TimeUnit.SECONDS); + importFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Import Feature Values Response"); System.out.println(importFeatureValuesResponse); } From 4fdda940de11473b26052f20a5a5739dde4b0ab6 Mon Sep 17 00:00:00 2001 From: sai-chaithu Date: Thu, 14 Jul 2022 15:15:29 +0530 Subject: [PATCH 5/6] feat(samples): updated feature values samples with close method call and list variable --- .../aiplatform/BatchCreateFeaturesSample.java | 1 + .../BatchReadFeatureValuesSample.java | 14 +++-- .../aiplatform/ExportFeatureValuesSample.java | 16 +++--- .../ExportFeatureValuesSnapshotSample.java | 15 +++-- .../aiplatform/ImportFeatureValuesSample.java | 1 + .../aiplatform/FeatureValuesSamplesTest.java | 55 ++++--------------- 6 files changed, 39 insertions(+), 63 deletions(-) diff --git a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java index 8a99237bc..be62ef81e 100644 --- a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java +++ b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java @@ -100,6 +100,7 @@ static void batchCreateFeaturesSample(String project, String featurestoreId, Str batchCreateFeaturesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Batch Create Features Response"); System.out.println(batchCreateFeaturesResponse); + featurestoreServiceClient.close(); } } } diff --git a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java index ebc535214..7ab786e35 100644 --- a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java @@ -40,6 +40,7 @@ import com.google.cloud.aiplatform.v1.IdMatcher; import java.io.IOException; import java.util.ArrayList; +import java.util.Arrays; import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; @@ -55,16 +56,17 @@ public static void main(String[] args) String entityTypeId = "YOUR_ENTITY_TYPE_ID"; String inputCsvFile = "YOU_INPUT_CSV_FILE"; String destinationTableUri = "YOUR_DESTINATION_TABLE_URI"; + List featureSelectorIds = Arrays.asList("title", "genres", "average_rating"); String location = "us-central1"; String endpoint = "us-central1-aiplatform.googleapis.com:443"; int timeout = 300; batchReadFeatureValuesSample(project, featurestoreId, entityTypeId, inputCsvFile, - destinationTableUri, location, endpoint, timeout); + destinationTableUri, featureSelectorIds, location, endpoint, timeout); } static void batchReadFeatureValuesSample(String project, String featurestoreId, - String entityTypeId, String inputCsvFile, String destinationTableUri, String location, - String endpoint, int timeout) + String entityTypeId, String inputCsvFile, String destinationTableUri, + List featureSelectorIds, String location, String endpoint, int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { FeaturestoreServiceSettings featurestoreServiceSettings = FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); @@ -77,12 +79,11 @@ static void batchReadFeatureValuesSample(String project, String featurestoreId, List entityTypeSpecs = new ArrayList<>(); - List ids = new ArrayList<>(); - ids.add("*"); FeatureSelector featureSelector = FeatureSelector.newBuilder() - .setIdMatcher(IdMatcher.newBuilder().addAllIds(ids).build()).build(); + .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()).build(); EntityTypeSpec entityTypeSpec = EntityTypeSpec.newBuilder().setEntityTypeId(entityTypeId) .setFeatureSelector(featureSelector).build(); + entityTypeSpecs.add(entityTypeSpec); BigQueryDestination bigQueryDestination = @@ -106,6 +107,7 @@ static void batchReadFeatureValuesSample(String project, String featurestoreId, batchReadFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Batch Read Feature Values Response"); System.out.println(batchReadFeatureValuesResponse); + featurestoreServiceClient.close(); } } } diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java index 990a92788..1b1f9ffd5 100644 --- a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java @@ -37,6 +37,7 @@ import com.google.cloud.aiplatform.v1.IdMatcher; import java.io.IOException; import java.util.Arrays; +import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -50,16 +51,17 @@ public static void main(String[] args) String featurestoreId = "YOUR_FEATURESTORE_ID"; String entityTypeId = "YOUR_ENTITY_TYPE_ID"; String destinationTableUri = "YOUR_DESTINATION_TABLE_URI"; + List featureSelectorIds = Arrays.asList("title", "genres", "average_rating"); String location = "us-central1"; String endpoint = "us-central1-aiplatform.googleapis.com:443"; int timeout = 300; - exportFeatureValuesSample(project, featurestoreId, entityTypeId, destinationTableUri, location, - endpoint, timeout); + exportFeatureValuesSample(project, featurestoreId, entityTypeId, destinationTableUri, + featureSelectorIds, location, endpoint, timeout); } static void exportFeatureValuesSample(String project, String featurestoreId, String entityTypeId, - String destinationTableUri, String location, String endpoint, int timeout) - throws IOException, InterruptedException, ExecutionException, TimeoutException { + String destinationTableUri, List featureSelectorIds, String location, String endpoint, + int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { FeaturestoreServiceSettings featurestoreServiceSettings = FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); @@ -69,9 +71,8 @@ static void exportFeatureValuesSample(String project, String featurestoreId, Str try (FeaturestoreServiceClient featurestoreServiceClient = FeaturestoreServiceClient.create(featurestoreServiceSettings)) { - FeatureSelector featureSelector = - FeatureSelector.newBuilder().setIdMatcher(IdMatcher.newBuilder() - .addAllIds(Arrays.asList("title", "genres", "average_rating")).build()).build(); + FeatureSelector featureSelector = FeatureSelector.newBuilder() + .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()).build(); ExportFeatureValuesRequest exportFeatureValuesRequest = ExportFeatureValuesRequest.newBuilder() @@ -91,6 +92,7 @@ static void exportFeatureValuesSample(String project, String featurestoreId, Str exportFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Export Feature Values Response"); System.out.println(exportFeatureValuesResponse); + featurestoreServiceClient.close(); } } } diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java index a5a623e2b..03a99574b 100644 --- a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java @@ -37,6 +37,7 @@ import com.google.cloud.aiplatform.v1.IdMatcher; import java.io.IOException; import java.util.Arrays; +import java.util.List; import java.util.concurrent.ExecutionException; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; @@ -50,16 +51,18 @@ public static void main(String[] args) String featurestoreId = "YOUR_FEATURESTORE_ID"; String entityTypeId = "YOUR_ENTITY_TYPE_ID"; String destinationTableUri = "YOUR_DESTINATION_TABLE_URI"; + List featureSelectorIds = Arrays.asList("title", "genres", "average_rating"); String location = "us-central1"; String endpoint = "us-central1-aiplatform.googleapis.com:443"; int timeout = 300; exportFeatureValuesSnapshotSample(project, featurestoreId, entityTypeId, destinationTableUri, - location, endpoint, timeout); + featureSelectorIds, location, endpoint, timeout); } static void exportFeatureValuesSnapshotSample(String project, String featurestoreId, - String entityTypeId, String destinationTableUri, String location, String endpoint, - int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { + String entityTypeId, String destinationTableUri, List featureSelectorIds, + String location, String endpoint, int timeout) + throws IOException, InterruptedException, ExecutionException, TimeoutException { FeaturestoreServiceSettings featurestoreServiceSettings = FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); @@ -69,9 +72,8 @@ static void exportFeatureValuesSnapshotSample(String project, String featurestor try (FeaturestoreServiceClient featurestoreServiceClient = FeaturestoreServiceClient.create(featurestoreServiceSettings)) { - FeatureSelector featureSelector = - FeatureSelector.newBuilder().setIdMatcher(IdMatcher.newBuilder() - .addAllIds(Arrays.asList("title", "genres", "average_rating")).build()).build(); + FeatureSelector featureSelector = FeatureSelector.newBuilder() + .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()).build(); ExportFeatureValuesRequest exportFeatureValuesRequest = ExportFeatureValuesRequest.newBuilder() @@ -92,6 +94,7 @@ static void exportFeatureValuesSnapshotSample(String project, String featurestor exportFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Snapshot Export Feature Values Response"); System.out.println(exportFeatureValuesResponse); + featurestoreServiceClient.close(); } } } diff --git a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java index 28e3e43df..833b56594 100644 --- a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java @@ -95,6 +95,7 @@ static void importFeatureValuesSample(String project, String featurestoreId, Str importFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); System.out.println("Import Feature Values Response"); System.out.println(importFeatureValuesResponse); + featurestoreServiceClient.close(); } } } diff --git a/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java b/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java index 8374a443d..53161a41a 100644 --- a/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java +++ b/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java @@ -19,13 +19,6 @@ import static com.google.common.truth.Truth.assertThat; import static junit.framework.TestCase.assertNotNull; -import com.google.api.gax.longrunning.OperationFuture; -import com.google.cloud.aiplatform.v1.CreateEntityTypeOperationMetadata; -import com.google.cloud.aiplatform.v1.CreateEntityTypeRequest; -import com.google.cloud.aiplatform.v1.EntityType; -import com.google.cloud.aiplatform.v1.FeaturestoreName; -import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient; -import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings; import com.google.cloud.bigquery.BigQuery; import com.google.cloud.bigquery.BigQuery.DatasetDeleteOption; import com.google.cloud.bigquery.BigQueryException; @@ -37,10 +30,11 @@ import java.io.IOException; import java.io.PrintStream; import java.text.SimpleDateFormat; +import java.util.Arrays; import java.util.Date; +import java.util.List; import java.util.UUID; import java.util.concurrent.ExecutionException; -import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; import org.junit.After; import org.junit.Before; @@ -64,6 +58,8 @@ public class FeatureValuesSamplesTest { private static final int WORKER_COUNT = 2; private static final String INPUT_CSV_FILE = "gs://cloud-samples-data-us-central1/vertex-ai/feature-store/datasets/movie_prediction.csv"; + private static final List FEATURE_SELECTOR_IDS = + Arrays.asList("title", "genres", "average_rating"); private static final String LOCATION = "us-central1"; private static final String ENDPOINT = "us-central1-aiplatform.googleapis.com:443"; private static final int TIMEOUT = 900; @@ -101,36 +97,6 @@ public void setUp() { System.setOut(out); } - static void createEntityTypeSample(String project, String featurestoreId, String entityTypeId, - String description, String location, String endpoint, int timeout) - throws IOException, InterruptedException, ExecutionException, TimeoutException { - - FeaturestoreServiceSettings featurestoreServiceSettings = - FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); - - // Initialize client that will be used to send requests. This client only needs to be created - // once, and can be reused for multiple requests. After completing all of your requests, call - // the "close" method on the client to safely clean up any remaining background resources. - try (FeaturestoreServiceClient featurestoreServiceClient = - FeaturestoreServiceClient.create(featurestoreServiceSettings)) { - - EntityType entityType = EntityType.newBuilder().setDescription(description).build(); - - CreateEntityTypeRequest createEntityTypeRequest = CreateEntityTypeRequest.newBuilder() - .setParent(FeaturestoreName.of(project, location, featurestoreId).toString()) - .setEntityType(entityType).setEntityTypeId(entityTypeId).build(); - - OperationFuture entityTypeFuture = - featurestoreServiceClient.createEntityTypeAsync(createEntityTypeRequest); - System.out.format("Operation name: %s%n", - entityTypeFuture.getInitialFuture().get().getName()); - System.out.println("Waiting for operation to finish..."); - EntityType entityTypeResponse = entityTypeFuture.get(timeout, TimeUnit.SECONDS); - System.out.println("Create Entity Type Response"); - System.out.format("Name: %s%n", entityTypeResponse.getName()); - } - } - static void createBigQueryDataset(String projectId, String datasetName, String location) { try { // Initialize client that will be used to send requests. This client only needs @@ -173,7 +139,7 @@ public void tearDown() // Delete the featurestore DeleteFeaturestoreSample.deleteFeaturestoreSample(PROJECT_ID, featurestoreId, USE_FORCE, - LOCATION, ENDPOINT, 60); + LOCATION, ENDPOINT, 300); // Assert String deleteFeaturestoreResponse = bout.toString(); @@ -208,8 +174,8 @@ public void testFeatureValuesSamples() // Create the entity type String entityTypeId = "movies"; - createEntityTypeSample(PROJECT_ID, featurestoreId, entityTypeId, DESCRIPTION, LOCATION, - ENDPOINT, TIMEOUT); + CreateEntityTypeSample.createEntityTypeSample(PROJECT_ID, featurestoreId, entityTypeId, + DESCRIPTION, LOCATION, ENDPOINT, 900); // Assert String createEntityTypeResponse = bout.toString(); @@ -243,7 +209,7 @@ public void testFeatureValuesSamples() // Export feature values ExportFeatureValuesSample.exportFeatureValuesSample(PROJECT_ID, featurestoreId, entityTypeId, - destinationTableUri, LOCATION, ENDPOINT, TIMEOUT); + destinationTableUri, FEATURE_SELECTOR_IDS, LOCATION, ENDPOINT, TIMEOUT); // Assert String exportFeatureValuesResponse = bout.toString(); @@ -254,7 +220,7 @@ public void testFeatureValuesSamples() // Snapshot export feature values ExportFeatureValuesSnapshotSample.exportFeatureValuesSnapshotSample(PROJECT_ID, featurestoreId, - entityTypeId, destinationTableUri, LOCATION, ENDPOINT, TIMEOUT); + entityTypeId, destinationTableUri, FEATURE_SELECTOR_IDS, LOCATION, ENDPOINT, TIMEOUT); // Assert String snapshotResponse = bout.toString(); @@ -265,7 +231,8 @@ public void testFeatureValuesSamples() // Batch read feature values BatchReadFeatureValuesSample.batchReadFeatureValuesSample(PROJECT_ID, featurestoreId, - entityTypeId, INPUT_CSV_FILE, destinationTableUri, LOCATION, ENDPOINT, TIMEOUT); + entityTypeId, INPUT_CSV_FILE, destinationTableUri, FEATURE_SELECTOR_IDS, LOCATION, ENDPOINT, + TIMEOUT); // Assert String batchReadFeatureValuesResponse = bout.toString(); From 07cc158cf5eb2bc6caffbd47169e1756ce20f496 Mon Sep 17 00:00:00 2001 From: Owl Bot Date: Wed, 3 Aug 2022 14:36:26 +0000 Subject: [PATCH 6/6] =?UTF-8?q?=F0=9F=A6=89=20Updates=20from=20OwlBot=20po?= =?UTF-8?q?st-processor?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --- README.md | 9 ++- .../aiplatform/BatchCreateFeaturesSample.java | 66 ++++++++++------ .../BatchReadFeatureValuesSample.java | 56 ++++++++----- .../aiplatform/ExportFeatureValuesSample.java | 51 ++++++++---- .../ExportFeatureValuesSnapshotSample.java | 49 ++++++++---- .../aiplatform/ImportFeatureValuesSample.java | 59 +++++++++----- .../aiplatform/FeatureValuesSamplesTest.java | 79 +++++++++++++------ 7 files changed, 252 insertions(+), 117 deletions(-) diff --git a/README.md b/README.md index 8820b7d4a..622fcf993 100644 --- a/README.md +++ b/README.md @@ -45,13 +45,13 @@ implementation 'com.google.cloud:google-cloud-aiplatform' If you are using Gradle without BOM, add this to your dependencies: ```Groovy -implementation 'com.google.cloud:google-cloud-aiplatform:3.0.0' +implementation 'com.google.cloud:google-cloud-aiplatform:3.1.0' ``` If you are using SBT, add this to your dependencies: ```Scala -libraryDependencies += "com.google.cloud" % "google-cloud-aiplatform" % "3.0.0" +libraryDependencies += "com.google.cloud" % "google-cloud-aiplatform" % "3.1.0" ``` ## Authentication @@ -95,6 +95,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-aiplatform/tr | Sample | Source Code | Try it | | --------------------------- | --------------------------------- | ------ | +| Batch Create Features Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java) | +| Batch Read Feature Values Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java) | | Cancel Batch Prediction Job Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CancelBatchPredictionJobSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/CancelBatchPredictionJobSample.java) | | Cancel Data Labeling Job Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CancelDataLabelingJobSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/CancelDataLabelingJobSample.java) | | Cancel Training Pipeline Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CancelTrainingPipelineSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/CancelTrainingPipelineSample.java) | @@ -148,6 +150,8 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-aiplatform/tr | Delete Training Pipeline Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/DeleteTrainingPipelineSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/DeleteTrainingPipelineSample.java) | | Deploy Model Custom Trained Model Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/DeployModelCustomTrainedModelSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/DeployModelCustomTrainedModelSample.java) | | Deploy Model Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/DeployModelSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/DeployModelSample.java) | +| Export Feature Values Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java) | +| Export Feature Values Snapshot Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java) | | Export Model Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ExportModelSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ExportModelSample.java) | | Export Model Tabular Classification Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ExportModelTabularClassificationSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ExportModelTabularClassificationSample.java) | | Export Model Video Action Recognition Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ExportModelVideoActionRecognitionSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ExportModelVideoActionRecognitionSample.java) | @@ -177,6 +181,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-aiplatform/tr | Import Data Video Action Recognition Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ImportDataVideoActionRecognitionSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ImportDataVideoActionRecognitionSample.java) | | Import Data Video Classification Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ImportDataVideoClassificationSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ImportDataVideoClassificationSample.java) | | Import Data Video Object Tracking Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ImportDataVideoObjectTrackingSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ImportDataVideoObjectTrackingSample.java) | +| Import Feature Values Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java) | | List Entity Types Async Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ListEntityTypesAsyncSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ListEntityTypesAsyncSample.java) | | List Entity Types Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ListEntityTypesSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ListEntityTypesSample.java) | | List Featurestores Async Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/ListFeaturestoresAsyncSample.java) | [![Open in Cloud Shell][shell_img]](https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/googleapis/java-aiplatform&page=editor&open_in_editor=samples/snippets/src/main/java/aiplatform/ListFeaturestoresAsyncSample.java) | diff --git a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java index be62ef81e..8b9480927 100644 --- a/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java +++ b/samples/snippets/src/main/java/aiplatform/BatchCreateFeaturesSample.java @@ -15,7 +15,7 @@ * * * Create features in bulk for an existing entity type. See - * https://cloud.google.com/vertex-ai/docs/featurestore/setup + * https://cloud.google.com/vertex-ai/docs/featurestore/setup * before running the code snippet */ @@ -54,8 +54,13 @@ public static void main(String[] args) batchCreateFeaturesSample(project, featurestoreId, entityTypeId, location, endpoint, timeout); } - static void batchCreateFeaturesSample(String project, String featurestoreId, String entityTypeId, - String location, String endpoint, int timeout) + static void batchCreateFeaturesSample( + String project, + String featurestoreId, + String entityTypeId, + String location, + String endpoint, + int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { FeaturestoreServiceSettings featurestoreServiceSettings = FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); @@ -68,33 +73,49 @@ static void batchCreateFeaturesSample(String project, String featurestoreId, Str List createFeatureRequests = new ArrayList<>(); - Feature titleFeature = Feature.newBuilder().setDescription("The title of the movie") - .setValueType(ValueType.STRING).build(); - Feature genresFeature = Feature.newBuilder().setDescription("The genres of the movie") - .setValueType(ValueType.STRING).build(); - Feature averageRatingFeature = Feature.newBuilder() - .setDescription("The average rating for the movie, range is [1.0-5.0]") - .setValueType(ValueType.DOUBLE).build(); + Feature titleFeature = + Feature.newBuilder() + .setDescription("The title of the movie") + .setValueType(ValueType.STRING) + .build(); + Feature genresFeature = + Feature.newBuilder() + .setDescription("The genres of the movie") + .setValueType(ValueType.STRING) + .build(); + Feature averageRatingFeature = + Feature.newBuilder() + .setDescription("The average rating for the movie, range is [1.0-5.0]") + .setValueType(ValueType.DOUBLE) + .build(); createFeatureRequests.add( CreateFeatureRequest.newBuilder().setFeature(titleFeature).setFeatureId("title").build()); - createFeatureRequests.add(CreateFeatureRequest.newBuilder().setFeature(genresFeature) - .setFeatureId("genres").build()); + createFeatureRequests.add( + CreateFeatureRequest.newBuilder() + .setFeature(genresFeature) + .setFeatureId("genres") + .build()); - createFeatureRequests.add(CreateFeatureRequest.newBuilder().setFeature(averageRatingFeature) - .setFeatureId("average_rating").build()); + createFeatureRequests.add( + CreateFeatureRequest.newBuilder() + .setFeature(averageRatingFeature) + .setFeatureId("average_rating") + .build()); - BatchCreateFeaturesRequest batchCreateFeaturesRequest = BatchCreateFeaturesRequest - .newBuilder() - .setParent(EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) - .addAllRequests(createFeatureRequests).build(); + BatchCreateFeaturesRequest batchCreateFeaturesRequest = + BatchCreateFeaturesRequest.newBuilder() + .setParent( + EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) + .addAllRequests(createFeatureRequests) + .build(); - OperationFuture - batchCreateFeaturesFuture = + OperationFuture + batchCreateFeaturesFuture = featurestoreServiceClient.batchCreateFeaturesAsync(batchCreateFeaturesRequest); - System.out.format("Operation name: %s%n", - batchCreateFeaturesFuture.getInitialFuture().get().getName()); + System.out.format( + "Operation name: %s%n", batchCreateFeaturesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); BatchCreateFeaturesResponse batchCreateFeaturesResponse = batchCreateFeaturesFuture.get(timeout, TimeUnit.SECONDS); @@ -105,4 +126,3 @@ static void batchCreateFeaturesSample(String project, String featurestoreId, Str } } // [END aiplatform_batch_create_features_sample] - diff --git a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java index 7ab786e35..a76c3388d 100644 --- a/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/BatchReadFeatureValuesSample.java @@ -14,9 +14,9 @@ * limitations under the License. * * - * Batch read feature values from a featurestore, as determined by your - * read instances list file, to export data. See - * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * Batch read feature values from a featurestore, as determined by your + * read instances list file, to export data. See + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running * the code snippet */ @@ -60,13 +60,28 @@ public static void main(String[] args) String location = "us-central1"; String endpoint = "us-central1-aiplatform.googleapis.com:443"; int timeout = 300; - batchReadFeatureValuesSample(project, featurestoreId, entityTypeId, inputCsvFile, - destinationTableUri, featureSelectorIds, location, endpoint, timeout); + batchReadFeatureValuesSample( + project, + featurestoreId, + entityTypeId, + inputCsvFile, + destinationTableUri, + featureSelectorIds, + location, + endpoint, + timeout); } - static void batchReadFeatureValuesSample(String project, String featurestoreId, - String entityTypeId, String inputCsvFile, String destinationTableUri, - List featureSelectorIds, String location, String endpoint, int timeout) + static void batchReadFeatureValuesSample( + String project, + String featurestoreId, + String entityTypeId, + String inputCsvFile, + String destinationTableUri, + List featureSelectorIds, + String location, + String endpoint, + int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { FeaturestoreServiceSettings featurestoreServiceSettings = FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); @@ -79,10 +94,15 @@ static void batchReadFeatureValuesSample(String project, String featurestoreId, List entityTypeSpecs = new ArrayList<>(); - FeatureSelector featureSelector = FeatureSelector.newBuilder() - .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()).build(); - EntityTypeSpec entityTypeSpec = EntityTypeSpec.newBuilder().setEntityTypeId(entityTypeId) - .setFeatureSelector(featureSelector).build(); + FeatureSelector featureSelector = + FeatureSelector.newBuilder() + .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()) + .build(); + EntityTypeSpec entityTypeSpec = + EntityTypeSpec.newBuilder() + .setEntityTypeId(entityTypeId) + .setFeatureSelector(featureSelector) + .build(); entityTypeSpecs.add(entityTypeSpec); @@ -95,13 +115,14 @@ static void batchReadFeatureValuesSample(String project, String featurestoreId, .setCsvReadInstances(CsvSource.newBuilder().setGcsSource(gcsSource)) .setDestination( FeatureValueDestination.newBuilder().setBigqueryDestination(bigQueryDestination)) - .addAllEntityTypeSpecs(entityTypeSpecs).build(); + .addAllEntityTypeSpecs(entityTypeSpecs) + .build(); - OperationFuture - batchReadFeatureValuesFuture = + OperationFuture + batchReadFeatureValuesFuture = featurestoreServiceClient.batchReadFeatureValuesAsync(batchReadFeatureValuesRequest); - System.out.format("Operation name: %s%n", - batchReadFeatureValuesFuture.getInitialFuture().get().getName()); + System.out.format( + "Operation name: %s%n", batchReadFeatureValuesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); BatchReadFeatureValuesResponse batchReadFeatureValuesResponse = batchReadFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); @@ -112,4 +133,3 @@ static void batchReadFeatureValuesSample(String project, String featurestoreId, } } // [END aiplatform_batch_read_feature_values_sample] - diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java index 1b1f9ffd5..6bb7b00d6 100644 --- a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSample.java @@ -15,7 +15,7 @@ * * * Bulk export feature values from a featurestore. See - * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running * the code snippet */ @@ -55,13 +55,27 @@ public static void main(String[] args) String location = "us-central1"; String endpoint = "us-central1-aiplatform.googleapis.com:443"; int timeout = 300; - exportFeatureValuesSample(project, featurestoreId, entityTypeId, destinationTableUri, - featureSelectorIds, location, endpoint, timeout); + exportFeatureValuesSample( + project, + featurestoreId, + entityTypeId, + destinationTableUri, + featureSelectorIds, + location, + endpoint, + timeout); } - static void exportFeatureValuesSample(String project, String featurestoreId, String entityTypeId, - String destinationTableUri, List featureSelectorIds, String location, String endpoint, - int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { + static void exportFeatureValuesSample( + String project, + String featurestoreId, + String entityTypeId, + String destinationTableUri, + List featureSelectorIds, + String location, + String endpoint, + int timeout) + throws IOException, InterruptedException, ExecutionException, TimeoutException { FeaturestoreServiceSettings featurestoreServiceSettings = FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); @@ -71,22 +85,28 @@ static void exportFeatureValuesSample(String project, String featurestoreId, Str try (FeaturestoreServiceClient featurestoreServiceClient = FeaturestoreServiceClient.create(featurestoreServiceSettings)) { - FeatureSelector featureSelector = FeatureSelector.newBuilder() - .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()).build(); + FeatureSelector featureSelector = + FeatureSelector.newBuilder() + .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()) + .build(); ExportFeatureValuesRequest exportFeatureValuesRequest = ExportFeatureValuesRequest.newBuilder() .setEntityType( EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) - .setDestination(FeatureValueDestination.newBuilder().setBigqueryDestination( - BigQueryDestination.newBuilder().setOutputUri(destinationTableUri))) - .setFeatureSelector(featureSelector).setFullExport(FullExport.newBuilder()).build(); + .setDestination( + FeatureValueDestination.newBuilder() + .setBigqueryDestination( + BigQueryDestination.newBuilder().setOutputUri(destinationTableUri))) + .setFeatureSelector(featureSelector) + .setFullExport(FullExport.newBuilder()) + .build(); - OperationFuture - exportFeatureValuesFuture = + OperationFuture + exportFeatureValuesFuture = featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest); - System.out.format("Operation name: %s%n", - exportFeatureValuesFuture.getInitialFuture().get().getName()); + System.out.format( + "Operation name: %s%n", exportFeatureValuesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); ExportFeatureValuesResponse exportFeatureValuesResponse = exportFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); @@ -97,4 +117,3 @@ static void exportFeatureValuesSample(String project, String featurestoreId, Str } } // [END aiplatform_export_feature_values_sample] - diff --git a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java index 03a99574b..6d48d34d0 100644 --- a/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java +++ b/samples/snippets/src/main/java/aiplatform/ExportFeatureValuesSnapshotSample.java @@ -15,7 +15,7 @@ * * * Bulk export feature values from a featurestore. See - * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running * the code snippet */ @@ -55,13 +55,26 @@ public static void main(String[] args) String location = "us-central1"; String endpoint = "us-central1-aiplatform.googleapis.com:443"; int timeout = 300; - exportFeatureValuesSnapshotSample(project, featurestoreId, entityTypeId, destinationTableUri, - featureSelectorIds, location, endpoint, timeout); + exportFeatureValuesSnapshotSample( + project, + featurestoreId, + entityTypeId, + destinationTableUri, + featureSelectorIds, + location, + endpoint, + timeout); } - static void exportFeatureValuesSnapshotSample(String project, String featurestoreId, - String entityTypeId, String destinationTableUri, List featureSelectorIds, - String location, String endpoint, int timeout) + static void exportFeatureValuesSnapshotSample( + String project, + String featurestoreId, + String entityTypeId, + String destinationTableUri, + List featureSelectorIds, + String location, + String endpoint, + int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { FeaturestoreServiceSettings featurestoreServiceSettings = FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); @@ -72,23 +85,28 @@ static void exportFeatureValuesSnapshotSample(String project, String featurestor try (FeaturestoreServiceClient featurestoreServiceClient = FeaturestoreServiceClient.create(featurestoreServiceSettings)) { - FeatureSelector featureSelector = FeatureSelector.newBuilder() - .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()).build(); + FeatureSelector featureSelector = + FeatureSelector.newBuilder() + .setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build()) + .build(); ExportFeatureValuesRequest exportFeatureValuesRequest = ExportFeatureValuesRequest.newBuilder() .setEntityType( EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) - .setDestination(FeatureValueDestination.newBuilder().setBigqueryDestination( - BigQueryDestination.newBuilder().setOutputUri(destinationTableUri))) - .setFeatureSelector(featureSelector).setSnapshotExport(SnapshotExport.newBuilder()) + .setDestination( + FeatureValueDestination.newBuilder() + .setBigqueryDestination( + BigQueryDestination.newBuilder().setOutputUri(destinationTableUri))) + .setFeatureSelector(featureSelector) + .setSnapshotExport(SnapshotExport.newBuilder()) .build(); - OperationFuture - exportFeatureValuesFuture = + OperationFuture + exportFeatureValuesFuture = featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest); - System.out.format("Operation name: %s%n", - exportFeatureValuesFuture.getInitialFuture().get().getName()); + System.out.format( + "Operation name: %s%n", exportFeatureValuesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); ExportFeatureValuesResponse exportFeatureValuesResponse = exportFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); @@ -99,4 +117,3 @@ static void exportFeatureValuesSnapshotSample(String project, String featurestor } } // [END aiplatform_export_feature_values_snapshot_sample] - diff --git a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java index 833b56594..405b05f54 100644 --- a/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java +++ b/samples/snippets/src/main/java/aiplatform/ImportFeatureValuesSample.java @@ -15,7 +15,7 @@ * * * Bulk import values into a featurestore for existing features. See - * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running + * https://cloud.google.com/vertex-ai/docs/featurestore/setup before running * the code snippet */ @@ -55,13 +55,30 @@ public static void main(String[] args) String location = "us-central1"; String endpoint = "us-central1-aiplatform.googleapis.com:443"; int timeout = 300; - importFeatureValuesSample(project, featurestoreId, entityTypeId, gcsSourceUri, entityIdField, - featureTimeField, workerCount, location, endpoint, timeout); + importFeatureValuesSample( + project, + featurestoreId, + entityTypeId, + gcsSourceUri, + entityIdField, + featureTimeField, + workerCount, + location, + endpoint, + timeout); } - static void importFeatureValuesSample(String project, String featurestoreId, String entityTypeId, - String gcsSourceUri, String entityIdField, String featureTimeField, int workerCount, - String location, String endpoint, int timeout) + static void importFeatureValuesSample( + String project, + String featurestoreId, + String entityTypeId, + String gcsSourceUri, + String entityIdField, + String featureTimeField, + int workerCount, + String location, + String endpoint, + int timeout) throws IOException, InterruptedException, ExecutionException, TimeoutException { FeaturestoreServiceSettings featurestoreServiceSettings = FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build(); @@ -76,20 +93,23 @@ static void importFeatureValuesSample(String project, String featurestoreId, Str featureSpecs.add(FeatureSpec.newBuilder().setId("title").build()); featureSpecs.add(FeatureSpec.newBuilder().setId("genres").build()); featureSpecs.add(FeatureSpec.newBuilder().setId("average_rating").build()); - ImportFeatureValuesRequest importFeatureValuesRequest = ImportFeatureValuesRequest - .newBuilder() - .setEntityType( - EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) - .setEntityIdField(entityIdField).setFeatureTimeField(featureTimeField) - .addAllFeatureSpecs(featureSpecs).setWorkerCount(workerCount) - .setAvroSource( - AvroSource.newBuilder().setGcsSource(GcsSource.newBuilder().addUris(gcsSourceUri))) - .build(); - OperationFuture - importFeatureValuesFuture = + ImportFeatureValuesRequest importFeatureValuesRequest = + ImportFeatureValuesRequest.newBuilder() + .setEntityType( + EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString()) + .setEntityIdField(entityIdField) + .setFeatureTimeField(featureTimeField) + .addAllFeatureSpecs(featureSpecs) + .setWorkerCount(workerCount) + .setAvroSource( + AvroSource.newBuilder() + .setGcsSource(GcsSource.newBuilder().addUris(gcsSourceUri))) + .build(); + OperationFuture + importFeatureValuesFuture = featurestoreServiceClient.importFeatureValuesAsync(importFeatureValuesRequest); - System.out.format("Operation name: %s%n", - importFeatureValuesFuture.getInitialFuture().get().getName()); + System.out.format( + "Operation name: %s%n", importFeatureValuesFuture.getInitialFuture().get().getName()); System.out.println("Waiting for operation to finish..."); ImportFeatureValuesResponse importFeatureValuesResponse = importFeatureValuesFuture.get(timeout, TimeUnit.SECONDS); @@ -100,4 +120,3 @@ static void importFeatureValuesSample(String project, String featurestoreId, Str } } // [END aiplatform_import_feature_values_sample] - diff --git a/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java b/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java index 53161a41a..56852503b 100644 --- a/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java +++ b/samples/snippets/src/test/java/aiplatform/FeatureValuesSamplesTest.java @@ -102,8 +102,12 @@ static void createBigQueryDataset(String projectId, String datasetName, String l // Initialize client that will be used to send requests. This client only needs // to be created // once, and can be reused for multiple requests. - BigQuery bigquery = BigQueryOptions.newBuilder().setLocation(location).setProjectId(projectId) - .build().getService(); + BigQuery bigquery = + BigQueryOptions.newBuilder() + .setLocation(location) + .setProjectId(projectId) + .build() + .getService(); DatasetInfo datasetInfo = DatasetInfo.newBuilder(datasetName).build(); Dataset newDataset = bigquery.create(datasetInfo); @@ -118,8 +122,12 @@ static void deleteBigQueryDataset(String projectId, String datasetName, String l try { // Initialize client that will be used to send requests. This client only needs to be created // once, and can be reused for multiple requests. - BigQuery bigquery = BigQueryOptions.newBuilder().setLocation(location).setProjectId(projectId) - .build().getService(); + BigQuery bigquery = + BigQueryOptions.newBuilder() + .setLocation(location) + .setProjectId(projectId) + .build() + .getService(); DatasetId datasetId = DatasetId.of(projectId, datasetName); boolean success = bigquery.delete(datasetId, DatasetDeleteOption.deleteContents()); @@ -138,8 +146,8 @@ public void tearDown() throws InterruptedException, ExecutionException, IOException, TimeoutException { // Delete the featurestore - DeleteFeaturestoreSample.deleteFeaturestoreSample(PROJECT_ID, featurestoreId, USE_FORCE, - LOCATION, ENDPOINT, 300); + DeleteFeaturestoreSample.deleteFeaturestoreSample( + PROJECT_ID, featurestoreId, USE_FORCE, LOCATION, ENDPOINT, 300); // Assert String deleteFeaturestoreResponse = bout.toString(); @@ -162,8 +170,8 @@ public void testFeatureValuesSamples() // Create the featurestore String tempUuid = UUID.randomUUID().toString().replaceAll("-", "_").substring(0, 26); String id = String.format("temp_create_featurestore_test_%s", tempUuid); - CreateFeaturestoreSample.createFeaturestoreSample(PROJECT_ID, id, MIN_NODE_COUNT, - MAX_NODE_COUNT, LOCATION, ENDPOINT, 900); + CreateFeaturestoreSample.createFeaturestoreSample( + PROJECT_ID, id, MIN_NODE_COUNT, MAX_NODE_COUNT, LOCATION, ENDPOINT, 900); // Assert String createFeaturestoreResponse = bout.toString(); @@ -174,24 +182,32 @@ public void testFeatureValuesSamples() // Create the entity type String entityTypeId = "movies"; - CreateEntityTypeSample.createEntityTypeSample(PROJECT_ID, featurestoreId, entityTypeId, - DESCRIPTION, LOCATION, ENDPOINT, 900); + CreateEntityTypeSample.createEntityTypeSample( + PROJECT_ID, featurestoreId, entityTypeId, DESCRIPTION, LOCATION, ENDPOINT, 900); // Assert String createEntityTypeResponse = bout.toString(); assertThat(createEntityTypeResponse).contains("Create Entity Type Response"); // Batch create features - BatchCreateFeaturesSample.batchCreateFeaturesSample(PROJECT_ID, featurestoreId, entityTypeId, - LOCATION, ENDPOINT, TIMEOUT); + BatchCreateFeaturesSample.batchCreateFeaturesSample( + PROJECT_ID, featurestoreId, entityTypeId, LOCATION, ENDPOINT, TIMEOUT); // Assert String batchCreateFeaturesResponse = bout.toString(); assertThat(batchCreateFeaturesResponse).contains("Batch Create Features Response"); // Import feature values - ImportFeatureValuesSample.importFeatureValuesSample(PROJECT_ID, featurestoreId, entityTypeId, - GCS_SOURCE_URI, ENTITY_ID_FIELD, FEATURE_TIME_FIELD, WORKER_COUNT, LOCATION, ENDPOINT, + ImportFeatureValuesSample.importFeatureValuesSample( + PROJECT_ID, + featurestoreId, + entityTypeId, + GCS_SOURCE_URI, + ENTITY_ID_FIELD, + FEATURE_TIME_FIELD, + WORKER_COUNT, + LOCATION, + ENDPOINT, TIMEOUT); // Assert @@ -208,8 +224,15 @@ public void testFeatureValuesSamples() assertThat(createBigQueryDatasetResponse).contains(datasetName + " created successfully"); // Export feature values - ExportFeatureValuesSample.exportFeatureValuesSample(PROJECT_ID, featurestoreId, entityTypeId, - destinationTableUri, FEATURE_SELECTOR_IDS, LOCATION, ENDPOINT, TIMEOUT); + ExportFeatureValuesSample.exportFeatureValuesSample( + PROJECT_ID, + featurestoreId, + entityTypeId, + destinationTableUri, + FEATURE_SELECTOR_IDS, + LOCATION, + ENDPOINT, + TIMEOUT); // Assert String exportFeatureValuesResponse = bout.toString(); @@ -219,8 +242,15 @@ public void testFeatureValuesSamples() String.format("bq://%s.%s.%s_snapshot", PROJECT_ID, datasetName, destinationTableName); // Snapshot export feature values - ExportFeatureValuesSnapshotSample.exportFeatureValuesSnapshotSample(PROJECT_ID, featurestoreId, - entityTypeId, destinationTableUri, FEATURE_SELECTOR_IDS, LOCATION, ENDPOINT, TIMEOUT); + ExportFeatureValuesSnapshotSample.exportFeatureValuesSnapshotSample( + PROJECT_ID, + featurestoreId, + entityTypeId, + destinationTableUri, + FEATURE_SELECTOR_IDS, + LOCATION, + ENDPOINT, + TIMEOUT); // Assert String snapshotResponse = bout.toString(); @@ -230,14 +260,19 @@ public void testFeatureValuesSamples() String.format("bq://%s.%s.%s_batchRead", PROJECT_ID, datasetName, destinationTableName); // Batch read feature values - BatchReadFeatureValuesSample.batchReadFeatureValuesSample(PROJECT_ID, featurestoreId, - entityTypeId, INPUT_CSV_FILE, destinationTableUri, FEATURE_SELECTOR_IDS, LOCATION, ENDPOINT, + BatchReadFeatureValuesSample.batchReadFeatureValuesSample( + PROJECT_ID, + featurestoreId, + entityTypeId, + INPUT_CSV_FILE, + destinationTableUri, + FEATURE_SELECTOR_IDS, + LOCATION, + ENDPOINT, TIMEOUT); // Assert String batchReadFeatureValuesResponse = bout.toString(); assertThat(batchReadFeatureValuesResponse).contains("Batch Read Feature Values Response"); - } } -