|
| 1 | +import uuid |
| 2 | +from datetime import datetime |
| 3 | +from pathlib import Path |
| 4 | +from typing import Any, Callable, List, Literal, Optional, Union |
| 5 | + |
| 6 | +import pandas as pd |
| 7 | +import pyarrow as pa |
| 8 | +import pyarrow.parquet |
| 9 | +from pydantic import StrictStr |
| 10 | + |
| 11 | +from feast import OnDemandFeatureView |
| 12 | +from feast.data_source import DataSource |
| 13 | +from feast.feature_logging import LoggingConfig, LoggingSource |
| 14 | +from feast.feature_view import FeatureView |
| 15 | +from feast.infra.offline_stores.offline_store import ( |
| 16 | + OfflineStore, |
| 17 | + RetrievalJob, |
| 18 | +) |
| 19 | +from feast.infra.registry.base_registry import BaseRegistry |
| 20 | +from feast.infra.registry.registry import Registry |
| 21 | +from feast.repo_config import FeastConfigBaseModel, RepoConfig |
| 22 | +from feast.usage import log_exceptions_and_usage |
| 23 | + |
| 24 | + |
| 25 | +class RemoteOfflineStoreConfig(FeastConfigBaseModel): |
| 26 | + |
| 27 | + offline_type: StrictStr = "remote" |
| 28 | + """ str: Provider name or a class name that implements Offline store.""" |
| 29 | + |
| 30 | + path: StrictStr = "" |
| 31 | + """ str: Path to metadata store. |
| 32 | + If offline_type is 'remote', then this is a URL for offline server """ |
| 33 | + |
| 34 | + host: StrictStr = "" |
| 35 | + """ str: host to offline store. |
| 36 | + If offline_type is 'remote', then this is a host URL for offline store of arrow flight server """ |
| 37 | + |
| 38 | + port: StrictStr = "" |
| 39 | + """ str: host to offline store.""" |
| 40 | + |
| 41 | + |
| 42 | +class RemoteRetrievalJob(RetrievalJob): |
| 43 | + def __init__( |
| 44 | + self, |
| 45 | + config: RepoConfig, |
| 46 | + feature_refs: List[str], |
| 47 | + entity_df: Union[pd.DataFrame, str], |
| 48 | + # TODO add missing parameters from the OfflineStore API |
| 49 | + ): |
| 50 | + # Generate unique command identifier |
| 51 | + self.command = str(uuid.uuid4()) |
| 52 | + # Initialize the client connection |
| 53 | + self.client = pa.flight.connect(f"grpc://{config.offline_store.host}:{config.offline_store.port}") |
| 54 | + # Put API parameters |
| 55 | + self._put_parameters(feature_refs, entity_df) |
| 56 | + |
| 57 | + def _put_parameters(self, feature_refs, entity_df): |
| 58 | + entity_df_table = pa.Table.from_pandas(entity_df) |
| 59 | + historical_flight_descriptor = pa.flight.FlightDescriptor.for_command(self.command) |
| 60 | + writer, _ = self.client.do_put(historical_flight_descriptor, |
| 61 | + entity_df_table.schema.with_metadata({ |
| 62 | + 'command': self.command, |
| 63 | + 'api': 'get_historical_features', |
| 64 | + 'param': 'entity_df'})) |
| 65 | + writer.write_table(entity_df_table) |
| 66 | + writer.close() |
| 67 | + |
| 68 | + features_array = pa.array(feature_refs) |
| 69 | + features_batch = pa.RecordBatch.from_arrays([features_array], ['features']) |
| 70 | + writer, _ = self.client.do_put(historical_flight_descriptor, |
| 71 | + features_batch.schema.with_metadata({ |
| 72 | + 'command': self.command, |
| 73 | + 'api': 'get_historical_features', |
| 74 | + 'param': 'features'})) |
| 75 | + writer.write_batch(features_batch) |
| 76 | + writer.close() |
| 77 | + |
| 78 | + # Invoked to realize the Pandas DataFrame |
| 79 | + def _to_df_internal(self, timeout: Optional[int] = None) -> pd.DataFrame: |
| 80 | + # We use arrow format because it gives better control of the table schema |
| 81 | + return self._to_arrow_internal().to_pandas() |
| 82 | + |
| 83 | + # Invoked to synchronously execute the underlying query and return the result as an arrow table |
| 84 | + # This is where do_get service is invoked |
| 85 | + def _to_arrow_internal(self, timeout: Optional[int] = None) -> pa.Table: |
| 86 | + upload_descriptor = pa.flight.FlightDescriptor.for_command(self.command) |
| 87 | + flight = self.client.get_flight_info(upload_descriptor) |
| 88 | + ticket = flight.endpoints[0].ticket |
| 89 | + |
| 90 | + reader = self.client.do_get(ticket) |
| 91 | + return reader.read_all() |
| 92 | + |
| 93 | + @property |
| 94 | + def on_demand_feature_views(self) -> List[OnDemandFeatureView]: |
| 95 | + return [] |
| 96 | + |
| 97 | + |
| 98 | +class RemoteOfflineStore(OfflineStore): |
| 99 | + def __init__( |
| 100 | + self, |
| 101 | + |
| 102 | + arrow_host, |
| 103 | + arrow_port |
| 104 | + ): |
| 105 | + self.arrow_host = arrow_host |
| 106 | + self.arrow_port = arrow_port |
| 107 | + |
| 108 | + @log_exceptions_and_usage(offline_store="remote") |
| 109 | + def get_historical_features( |
| 110 | + self, |
| 111 | + config: RepoConfig, |
| 112 | + feature_views: List[FeatureView], |
| 113 | + feature_refs: List[str], |
| 114 | + entity_df: Union[pd.DataFrame, str], |
| 115 | + registry: Registry = None, |
| 116 | + project: str = '', |
| 117 | + full_feature_names: bool = False, |
| 118 | + ) -> RemoteRetrievalJob: |
| 119 | + offline_store_config = config.offline_store |
| 120 | + assert isinstance(config.offline_store_config, RemoteOfflineStoreConfig) |
| 121 | + store_type = offline_store_config.type |
| 122 | + port = offline_store_config.port |
| 123 | + host = offline_store_config.host |
| 124 | + |
| 125 | + return RemoteRetrievalJob(RepoConfig, feature_refs, entity_df) |
| 126 | + |
| 127 | + @log_exceptions_and_usage(offline_store="remote") |
| 128 | + def pull_latest_from_table_or_query(self, |
| 129 | + config: RepoConfig, |
| 130 | + data_source: DataSource, |
| 131 | + join_key_columns: List[str], |
| 132 | + feature_name_columns: List[str], |
| 133 | + timestamp_field: str, |
| 134 | + created_timestamp_column: Optional[str], |
| 135 | + start_date: datetime, |
| 136 | + end_date: datetime) -> RetrievalJob: |
| 137 | + """ Pulls data from the offline store for use in materialization.""" |
| 138 | + print("Pulling latest features from my offline store") |
| 139 | + # Implementation here. |
| 140 | + pass |
| 141 | + |
| 142 | + def write_logged_features( |
| 143 | + config: RepoConfig, |
| 144 | + data: Union[pyarrow.Table, Path], |
| 145 | + source: LoggingSource, |
| 146 | + logging_config: LoggingConfig, |
| 147 | + registry: BaseRegistry, |
| 148 | + ): |
| 149 | + """ Optional method to have Feast support logging your online features.""" |
| 150 | + # Implementation here. |
| 151 | + pass |
| 152 | + |
| 153 | + def offline_write_batch( |
| 154 | + config: RepoConfig, |
| 155 | + feature_view: FeatureView, |
| 156 | + table: pyarrow.Table, |
| 157 | + progress: Optional[Callable[[int], Any]], |
| 158 | + ): |
| 159 | + # Implementation here. |
| 160 | + pass |
0 commit comments