Skip to content

Commit 09ae767

Browse files
feat(samples): add all feature values samples (#981)
* feat(samples): add all feature values samples * feat(samples): update all feature values samples * feat(samples): update big query dependency in pom.xml * feat(samples): code review changes * feat(samples): updated feature values samples with close method call and list variable * 🦉 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 df8148a commit 09ae767

File tree

7 files changed

+906
-1
lines changed

7 files changed

+906
-1
lines changed

aiplatform/snippets/pom.xml

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@
6262
<artifactId>proto-google-cloud-aiplatform-v1beta1</artifactId>
6363
<version>0.17.0</version>
6464
</dependency>
65-
65+
<dependency>
66+
<groupId>com.google.cloud</groupId>
67+
<artifactId>google-cloud-bigquery</artifactId>
68+
<version>2.13.6</version>
69+
</dependency>
6670
</dependencies>
6771
</project>
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
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 features in bulk for an existing entity type. See
18+
* https://cloud.google.com/vertex-ai/docs/featurestore/setup
19+
* before running the code snippet
20+
*/
21+
22+
package aiplatform;
23+
24+
// [START aiplatform_batch_create_features_sample]
25+
26+
import com.google.api.gax.longrunning.OperationFuture;
27+
import com.google.cloud.aiplatform.v1.BatchCreateFeaturesOperationMetadata;
28+
import com.google.cloud.aiplatform.v1.BatchCreateFeaturesRequest;
29+
import com.google.cloud.aiplatform.v1.BatchCreateFeaturesResponse;
30+
import com.google.cloud.aiplatform.v1.CreateFeatureRequest;
31+
import com.google.cloud.aiplatform.v1.EntityTypeName;
32+
import com.google.cloud.aiplatform.v1.Feature;
33+
import com.google.cloud.aiplatform.v1.Feature.ValueType;
34+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
35+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
36+
import java.io.IOException;
37+
import java.util.ArrayList;
38+
import java.util.List;
39+
import java.util.concurrent.ExecutionException;
40+
import java.util.concurrent.TimeUnit;
41+
import java.util.concurrent.TimeoutException;
42+
43+
public class BatchCreateFeaturesSample {
44+
45+
public static void main(String[] args)
46+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
47+
// TODO(developer): Replace these variables before running the sample.
48+
String project = "YOUR_PROJECT_ID";
49+
String featurestoreId = "YOUR_FEATURESTORE_ID";
50+
String entityTypeId = "YOUR_ENTITY_TYPE_ID";
51+
String location = "us-central1";
52+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
53+
int timeout = 300;
54+
batchCreateFeaturesSample(project, featurestoreId, entityTypeId, location, endpoint, timeout);
55+
}
56+
57+
static void batchCreateFeaturesSample(
58+
String project,
59+
String featurestoreId,
60+
String entityTypeId,
61+
String location,
62+
String endpoint,
63+
int timeout)
64+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
65+
FeaturestoreServiceSettings featurestoreServiceSettings =
66+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
67+
68+
// Initialize client that will be used to send requests. This client only needs to be created
69+
// once, and can be reused for multiple requests. After completing all of your requests, call
70+
// the "close" method on the client to safely clean up any remaining background resources.
71+
try (FeaturestoreServiceClient featurestoreServiceClient =
72+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
73+
74+
List<CreateFeatureRequest> createFeatureRequests = new ArrayList<>();
75+
76+
Feature titleFeature =
77+
Feature.newBuilder()
78+
.setDescription("The title of the movie")
79+
.setValueType(ValueType.STRING)
80+
.build();
81+
Feature genresFeature =
82+
Feature.newBuilder()
83+
.setDescription("The genres of the movie")
84+
.setValueType(ValueType.STRING)
85+
.build();
86+
Feature averageRatingFeature =
87+
Feature.newBuilder()
88+
.setDescription("The average rating for the movie, range is [1.0-5.0]")
89+
.setValueType(ValueType.DOUBLE)
90+
.build();
91+
92+
createFeatureRequests.add(
93+
CreateFeatureRequest.newBuilder().setFeature(titleFeature).setFeatureId("title").build());
94+
95+
createFeatureRequests.add(
96+
CreateFeatureRequest.newBuilder()
97+
.setFeature(genresFeature)
98+
.setFeatureId("genres")
99+
.build());
100+
101+
createFeatureRequests.add(
102+
CreateFeatureRequest.newBuilder()
103+
.setFeature(averageRatingFeature)
104+
.setFeatureId("average_rating")
105+
.build());
106+
107+
BatchCreateFeaturesRequest batchCreateFeaturesRequest =
108+
BatchCreateFeaturesRequest.newBuilder()
109+
.setParent(
110+
EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString())
111+
.addAllRequests(createFeatureRequests)
112+
.build();
113+
114+
OperationFuture<BatchCreateFeaturesResponse, BatchCreateFeaturesOperationMetadata>
115+
batchCreateFeaturesFuture =
116+
featurestoreServiceClient.batchCreateFeaturesAsync(batchCreateFeaturesRequest);
117+
System.out.format(
118+
"Operation name: %s%n", batchCreateFeaturesFuture.getInitialFuture().get().getName());
119+
System.out.println("Waiting for operation to finish...");
120+
BatchCreateFeaturesResponse batchCreateFeaturesResponse =
121+
batchCreateFeaturesFuture.get(timeout, TimeUnit.SECONDS);
122+
System.out.println("Batch Create Features Response");
123+
System.out.println(batchCreateFeaturesResponse);
124+
featurestoreServiceClient.close();
125+
}
126+
}
127+
}
128+
// [END aiplatform_batch_create_features_sample]
Lines changed: 135 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,135 @@
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+
* Batch read feature values from a featurestore, as determined by your
18+
* read instances list file, to export data. See
19+
* https://cloud.google.com/vertex-ai/docs/featurestore/setup before running
20+
* the code snippet
21+
*/
22+
23+
package aiplatform;
24+
25+
// [START aiplatform_batch_read_feature_values_sample]
26+
27+
import com.google.api.gax.longrunning.OperationFuture;
28+
import com.google.cloud.aiplatform.v1.BatchReadFeatureValuesOperationMetadata;
29+
import com.google.cloud.aiplatform.v1.BatchReadFeatureValuesRequest;
30+
import com.google.cloud.aiplatform.v1.BatchReadFeatureValuesRequest.EntityTypeSpec;
31+
import com.google.cloud.aiplatform.v1.BatchReadFeatureValuesResponse;
32+
import com.google.cloud.aiplatform.v1.BigQueryDestination;
33+
import com.google.cloud.aiplatform.v1.CsvSource;
34+
import com.google.cloud.aiplatform.v1.FeatureSelector;
35+
import com.google.cloud.aiplatform.v1.FeatureValueDestination;
36+
import com.google.cloud.aiplatform.v1.FeaturestoreName;
37+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
38+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
39+
import com.google.cloud.aiplatform.v1.GcsSource;
40+
import com.google.cloud.aiplatform.v1.IdMatcher;
41+
import java.io.IOException;
42+
import java.util.ArrayList;
43+
import java.util.Arrays;
44+
import java.util.List;
45+
import java.util.concurrent.ExecutionException;
46+
import java.util.concurrent.TimeUnit;
47+
import java.util.concurrent.TimeoutException;
48+
49+
public class BatchReadFeatureValuesSample {
50+
51+
public static void main(String[] args)
52+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
53+
// TODO(developer): Replace these variables before running the sample.
54+
String project = "YOUR_PROJECT_ID";
55+
String featurestoreId = "YOUR_FEATURESTORE_ID";
56+
String entityTypeId = "YOUR_ENTITY_TYPE_ID";
57+
String inputCsvFile = "YOU_INPUT_CSV_FILE";
58+
String destinationTableUri = "YOUR_DESTINATION_TABLE_URI";
59+
List<String> featureSelectorIds = Arrays.asList("title", "genres", "average_rating");
60+
String location = "us-central1";
61+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
62+
int timeout = 300;
63+
batchReadFeatureValuesSample(
64+
project,
65+
featurestoreId,
66+
entityTypeId,
67+
inputCsvFile,
68+
destinationTableUri,
69+
featureSelectorIds,
70+
location,
71+
endpoint,
72+
timeout);
73+
}
74+
75+
static void batchReadFeatureValuesSample(
76+
String project,
77+
String featurestoreId,
78+
String entityTypeId,
79+
String inputCsvFile,
80+
String destinationTableUri,
81+
List<String> featureSelectorIds,
82+
String location,
83+
String endpoint,
84+
int timeout)
85+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
86+
FeaturestoreServiceSettings featurestoreServiceSettings =
87+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
88+
89+
// Initialize client that will be used to send requests. This client only needs to be created
90+
// once, and can be reused for multiple requests. After completing all of your requests, call
91+
// the "close" method on the client to safely clean up any remaining background resources.
92+
try (FeaturestoreServiceClient featurestoreServiceClient =
93+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
94+
95+
List<EntityTypeSpec> entityTypeSpecs = new ArrayList<>();
96+
97+
FeatureSelector featureSelector =
98+
FeatureSelector.newBuilder()
99+
.setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build())
100+
.build();
101+
EntityTypeSpec entityTypeSpec =
102+
EntityTypeSpec.newBuilder()
103+
.setEntityTypeId(entityTypeId)
104+
.setFeatureSelector(featureSelector)
105+
.build();
106+
107+
entityTypeSpecs.add(entityTypeSpec);
108+
109+
BigQueryDestination bigQueryDestination =
110+
BigQueryDestination.newBuilder().setOutputUri(destinationTableUri).build();
111+
GcsSource gcsSource = GcsSource.newBuilder().addUris(inputCsvFile).build();
112+
BatchReadFeatureValuesRequest batchReadFeatureValuesRequest =
113+
BatchReadFeatureValuesRequest.newBuilder()
114+
.setFeaturestore(FeaturestoreName.of(project, location, featurestoreId).toString())
115+
.setCsvReadInstances(CsvSource.newBuilder().setGcsSource(gcsSource))
116+
.setDestination(
117+
FeatureValueDestination.newBuilder().setBigqueryDestination(bigQueryDestination))
118+
.addAllEntityTypeSpecs(entityTypeSpecs)
119+
.build();
120+
121+
OperationFuture<BatchReadFeatureValuesResponse, BatchReadFeatureValuesOperationMetadata>
122+
batchReadFeatureValuesFuture =
123+
featurestoreServiceClient.batchReadFeatureValuesAsync(batchReadFeatureValuesRequest);
124+
System.out.format(
125+
"Operation name: %s%n", batchReadFeatureValuesFuture.getInitialFuture().get().getName());
126+
System.out.println("Waiting for operation to finish...");
127+
BatchReadFeatureValuesResponse batchReadFeatureValuesResponse =
128+
batchReadFeatureValuesFuture.get(timeout, TimeUnit.SECONDS);
129+
System.out.println("Batch Read Feature Values Response");
130+
System.out.println(batchReadFeatureValuesResponse);
131+
featurestoreServiceClient.close();
132+
}
133+
}
134+
}
135+
// [END aiplatform_batch_read_feature_values_sample]
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
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+
* Bulk export feature values from 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_export_feature_values_sample]
25+
26+
import com.google.api.gax.longrunning.OperationFuture;
27+
import com.google.cloud.aiplatform.v1.BigQueryDestination;
28+
import com.google.cloud.aiplatform.v1.EntityTypeName;
29+
import com.google.cloud.aiplatform.v1.ExportFeatureValuesOperationMetadata;
30+
import com.google.cloud.aiplatform.v1.ExportFeatureValuesRequest;
31+
import com.google.cloud.aiplatform.v1.ExportFeatureValuesRequest.FullExport;
32+
import com.google.cloud.aiplatform.v1.ExportFeatureValuesResponse;
33+
import com.google.cloud.aiplatform.v1.FeatureSelector;
34+
import com.google.cloud.aiplatform.v1.FeatureValueDestination;
35+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceClient;
36+
import com.google.cloud.aiplatform.v1.FeaturestoreServiceSettings;
37+
import com.google.cloud.aiplatform.v1.IdMatcher;
38+
import java.io.IOException;
39+
import java.util.Arrays;
40+
import java.util.List;
41+
import java.util.concurrent.ExecutionException;
42+
import java.util.concurrent.TimeUnit;
43+
import java.util.concurrent.TimeoutException;
44+
45+
public class ExportFeatureValuesSample {
46+
47+
public static void main(String[] args)
48+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
49+
// TODO(developer): Replace these variables before running the sample.
50+
String project = "YOUR_PROJECT_ID";
51+
String featurestoreId = "YOUR_FEATURESTORE_ID";
52+
String entityTypeId = "YOUR_ENTITY_TYPE_ID";
53+
String destinationTableUri = "YOUR_DESTINATION_TABLE_URI";
54+
List<String> featureSelectorIds = Arrays.asList("title", "genres", "average_rating");
55+
String location = "us-central1";
56+
String endpoint = "us-central1-aiplatform.googleapis.com:443";
57+
int timeout = 300;
58+
exportFeatureValuesSample(
59+
project,
60+
featurestoreId,
61+
entityTypeId,
62+
destinationTableUri,
63+
featureSelectorIds,
64+
location,
65+
endpoint,
66+
timeout);
67+
}
68+
69+
static void exportFeatureValuesSample(
70+
String project,
71+
String featurestoreId,
72+
String entityTypeId,
73+
String destinationTableUri,
74+
List<String> featureSelectorIds,
75+
String location,
76+
String endpoint,
77+
int timeout)
78+
throws IOException, InterruptedException, ExecutionException, TimeoutException {
79+
FeaturestoreServiceSettings featurestoreServiceSettings =
80+
FeaturestoreServiceSettings.newBuilder().setEndpoint(endpoint).build();
81+
82+
// Initialize client that will be used to send requests. This client only needs to be created
83+
// once, and can be reused for multiple requests. After completing all of your requests, call
84+
// the "close" method on the client to safely clean up any remaining background resources.
85+
try (FeaturestoreServiceClient featurestoreServiceClient =
86+
FeaturestoreServiceClient.create(featurestoreServiceSettings)) {
87+
88+
FeatureSelector featureSelector =
89+
FeatureSelector.newBuilder()
90+
.setIdMatcher(IdMatcher.newBuilder().addAllIds(featureSelectorIds).build())
91+
.build();
92+
93+
ExportFeatureValuesRequest exportFeatureValuesRequest =
94+
ExportFeatureValuesRequest.newBuilder()
95+
.setEntityType(
96+
EntityTypeName.of(project, location, featurestoreId, entityTypeId).toString())
97+
.setDestination(
98+
FeatureValueDestination.newBuilder()
99+
.setBigqueryDestination(
100+
BigQueryDestination.newBuilder().setOutputUri(destinationTableUri)))
101+
.setFeatureSelector(featureSelector)
102+
.setFullExport(FullExport.newBuilder())
103+
.build();
104+
105+
OperationFuture<ExportFeatureValuesResponse, ExportFeatureValuesOperationMetadata>
106+
exportFeatureValuesFuture =
107+
featurestoreServiceClient.exportFeatureValuesAsync(exportFeatureValuesRequest);
108+
System.out.format(
109+
"Operation name: %s%n", exportFeatureValuesFuture.getInitialFuture().get().getName());
110+
System.out.println("Waiting for operation to finish...");
111+
ExportFeatureValuesResponse exportFeatureValuesResponse =
112+
exportFeatureValuesFuture.get(timeout, TimeUnit.SECONDS);
113+
System.out.println("Export Feature Values Response");
114+
System.out.println(exportFeatureValuesResponse);
115+
featurestoreServiceClient.close();
116+
}
117+
}
118+
}
119+
// [END aiplatform_export_feature_values_sample]

0 commit comments

Comments
 (0)