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

Commit a932cf8

Browse files
feat(samples): add all feature samples (#980)
* 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>
1 parent 6fb1534 commit a932cf8

10 files changed

+825
-0
lines changed

README.md

+8
Large diffs are not rendered by default.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*
17+
* Create a single feature for an existing entity type. See
18+
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
19+
* the code snippet
20+
*/
21+
22+
package aiplatform;
23+
24+
// [START aiplatform_create_feature_sample]
25+
26+
import com.google.api.gax.longrunning.OperationFuture;
27+
import com.google.cloud.aiplatform.v1.CreateFeatureOperationMetadata;
28+
import com.google.cloud.aiplatform.v1.CreateFeatureRequest;
29+
import com.google.cloud.aiplatform.v1.EntityTypeName;
30+
import com.google.cloud.aiplatform.v1.Feature;
31+
import com.google.cloud.aiplatform.v1.Feature.ValueType;
32+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
33+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
34+
import java.io.IOException;
35+
import java.util.concurrent.ExecutionException;
36+
import java.util.concurrent.TimeUnit;
37+
import java.util.concurrent.TimeoutException;
38+
39+
public class CreateFeatureSample {
40+
41+
public static void main(String[] args)
42+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
43+
// TODO(developer): Replace these variables before running the sample.
44+
String project = "YOUR_PROJECT_ID";
45+
String featurestoreId = "YOUR_FEATURESTORE_ID";
46+
String entityTypeId = "YOUR_ENTITY_TYPE_ID";
47+
String featureId = "YOUR_FEATURE_ID";
48+
String description = "YOUR_FEATURE_DESCRIPTION";
49+
ValueType valueType = ValueType.STRING;
50+
String location = "us-central1";
51+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
52+
int timeout = 900;
53+
createFeatureSample(
54+
project,
55+
featurestoreId,
56+
entityTypeId,
57+
featureId,
58+
description,
59+
valueType,
60+
location,
61+
endpoint,
62+
timeout);
63+
}
64+
65+
static void createFeatureSample(
66+
String project,
67+
String featurestoreId,
68+
String entityTypeId,
69+
String featureId,
70+
String description,
71+
ValueType valueType,
72+
String location,
73+
String endpoint,
74+
int timeout)
75+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
76+
77+
FeaturestoreServiceSettings featurestoreServiceSettings =
78+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
79+
80+
// Initialize client that will be used to send requests. This client only needs to be created
81+
// once, and can be reused for multiple requests. After completing all of your requests, call
82+
// the "close" method on the client to safely clean up any remaining background resources.
83+
try (FeaturestoreServiceClient featurestoreServiceClient =
84+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
85+
86+
Feature feature =
87+
Feature.newBuilder().setDescription(description).setValueType(valueType).build();
88+
89+
CreateFeatureRequest createFeatureRequest =
90+
CreateFeatureRequest.newBuilder()
91+
.setParent(
92+
EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString())
93+
.setFeature(feature)
94+
.setFeatureId(featureId)
95+
.build();
96+
97+
OperationFuture<Feature, CreateFeatureOperationMetadata> featureFuture =
98+
featurestoreServiceClient.createFeatureAsync(createFeatureRequest);
99+
System.out.format("Operation name: %s%n", featureFuture.getInitialFuture().get().getName());
100+
System.out.println("Waiting for operation to finish...");
101+
Feature featureResponse = featureFuture.get(timeout, TimeUnit.SECONDS);
102+
System.out.println("Create Feature Response");
103+
System.out.format("Name: %s%n", featureResponse.getName());
104+
featurestoreServiceClient.close();
105+
}
106+
}
107+
}
108+
// [END aiplatform_create_feature_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*
17+
* Delete a single feature from an existing entity type. See
18+
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
19+
* the code snippet
20+
*/
21+
22+
package aiplatform;
23+
24+
// [START aiplatform_delete_feature_sample]
25+
26+
import com.google.api.gax.longrunning.OperationFuture;
27+
import com.google.cloud.aiplatform.v1.DeleteFeatureRequest;
28+
import com.google.cloud.aiplatform.v1.DeleteOperationMetadata;
29+
import com.google.cloud.aiplatform.v1.FeatureName;
30+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
31+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
32+
import com.google.protobuf.Empty;
33+
import java.io.IOException;
34+
import java.util.concurrent.ExecutionException;
35+
import java.util.concurrent.TimeUnit;
36+
import java.util.concurrent.TimeoutException;
37+
38+
public class DeleteFeatureSample {
39+
40+
public static void main(String[] args)
41+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
42+
// TODO(developer): Replace these variables before running the sample.
43+
String project = "YOUR_PROJECT_ID";
44+
String featurestoreId = "YOUR_FEATURESTORE_ID";
45+
String entityTypeId = "YOUR_ENTITY_TYPE_ID";
46+
String featureId = "YOUR_FEATURE_ID";
47+
String location = "us-central1";
48+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
49+
int timeout = 300;
50+
51+
deleteFeatureSample(
52+
project, featurestoreId, entityTypeId, featureId, location, endpoint, timeout);
53+
}
54+
55+
static void deleteFeatureSample(
56+
String project,
57+
String featurestoreId,
58+
String entityTypeId,
59+
String featureId,
60+
String location,
61+
String endpoint,
62+
int timeout)
63+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
64+
FeaturestoreServiceSettings featurestoreServiceSettings =
65+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
66+
67+
// Initialize client that will be used to send requests. This client only needs to be created
68+
// once, and can be reused for multiple requests. After completing all of your requests, call
69+
// the "close" method on the client to safely clean up any remaining background resources.
70+
try (FeaturestoreServiceClient featurestoreServiceClient =
71+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
72+
73+
DeleteFeatureRequest deleteFeatureRequest =
74+
DeleteFeatureRequest.newBuilder()
75+
.setName(
76+
FeatureName.of(project, location, featurestoreId, entityTypeId, featureId)
77+
.toString())
78+
.build();
79+
80+
OperationFuture<Empty, DeleteOperationMetadata> operationFuture =
81+
featurestoreServiceClient.deleteFeatureAsync(deleteFeatureRequest);
82+
System.out.format("Operation name: %s%n", operationFuture.getInitialFuture().get().getName());
83+
System.out.println("Waiting for operation to finish...");
84+
operationFuture.get(timeout, TimeUnit.SECONDS);
85+
System.out.format("Deleted Feature.");
86+
featurestoreServiceClient.close();
87+
}
88+
}
89+
}
90+
// [END aiplatform_delete_feature_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*
17+
* Get feature details. See
18+
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
19+
* the code snippet
20+
*/
21+
22+
package aiplatform;
23+
24+
// [START aiplatform_get_feature_sample]
25+
26+
import com.google.cloud.aiplatform.v1.Feature;
27+
import com.google.cloud.aiplatform.v1.FeatureName;
28+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
29+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
30+
import com.google.cloud.aiplatform.v1.GetFeatureRequest;
31+
import java.io.IOException;
32+
33+
public class GetFeatureSample {
34+
35+
public static void main(String[] args) throws IOException {
36+
// TODO(developer): Replace these variables before running the sample.
37+
String project = "YOUR_PROJECT_ID";
38+
String featurestoreId = "YOUR_FEATURESTORE_ID";
39+
String entityTypeId = "YOUR_ENTITY_TYPE_ID";
40+
String featureId = "YOUR_FEATURE_ID";
41+
String location = "us-central1";
42+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
43+
44+
getFeatureSample(project, featurestoreId, entityTypeId, featureId, location, endpoint);
45+
}
46+
47+
static void getFeatureSample(
48+
String project,
49+
String featurestoreId,
50+
String entityTypeId,
51+
String featureId,
52+
String location,
53+
String endpoint)
54+
throws IOException {
55+
56+
FeaturestoreServiceSettings featurestoreServiceSettings =
57+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
58+
59+
// Initialize client that will be used to send requests. This client only needs to be created
60+
// once, and can be reused for multiple requests. After completing all of your requests, call
61+
// the "close" method on the client to safely clean up any remaining background resources.
62+
try (FeaturestoreServiceClient featurestoreServiceClient =
63+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
64+
65+
GetFeatureRequest getFeatureRequest =
66+
GetFeatureRequest.newBuilder()
67+
.setName(
68+
FeatureName.of(project, location, featurestoreId, entityTypeId, featureId)
69+
.toString())
70+
.build();
71+
72+
Feature feature = featurestoreServiceClient.getFeature(getFeatureRequest);
73+
System.out.println("Get Feature Response");
74+
System.out.println(feature);
75+
featurestoreServiceClient.close();
76+
}
77+
}
78+
}
79+
// [END aiplatform_get_feature_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
*
17+
* List available feature details. See
18+
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
19+
* the code snippet
20+
*/
21+
22+
package aiplatform;
23+
24+
// [START aiplatform_list_features_async_sample]
25+
26+
import com.google.cloud.aiplatform.v1.EntityTypeName;
27+
import com.google.cloud.aiplatform.v1.Feature;
28+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
29+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
30+
import com.google.cloud.aiplatform.v1.ListFeaturesRequest;
31+
import com.google.cloud.aiplatform.v1.ListFeaturesResponse;
32+
import com.google.common.base.Strings;
33+
import java.io.IOException;
34+
35+
public class ListFeaturesAsyncSample {
36+
37+
public static void main(String[] args) throws IOException {
38+
// TODO(developer): Replace these variables before running the sample.
39+
String project = "YOUR_PROJECT_ID";
40+
String featurestoreId = "YOUR_FEATURESTORE_ID";
41+
String entityTypeId = "YOUR_ENTITY_TYPE_ID";
42+
String location = "us-central1";
43+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
44+
45+
listFeaturesAsyncSample(project, featurestoreId, entityTypeId, location, endpoint);
46+
}
47+
48+
static void listFeaturesAsyncSample(
49+
String project, String featurestoreId, String entityTypeId, String location, String endpoint)
50+
throws IOException {
51+
FeaturestoreServiceSettings featurestoreServiceSettings =
52+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
53+
54+
// Initialize client that will be used to send requests. This client only needs to be created
55+
// once, and can be reused for multiple requests. After completing all of your requests, call
56+
// the "close" method on the client to safely clean up any remaining background resources.
57+
try (FeaturestoreServiceClient featurestoreServiceClient =
58+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
59+
60+
ListFeaturesRequest listFeaturesRequest =
61+
ListFeaturesRequest.newBuilder()
62+
.setParent(
63+
EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString())
64+
.build();
65+
System.out.println("List Features Async Response");
66+
while (true) {
67+
ListFeaturesResponse listFeaturesResponse =
68+
featurestoreServiceClient.listFeaturesCallable().call(listFeaturesRequest);
69+
for (Feature element : listFeaturesResponse.getFeaturesList()) {
70+
System.out.println(element);
71+
}
72+
String nextPageToken = listFeaturesResponse.getNextPageToken();
73+
if (!Strings.isNullOrEmpty(nextPageToken)) {
74+
listFeaturesRequest = listFeaturesRequest.toBuilder().setPageToken(nextPageToken).build();
75+
} else {
76+
break;
77+
}
78+
}
79+
featurestoreServiceClient.close();
80+
}
81+
}
82+
}
83+
// [END aiplatform_list_features_async_sample]

0 commit comments

Comments
 (0)