Skip to content

Commit ffc4b87

Browse files
feat(samples): add create-featurestore sample (googleapis#948)
* CreateFeaturestore Sample API * DeleteFeaturestore Sample API * Create, Update, Get featurestore API's * Revert Code * CreateFeaturestoreSampleTest update * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com>
1 parent 107973c commit ffc4b87

File tree

4 files changed

+331
-0
lines changed

4 files changed

+331
-0
lines changed

java-aiplatform/README.md

+2
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-aiplatform/tr
118118
| Create Dataset Text Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CreateDatasetTextSample.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/CreateDatasetTextSample.java) |
119119
| Create Dataset Video Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CreateDatasetVideoSample.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/CreateDatasetVideoSample.java) |
120120
| Create Endpoint Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CreateEndpointSample.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/CreateEndpointSample.java) |
121+
| Create Featurestore Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CreateFeaturestoreSample.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/CreateFeaturestoreSample.java) |
121122
| Create Hyperparameter Tuning Job Python Package Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CreateHyperparameterTuningJobPythonPackageSample.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/CreateHyperparameterTuningJobPythonPackageSample.java) |
122123
| Create Hyperparameter Tuning Job Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CreateHyperparameterTuningJobSample.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/CreateHyperparameterTuningJobSample.java) |
123124
| Create Training Pipeline Custom Job Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/CreateTrainingPipelineCustomJobSample.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/CreateTrainingPipelineCustomJobSample.java) |
@@ -138,6 +139,7 @@ Samples are in the [`samples/`](https://github.com/googleapis/java-aiplatform/tr
138139
| Delete Dataset Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/DeleteDatasetSample.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/DeleteDatasetSample.java) |
139140
| Delete Endpoint Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/DeleteEndpointSample.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/DeleteEndpointSample.java) |
140141
| Delete Export Model Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/DeleteExportModelSample.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/DeleteExportModelSample.java) |
142+
| Delete Featurestore Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/DeleteFeaturestoreSample.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/DeleteFeaturestoreSample.java) |
141143
| Delete Model Sample | [source code](https://github.com/googleapis/java-aiplatform/blob/main/samples/snippets/src/main/java/aiplatform/DeleteModelSample.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/DeleteModelSample.java) |
142144
| 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) |
143145
| 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) |
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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 featurestore resource to contain entity types and features. 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_featurestore_sample]
25+
26+
import com.google.api.gax.longrunning.OperationFuture;
27+
import com.google.cloud.aiplatform.v1beta1.CreateFeaturestoreOperationMetadata;
28+
import com.google.cloud.aiplatform.v1beta1.CreateFeaturestoreRequest;
29+
import com.google.cloud.aiplatform.v1beta1.Featurestore;
30+
import com.google.cloud.aiplatform.v1beta1.Featurestore.OnlineServingConfig;
31+
import com.google.cloud.aiplatform.v1beta1.Featurestore.OnlineServingConfig.Scaling;
32+
import com.google.cloud.aiplatform.v1beta1.FeaturestoreServiceClient;
33+
import com.google.cloud.aiplatform.v1beta1.FeaturestoreServiceSettings;
34+
import com.google.cloud.aiplatform.v1beta1.LocationName;
35+
import java.io.IOException;
36+
import java.util.concurrent.ExecutionException;
37+
import java.util.concurrent.TimeUnit;
38+
import java.util.concurrent.TimeoutException;
39+
40+
public class CreateFeaturestoreSample {
41+
42+
public static void main(String[] args)
43+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
44+
// TODO(developer): Replace these variables before running the sample.
45+
String project = "YOUR_PROJECT_ID";
46+
String featurestoreId = "YOUR_FEATURESTORE_ID";
47+
int minNodeCount = 1;
48+
int maxNodeCount = 5;
49+
String location = "us-central1";
50+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
51+
int timeout = 900;
52+
createFeaturestoreSample(
53+
project, featurestoreId, minNodeCount, maxNodeCount, location, endpoint, timeout);
54+
}
55+
56+
static void createFeaturestoreSample(
57+
String project,
58+
String featurestoreId,
59+
int minNodeCount,
60+
int maxNodeCount,
61+
String location,
62+
String endpoint,
63+
int timeout)
64+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
65+
66+
FeaturestoreServiceSettings featurestoreServiceSettings =
67+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
68+
69+
// Initialize client that will be used to send requests. This client only needs to be created
70+
// once, and can be reused for multiple requests. After completing all of your requests, call
71+
// the "close" method on the client to safely clean up any remaining background resources.
72+
try (FeaturestoreServiceClient featurestoreServiceClient =
73+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
74+
75+
OnlineServingConfig.Builder builderValue =
76+
OnlineServingConfig.newBuilder()
77+
.setScaling(
78+
Scaling.newBuilder().setMinNodeCount(minNodeCount).setMaxNodeCount(maxNodeCount));
79+
Featurestore featurestore =
80+
Featurestore.newBuilder().setOnlineServingConfig(builderValue).build();
81+
String parent = LocationName.of(project, location).toString();
82+
83+
CreateFeaturestoreRequest createFeaturestoreRequest =
84+
CreateFeaturestoreRequest.newBuilder()
85+
.setParent(parent)
86+
.setFeaturestore(featurestore)
87+
.setFeaturestoreId(featurestoreId)
88+
.build();
89+
90+
OperationFuture<Featurestore, CreateFeaturestoreOperationMetadata> featurestoreFuture =
91+
featurestoreServiceClient.createFeaturestoreAsync(createFeaturestoreRequest);
92+
System.out.format(
93+
"Operation name: %s%n", featurestoreFuture.getInitialFuture().get().getName());
94+
System.out.println("Waiting for operation to finish...");
95+
Featurestore featurestoreResponse = featurestoreFuture.get(timeout, TimeUnit.SECONDS);
96+
System.out.println("Create Featurestore Response");
97+
System.out.format("Name: %s%n", featurestoreResponse.getName());
98+
}
99+
}
100+
}
101+
// [END aiplatform_create_featurestore_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
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 featurestore. 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_featurestore_sample]
25+
26+
import com.google.api.gax.longrunning.OperationFuture;
27+
import com.google.cloud.aiplatform.v1.DeleteFeaturestoreRequest;
28+
import com.google.cloud.aiplatform.v1.DeleteOperationMetadata;
29+
import com.google.cloud.aiplatform.v1.FeaturestoreName;
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 DeleteFeaturestoreSample {
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+
boolean useForce = true;
46+
String location = "us-central1";
47+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
48+
int timeout = 60;
49+
deleteFeaturestoreSample(project, featurestoreId, useForce, location, endpoint, timeout);
50+
}
51+
52+
static void deleteFeaturestoreSample(
53+
String project,
54+
String featurestoreId,
55+
boolean useForce,
56+
String location,
57+
String endpoint,
58+
int timeout)
59+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
60+
61+
FeaturestoreServiceSettings featurestoreServiceSettings =
62+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
63+
64+
// Initialize client that will be used to send requests. This client only needs to be created
65+
// once, and can be reused for multiple requests. After completing all of your requests, call
66+
// the "close" method on the client to safely clean up any remaining background resources.
67+
try (FeaturestoreServiceClient featurestoreServiceClient =
68+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
69+
70+
DeleteFeaturestoreRequest deleteFeaturestoreRequest =
71+
DeleteFeaturestoreRequest.newBuilder()
72+
.setName(FeaturestoreName.of(project, location, featurestoreId).toString())
73+
.setForce(useForce)
74+
.build();
75+
76+
OperationFuture<Empty, DeleteOperationMetadata> operationFuture =
77+
featurestoreServiceClient.deleteFeaturestoreAsync(deleteFeaturestoreRequest);
78+
System.out.format("Operation name: %s%n", operationFuture.getInitialFuture().get().getName());
79+
System.out.println("Waiting for operation to finish...");
80+
operationFuture.get(timeout, TimeUnit.SECONDS);
81+
82+
System.out.format("Deleted Featurestore.");
83+
}
84+
}
85+
}
86+
// [END aiplatform_delete_featurestore_sample]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
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+
package aiplatform;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static junit.framework.TestCase.assertNotNull;
21+
22+
import com.google.api.gax.longrunning.OperationFuture;
23+
import com.google.cloud.aiplatform.v1.DeleteFeaturestoreRequest;
24+
import com.google.cloud.aiplatform.v1.DeleteOperationMetadata;
25+
import com.google.cloud.aiplatform.v1.FeaturestoreName;
26+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
27+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
28+
import com.google.protobuf.Empty;
29+
import java.io.ByteArrayOutputStream;
30+
import java.io.IOException;
31+
import java.io.PrintStream;
32+
import java.util.UUID;
33+
import java.util.concurrent.ExecutionException;
34+
import java.util.concurrent.TimeUnit;
35+
import java.util.concurrent.TimeoutException;
36+
import org.junit.After;
37+
import org.junit.Before;
38+
import org.junit.BeforeClass;
39+
import org.junit.Test;
40+
import org.junit.runner.RunWith;
41+
import org.junit.runners.JUnit4;
42+
43+
@RunWith(JUnit4.class)
44+
public class CreateFeaturestoreSampleTest {
45+
46+
private static final String PROJECT_ID = System.getenv("UCAIP_PROJECT_ID");
47+
private static final int MIN_NODE_COUNT = 1;
48+
private static final int MAX_NODE_COUNT = 5;
49+
private static final boolean USE_FORCE = true;
50+
private static final String LOCATION = "us-central1";
51+
private static final String ENDPOINT = "us-central1-aiplatform.googleapis.com:443";
52+
private static final int TIMEOUT = 900;
53+
private ByteArrayOutputStream bout;
54+
private PrintStream out;
55+
private PrintStream originalPrintStream;
56+
private String featurestoreId;
57+
58+
private static void requireEnvVar(String varName) {
59+
String errorMessage =
60+
String.format("Environment variable '%s' is required to perform these tests.", varName);
61+
assertNotNull(errorMessage, System.getenv(varName));
62+
}
63+
64+
@BeforeClass
65+
public static void checkRequirements() {
66+
requireEnvVar("GOOGLE_APPLICATION_CREDENTIALS");
67+
requireEnvVar("UCAIP_PROJECT_ID");
68+
}
69+
70+
@Before
71+
public void setUp() {
72+
bout = new ByteArrayOutputStream();
73+
out = new PrintStream(bout);
74+
originalPrintStream = System.out;
75+
System.setOut(out);
76+
}
77+
78+
static void deleteFeaturestoreSample(
79+
String project,
80+
String featurestoreId,
81+
boolean useForce,
82+
String location,
83+
String endpoint,
84+
int timeout)
85+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
86+
87+
FeaturestoreServiceSettings featurestoreServiceSettings =
88+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
89+
90+
// Initialize client that will be used to send requests. This client only needs to be created
91+
// once, and can be reused for multiple requests. After completing all of your requests, call
92+
// the "close" method on the client to safely clean up any remaining background resources.
93+
try (FeaturestoreServiceClient featurestoreServiceClient =
94+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
95+
96+
DeleteFeaturestoreRequest deleteFeaturestoreRequest =
97+
DeleteFeaturestoreRequest.newBuilder()
98+
.setName(FeaturestoreName.of(project, location, featurestoreId).toString())
99+
.setForce(useForce)
100+
.build();
101+
102+
OperationFuture<Empty, DeleteOperationMetadata> operationFuture =
103+
featurestoreServiceClient.deleteFeaturestoreAsync(deleteFeaturestoreRequest);
104+
System.out.format("Operation name: %s%n", operationFuture.getInitialFuture().get().getName());
105+
System.out.println("Waiting for operation to finish...");
106+
operationFuture.get(timeout, TimeUnit.SECONDS);
107+
108+
System.out.format("Deleted Featurestore.");
109+
}
110+
}
111+
112+
@After
113+
public void tearDown()
114+
throws InterruptedException, ExecutionException, IOException, TimeoutException {
115+
116+
// Delete the featurestore
117+
deleteFeaturestoreSample(PROJECT_ID, featurestoreId, USE_FORCE, LOCATION, ENDPOINT, 60);
118+
119+
// Assert
120+
String deleteFeaturestoreResponse = bout.toString();
121+
assertThat(deleteFeaturestoreResponse).contains("Deleted Featurestore");
122+
System.out.flush();
123+
System.setOut(originalPrintStream);
124+
}
125+
126+
@Test
127+
public void testCreateFeaturestoreSample()
128+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
129+
// Create the featurestore
130+
String tempUuid = UUID.randomUUID().toString().replaceAll("-", "_").substring(0, 26);
131+
String id = String.format("temp_create_featurestore_test_%s", tempUuid);
132+
CreateFeaturestoreSample.createFeaturestoreSample(
133+
PROJECT_ID, id, MIN_NODE_COUNT, MAX_NODE_COUNT, LOCATION, ENDPOINT, TIMEOUT);
134+
135+
// Assert
136+
String createFeaturestoreResponse = bout.toString();
137+
assertThat(createFeaturestoreResponse).contains("Create Featurestore Response");
138+
featurestoreId =
139+
createFeaturestoreResponse.split("Name: ")[1].split("featurestores/")[1].split("\n")[0]
140+
.trim();
141+
}
142+
}

0 commit comments

Comments
 (0)