|
| 1 | +# Copyright 2021 Google LLC |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +# Batch read feature values from a featurestore, as determined by your read |
| 16 | +# instances list file, to export data. |
| 17 | +# See https://cloud.google.com/vertex-ai/docs/featurestore/setup before running |
| 18 | +# the code snippet |
| 19 | + |
| 20 | +# [START aiplatform_batch_read_feature_values_sample] |
| 21 | +from google.cloud import aiplatform_v1beta1 as aiplatform |
| 22 | + |
| 23 | + |
| 24 | +def batch_read_feature_values_sample( |
| 25 | + project: str, |
| 26 | + featurestore_id: str, |
| 27 | + input_csv_file: str, |
| 28 | + destination_table_uri: str, |
| 29 | + location: str = "us-central1", |
| 30 | + api_endpoint: str = "us-central1-aiplatform.googleapis.com", |
| 31 | + timeout: int = 300, |
| 32 | +): |
| 33 | + # The AI Platform services require regional API endpoints, which need to be |
| 34 | + # in the same region or multi-region overlap with the Feature Store location. |
| 35 | + client_options = {"api_endpoint": api_endpoint} |
| 36 | + # Initialize client that will be used to create and send requests. |
| 37 | + # This client only needs to be created once, and can be reused for multiple requests. |
| 38 | + client = aiplatform.FeaturestoreServiceClient(client_options=client_options) |
| 39 | + featurestore = ( |
| 40 | + f"projects/{project}/locations/{location}/featurestores/{featurestore_id}" |
| 41 | + ) |
| 42 | + csv_read_instances = aiplatform.CsvSource( |
| 43 | + gcs_source=aiplatform.GcsSource(uris=[input_csv_file]) |
| 44 | + ) |
| 45 | + destination = aiplatform.FeatureValueDestination( |
| 46 | + bigquery_destination=aiplatform.BigQueryDestination( |
| 47 | + # Output to BigQuery table created earlier |
| 48 | + output_uri=destination_table_uri |
| 49 | + ) |
| 50 | + ) |
| 51 | + |
| 52 | + users_feature_selector = aiplatform.FeatureSelector( |
| 53 | + id_matcher=aiplatform.IdMatcher(ids=["age", "gender", "liked_genres"]) |
| 54 | + ) |
| 55 | + users_entity_type_spec = aiplatform.BatchReadFeatureValuesRequest.EntityTypeSpec( |
| 56 | + # Read the 'age', 'gender' and 'liked_genres' features from the 'perm_users' entity |
| 57 | + entity_type_id="perm_users", |
| 58 | + feature_selector=users_feature_selector, |
| 59 | + ) |
| 60 | + |
| 61 | + movies_feature_selector = aiplatform.FeatureSelector( |
| 62 | + id_matcher=aiplatform.IdMatcher(ids=["*"]) |
| 63 | + ) |
| 64 | + movies_entity_type_spec = aiplatform.BatchReadFeatureValuesRequest.EntityTypeSpec( |
| 65 | + # Read the all features from the 'perm_movies' entity |
| 66 | + entity_type_id="perm_movies", |
| 67 | + feature_selector=movies_feature_selector, |
| 68 | + ) |
| 69 | + |
| 70 | + entity_type_specs = [users_entity_type_spec, movies_entity_type_spec] |
| 71 | + # Batch serving request from CSV |
| 72 | + batch_read_feature_values_request = aiplatform.BatchReadFeatureValuesRequest( |
| 73 | + featurestore=featurestore, |
| 74 | + csv_read_instances=csv_read_instances, |
| 75 | + destination=destination, |
| 76 | + entity_type_specs=entity_type_specs, |
| 77 | + ) |
| 78 | + lro_response = client.batch_read_feature_values( |
| 79 | + request=batch_read_feature_values_request |
| 80 | + ) |
| 81 | + print("Long running operation:", lro_response.operation.name) |
| 82 | + batch_read_feature_values_response = lro_response.result(timeout=timeout) |
| 83 | + print("batch_read_feature_values_response:", batch_read_feature_values_response) |
| 84 | + |
| 85 | + |
| 86 | +# [END aiplatform_batch_read_feature_values_sample] |
0 commit comments