|
| 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] |
0 commit comments