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

feat(samples): add all feature samples #980

Merged
merged 8 commits into from
Aug 3, 2022
90 changes: 90 additions & 0 deletions samples/snippets/src/main/java/aiplatform/CreateFeatureSample.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.
*
*
* 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";
String valueType = "YOUR_FEATURE_VALUE_TYPE";
String location = "us-central1";
String endpoint = "us-central1-aiplatform.googleapis.com:443";
int timeout = 300;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we follow the same timeout strategy as suggested in googleapis/nodejs-ai-platform#338

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was suggested for Featurestore API's, should follow the same timeouts as below for EntityType and Feature API's as well ?
15min for create
5min for delete
10min for update
5sec for list,get

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets do this instead, as Update on Feature/EntityType is syncronous, no long running operation i.e.

15min for create
5min for delete
1min for update
5sec for list,get

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

updated for create and delete feature API's with timeouts 15min and 5min respectively and for update, list, get timeout is not required.

createFeatureSample(project, featurestoreId, entityTypeId, featureId, description, valueType,
location, endpoint, timeout);
}

static void createFeatureSample(String project, String featurestoreId, String entityTypeId,
String featureId, String description, String 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.valueOf(valueType))
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can we use the ENUM directly. Applies to all occurrences.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, will pass the ENUM variable instead of String Variable as below

Suggested change
.setValueType(ValueType.valueOf(valueType))
ValueType valueType = ValueType.STRING;
...
.setValueType(valueType)

// .setDisableMonitoring(disableMonitoring)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can remove this unless you guys are asked to put this example explicitly.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will remove it.

.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());
}
}
}
// [END aiplatform_create_feature_sample]

83 changes: 83 additions & 0 deletions samples/snippets/src/main/java/aiplatform/DeleteFeatureSample.java
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.
*
*
* 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.");
}
}
}
// [END aiplatform_delete_Feature_sample]

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

aiplatform_delete_Feature_sample -> aiplatform_delete_feature_sample
both START and END

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will update it


71 changes: 71 additions & 0 deletions samples/snippets/src/main/java/aiplatform/GetFeatureSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* 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 =
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add the close to examples as well ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In other samples of GitHub close is not used, so we were following the same approach.
Should we add it for all the feature samples?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah I see it as a plus to have it be there.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added for all feature API's

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);
}
}
}
// [END aiplatform_get_feature_sample]

Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* 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;
}
}
}
}
}
// [END aiplatform_list_features_async_sample]

69 changes: 69 additions & 0 deletions samples/snippets/src/main/java/aiplatform/ListFeaturesSample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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_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 java.io.IOException;

public class ListFeaturesSample {

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";

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

static void listFeaturesSample(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 Response");
for (Feature element : featurestoreServiceClient.listFeatures(listFeaturesRequest)
.iterateAll()) {
System.out.println(element);
}
}
}
}
// [END aiplatform_list_features_sample]

Loading