This repository has been archived by the owner on Sep 9, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 24
feat(samples): add all feature samples #980
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
c04b425
feat(samples): add all feature samples
sai-chaithu 361aed3
feat(samples): update all feature samples
sai-chaithu 6263f3d
Merge branch 'googleapis:main' into FeatureSamples
sai-chaithu 851fc59
Merge branch 'googleapis:main' into FeatureSamples
sai-chaithu c70d153
feat(samples): updated the feature samples with close method call and…
sai-chaithu 471b4c6
🦉 Updates from OwlBot post-processor
gcf-owl-bot[bot] 6e14839
Merge branch 'googleapis:main' into FeatureSamples
sai-chaithu 96c42a9
feat(samples): update test with fixed featurestoreId
sai-chaithu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
108 changes: 108 additions & 0 deletions
108
samples/snippets/src/main/java/aiplatform/CreateFeatureSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
90
samples/snippets/src/main/java/aiplatform/DeleteFeatureSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
79
samples/snippets/src/main/java/aiplatform/GetFeatureSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
83 changes: 83 additions & 0 deletions
83
samples/snippets/src/main/java/aiplatform/ListFeaturesAsyncSample.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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 ?
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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