Skip to content
This repository has been archived by the owner on Sep 9, 2023. It is now read-only.

Commit

Permalink
feat(samples): add all feature samples (#980)
Browse files Browse the repository at this point in the history
* feat(samples): add all feature samples

* feat(samples): update all feature samples

* feat(samples): updated the feature samples with close method call and separate timeouts

* 🦉 Updates from OwlBot post-processor

See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md

* feat(samples): update test with fixed featurestoreId

Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
  • Loading branch information
sai-chaithu and gcf-owl-bot[bot] authored Aug 3, 2022
1 parent 6fb1534 commit a932cf8
Show file tree
Hide file tree
Showing 10 changed files with 825 additions and 0 deletions.
8 changes: 8 additions & 0 deletions README.md

Large diffs are not rendered by default.

108 changes: 108 additions & 0 deletions samples/snippets/src/main/java/aiplatform/CreateFeatureSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
/*
* 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 a single feature for an existing entity type. See
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
* the code snippet
*/

package aiplatform;

// [START aiplatform_create_feature_sample]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1.CreateFeatureOperationMetadata;
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.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class CreateFeatureSample {

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 featureId = "YOUR_FEATURE_ID";
String description = "YOUR_FEATURE_DESCRIPTION";
ValueType valueType = ValueType.STRING;
String location = "us-central1";
String endpoint = "us-central1-aiplatform.googleapis.com:443";
int timeout = 900;
createFeatureSample(
project,
featurestoreId,
entityTypeId,
featureId,
description,
valueType,
location,
endpoint,
timeout);
}

static void createFeatureSample(
String project,
String featurestoreId,
String entityTypeId,
String featureId,
String description,
ValueType valueType,
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)) {

Feature feature =
Feature.newBuilder().setDescription(description).setValueType(valueType).build();

CreateFeatureRequest createFeatureRequest =
CreateFeatureRequest.newBuilder()
.setParent(
EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString())
.setFeature(feature)
.setFeatureId(featureId)
.build();

OperationFuture<Feature, CreateFeatureOperationMetadata> featureFuture =
featurestoreServiceClient.createFeatureAsync(createFeatureRequest);
System.out.format("Operation name: %s%n", featureFuture.getInitialFuture().get().getName());
System.out.println("Waiting for operation to finish...");
Feature featureResponse = featureFuture.get(timeout, TimeUnit.SECONDS);
System.out.println("Create Feature Response");
System.out.format("Name: %s%n", featureResponse.getName());
featurestoreServiceClient.close();
}
}
}
// [END aiplatform_create_feature_sample]
90 changes: 90 additions & 0 deletions samples/snippets/src/main/java/aiplatform/DeleteFeatureSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
/*
* 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.
*
*
* Delete a single feature from an existing entity type. See
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
* the code snippet
*/

package aiplatform;

// [START aiplatform_delete_feature_sample]

import com.google.api.gax.longrunning.OperationFuture;
import com.google.cloud.aiplatform.v1.DeleteFeatureRequest;
import com.google.cloud.aiplatform.v1.DeleteOperationMetadata;
import com.google.cloud.aiplatform.v1.FeatureName;
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
import com.google.protobuf.Empty;
import java.io.IOException;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class DeleteFeatureSample {

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 featureId = "YOUR_FEATURE_ID";
String location = "us-central1";
String endpoint = "us-central1-aiplatform.googleapis.com:443";
int timeout = 300;

deleteFeatureSample(
project, featurestoreId, entityTypeId, featureId, location, endpoint, timeout);
}

static void deleteFeatureSample(
String project,
String featurestoreId,
String entityTypeId,
String featureId,
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)) {

DeleteFeatureRequest deleteFeatureRequest =
DeleteFeatureRequest.newBuilder()
.setName(
FeatureName.of(project, location, featurestoreId, entityTypeId, featureId)
.toString())
.build();

OperationFuture<Empty, DeleteOperationMetadata> operationFuture =
featurestoreServiceClient.deleteFeatureAsync(deleteFeatureRequest);
System.out.format("Operation name: %s%n", operationFuture.getInitialFuture().get().getName());
System.out.println("Waiting for operation to finish...");
operationFuture.get(timeout, TimeUnit.SECONDS);
System.out.format("Deleted Feature.");
featurestoreServiceClient.close();
}
}
}
// [END aiplatform_delete_feature_sample]
79 changes: 79 additions & 0 deletions samples/snippets/src/main/java/aiplatform/GetFeatureSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* 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.
*
*
* Get feature details. See
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
* the code snippet
*/

package aiplatform;

// [START aiplatform_get_feature_sample]

import com.google.cloud.aiplatform.v1.Feature;
import com.google.cloud.aiplatform.v1.FeatureName;
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
import com.google.cloud.aiplatform.v1.GetFeatureRequest;
import java.io.IOException;

public class GetFeatureSample {

public static void main(String[] args) throws IOException {
// 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 featureId = "YOUR_FEATURE_ID";
String location = "us-central1";
String endpoint = "us-central1-aiplatform.googleapis.com:443";

getFeatureSample(project, featurestoreId, entityTypeId, featureId, location, endpoint);
}

static void getFeatureSample(
String project,
String featurestoreId,
String entityTypeId,
String featureId,
String location,
String endpoint)
throws IOException {

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)) {

GetFeatureRequest getFeatureRequest =
GetFeatureRequest.newBuilder()
.setName(
FeatureName.of(project, location, featurestoreId, entityTypeId, featureId)
.toString())
.build();

Feature feature = featurestoreServiceClient.getFeature(getFeatureRequest);
System.out.println("Get Feature Response");
System.out.println(feature);
featurestoreServiceClient.close();
}
}
}
// [END aiplatform_get_feature_sample]
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.
*
*
* List available feature details. See
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
* the code snippet
*/

package aiplatform;

// [START aiplatform_list_features_async_sample]

import com.google.cloud.aiplatform.v1.EntityTypeName;
import com.google.cloud.aiplatform.v1.Feature;
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
import com.google.cloud.aiplatform.v1.ListFeaturesRequest;
import com.google.cloud.aiplatform.v1.ListFeaturesResponse;
import com.google.common.base.Strings;
import java.io.IOException;

public class ListFeaturesAsyncSample {

public static void main(String[] args) throws IOException {
// 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";

listFeaturesAsyncSample(project, featurestoreId, entityTypeId, location, endpoint);
}

static void listFeaturesAsyncSample(
String project, String featurestoreId, String entityTypeId, String location, String endpoint)
throws IOException {
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)) {

ListFeaturesRequest listFeaturesRequest =
ListFeaturesRequest.newBuilder()
.setParent(
EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString())
.build();
System.out.println("List Features Async Response");
while (true) {
ListFeaturesResponse listFeaturesResponse =
featurestoreServiceClient.listFeaturesCallable().call(listFeaturesRequest);
for (Feature element : listFeaturesResponse.getFeaturesList()) {
System.out.println(element);
}
String nextPageToken = listFeaturesResponse.getNextPageToken();
if (!Strings.isNullOrEmpty(nextPageToken)) {
listFeaturesRequest = listFeaturesRequest.toBuilder().setPageToken(nextPageToken).build();
} else {
break;
}
}
featurestoreServiceClient.close();
}
}
}
// [END aiplatform_list_features_async_sample]
Loading

0 comments on commit a932cf8

Please sign in to comment.