forked from feast-dev/feast
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstream_feature_view.py
More file actions
287 lines (254 loc) · 10.4 KB
/
Copy pathstream_feature_view.py
File metadata and controls
287 lines (254 loc) · 10.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
import functools
import warnings
from datetime import timedelta
from types import MethodType
from typing import Dict, List, Optional, Union
import dill
from google.protobuf.duration_pb2 import Duration
from feast import utils
from feast.aggregation import Aggregation
from feast.data_source import DataSource, KafkaSource
from feast.entity import Entity
from feast.feature_view import FeatureView
from feast.field import Field
from feast.protos.feast.core.DataSource_pb2 import DataSource as DataSourceProto
from feast.protos.feast.core.FeatureView_pb2 import (
MaterializationInterval as MaterializationIntervalProto,
)
from feast.protos.feast.core.OnDemandFeatureView_pb2 import (
UserDefinedFunction as UserDefinedFunctionProto,
)
from feast.protos.feast.core.StreamFeatureView_pb2 import (
StreamFeatureView as StreamFeatureViewProto,
)
from feast.protos.feast.core.StreamFeatureView_pb2 import (
StreamFeatureViewMeta as StreamFeatureViewMetaProto,
)
from feast.protos.feast.core.StreamFeatureView_pb2 import (
StreamFeatureViewSpec as StreamFeatureViewSpecProto,
)
warnings.simplefilter("once", RuntimeWarning)
SUPPORTED_STREAM_SOURCES = {"KafkaSource", "PushSource"}
class StreamFeatureView(FeatureView):
"""
NOTE: Stream Feature Views are not yet fully implemented and exist to allow users to register their stream sources and
schemas with Feast.
"""
def __init__(
self,
*,
name: Optional[str] = None,
entities: Optional[Union[List[Entity], List[str]]] = None,
ttl: Optional[timedelta] = None,
tags: Optional[Dict[str, str]] = None,
online: Optional[bool] = True,
description: Optional[str] = "",
owner: Optional[str] = "",
schema: Optional[List[Field]] = None,
source: Optional[DataSource] = None,
aggregations: Optional[List[Aggregation]] = None,
mode: Optional[str] = "spark", # Mode of ingestion/transformation
timestamp_field: Optional[str] = "", # Timestamp for aggregation
udf: Optional[MethodType] = None,
):
warnings.warn(
"Stream Feature Views are experimental features in alpha development. "
"Some functionality may still be unstable so functionality can change in the future.",
RuntimeWarning,
)
if source is None:
raise ValueError("Stream Feature views need a source specified")
# source uses the batch_source of the kafkasource in feature_view
if (
type(source).__name__ not in SUPPORTED_STREAM_SOURCES
and source.to_proto().type != DataSourceProto.SourceType.CUSTOM_SOURCE
):
raise ValueError(
f"Stream feature views need a stream source, expected one of {SUPPORTED_STREAM_SOURCES} "
f"or CUSTOM_SOURCE, got {type(source).__name__}: {source.name} instead "
)
self.aggregations = aggregations or []
self.mode = mode
self.timestamp_field = timestamp_field
self.udf = udf
_batch_source = None
if isinstance(source, KafkaSource):
_batch_source = source.batch_source if source.batch_source else None
super().__init__(
name=name,
entities=entities,
ttl=ttl,
batch_source=_batch_source,
stream_source=source,
tags=tags,
online=online,
description=description,
owner=owner,
schema=schema,
source=source,
)
def __eq__(self, other):
if not isinstance(other, StreamFeatureView):
raise TypeError("Comparisons should only involve StreamFeatureViews")
if not super().__eq__(other):
return False
if (
self.mode != other.mode
or self.timestamp_field != other.timestamp_field
or self.udf.__code__.co_code != other.udf.__code__.co_code
or self.aggregations != other.aggregations
):
return False
return True
def __hash__(self):
return super().__hash__()
def to_proto(self):
meta = StreamFeatureViewMetaProto(materialization_intervals=[])
if self.created_timestamp:
meta.created_timestamp.FromDatetime(self.created_timestamp)
if self.last_updated_timestamp:
meta.last_updated_timestamp.FromDatetime(self.last_updated_timestamp)
for interval in self.materialization_intervals:
interval_proto = MaterializationIntervalProto()
interval_proto.start_time.FromDatetime(interval[0])
interval_proto.end_time.FromDatetime(interval[1])
meta.materialization_intervals.append(interval_proto)
ttl_duration = None
if self.ttl is not None:
ttl_duration = Duration()
ttl_duration.FromTimedelta(self.ttl)
if self.batch_source:
batch_source_proto = self.batch_source.to_proto()
batch_source_proto.data_source_class_type = f"{self.batch_source.__class__.__module__}.{self.batch_source.__class__.__name__}"
stream_source_proto = None
if self.stream_source:
stream_source_proto = self.stream_source.to_proto()
stream_source_proto.data_source_class_type = f"{self.stream_source.__class__.__module__}.{self.stream_source.__class__.__name__}"
spec = StreamFeatureViewSpecProto(
name=self.name,
entities=self.entities,
entity_columns=[field.to_proto() for field in self.entity_columns],
features=[field.to_proto() for field in self.schema],
user_defined_function=UserDefinedFunctionProto(
name=self.udf.__name__, body=dill.dumps(self.udf, recurse=True),
)
if self.udf
else None,
description=self.description,
tags=self.tags,
owner=self.owner,
ttl=(ttl_duration if ttl_duration is not None else None),
online=self.online,
batch_source=batch_source_proto or None,
stream_source=stream_source_proto,
timestamp_field=self.timestamp_field,
aggregations=[agg.to_proto() for agg in self.aggregations],
mode=self.mode,
)
return StreamFeatureViewProto(spec=spec, meta=meta)
@classmethod
def from_proto(cls, sfv_proto):
batch_source = (
DataSource.from_proto(sfv_proto.spec.batch_source)
if sfv_proto.spec.HasField("batch_source")
else None
)
stream_source = (
DataSource.from_proto(sfv_proto.spec.stream_source)
if sfv_proto.spec.HasField("stream_source")
else None
)
udf = (
dill.loads(sfv_proto.spec.user_defined_function.body)
if sfv_proto.spec.HasField("user_defined_function")
else None
)
sfv_feature_view = cls(
name=sfv_proto.spec.name,
description=sfv_proto.spec.description,
tags=dict(sfv_proto.spec.tags),
owner=sfv_proto.spec.owner,
online=sfv_proto.spec.online,
schema=[
Field.from_proto(field_proto) for field_proto in sfv_proto.spec.features
],
ttl=(
timedelta(days=0)
if sfv_proto.spec.ttl.ToNanoseconds() == 0
else sfv_proto.spec.ttl.ToTimedelta()
),
source=stream_source,
mode=sfv_proto.spec.mode,
udf=udf,
aggregations=[
Aggregation.from_proto(agg_proto)
for agg_proto in sfv_proto.spec.aggregations
],
timestamp_field=sfv_proto.spec.timestamp_field,
)
if batch_source:
sfv_feature_view.batch_source = batch_source
if stream_source:
sfv_feature_view.stream_source = stream_source
sfv_feature_view.entities = list(sfv_proto.spec.entities)
sfv_feature_view.features = [
Field.from_proto(field_proto) for field_proto in sfv_proto.spec.features
]
if sfv_proto.meta.HasField("created_timestamp"):
sfv_feature_view.created_timestamp = (
sfv_proto.meta.created_timestamp.ToDatetime()
)
if sfv_proto.meta.HasField("last_updated_timestamp"):
sfv_feature_view.last_updated_timestamp = (
sfv_proto.meta.last_updated_timestamp.ToDatetime()
)
for interval in sfv_proto.meta.materialization_intervals:
stream_feature_view.materialization_intervals.append(
(
utils.make_tzaware(interval.start_time.ToDatetime()),
utils.make_tzaware(interval.end_time.ToDatetime()),
)
)
return sfv_feature_view
def stream_feature_view(
*,
entities: Optional[Union[List[Entity], List[str]]] = None,
ttl: Optional[timedelta] = None,
tags: Optional[Dict[str, str]] = None,
online: Optional[bool] = True,
description: Optional[str] = "",
owner: Optional[str] = "",
schema: Optional[List[Field]] = None,
source: Optional[DataSource] = None,
aggregations: Optional[List[Aggregation]] = None,
mode: Optional[str] = "spark", # Mode of ingestion/transformation
timestamp_field: Optional[str] = "", # Timestamp for aggregation
):
"""
Creates an StreamFeatureView object with the given user function as udf.
"""
def mainify(obj):
# Needed to allow dill to properly serialize the udf. Otherwise, clients will need to have a file with the same
# name as the original file defining the sfv.
if obj.__module__ != "__main__":
obj.__module__ = "__main__"
def decorator(user_function):
mainify(user_function)
stream_feature_view_obj = StreamFeatureView(
name=user_function.__name__,
entities=entities,
ttl=ttl,
source=source,
schema=schema,
udf=user_function,
description=description,
tags=tags,
online=online,
owner=owner,
aggregations=aggregations,
mode=mode,
timestamp_field=timestamp_field,
)
functools.update_wrapper(wrapper=stream_feature_view_obj, wrapped=user_function)
return stream_feature_view_obj
return decorator