forked from feldera/feldera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathformats.py
More file actions
306 lines (245 loc) · 10.4 KB
/
formats.py
File metadata and controls
306 lines (245 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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
from typing import Optional, Mapping
from typing_extensions import Self
from enum import Enum
import json
from abc import ABC
class Format(ABC):
"""
Base class for all data formats.
"""
def to_dict(self) -> dict:
"""
Serialize to a dict to be used in the API request.
:meta private:
"""
raise NotImplementedError
class JSONUpdateFormat(Enum):
"""
Supported JSON data change event formats.
Each element in a JSON-formatted input stream specifies
an update to one or more records in an input table. We support
several different ways to represent such updates.
https://www.feldera.com/docs/api/json/#the-insertdelete-format
"""
InsertDelete = 1
"""
Insert/delete format.
Each element in the input stream consists of an "insert" or "delete"
command and a record to be inserted to or deleted from the input table.
Example: `{"insert": {"id": 1, "name": "Alice"}, "delete": {"id": 2, "name": "Bob"}}`
Here, `id` and `name` are the columns in the table.
"""
Raw = 2
"""
Raw input format.
This format is suitable for insert-only streams (no deletions).
Each element in the input stream contains a record without any
additional envelope that gets inserted in the input table.
Example: `{"id": 1, "name": "Alice"}`
Here, `id` and `name` are the columns in the table.
"""
def __str__(self):
match self:
case JSONUpdateFormat.InsertDelete:
return "insert_delete"
case JSONUpdateFormat.Raw:
return "raw"
class JSONFormat(Format):
"""
Used to represent data ingested and output from Feldera in the JSON format.
"""
def __init__(self, config: Optional[dict] = None):
"""
Creates a new JSONFormat instance.
:param config: Optional. Configuration for the JSON format.
"""
self.config: dict = config or {
"array": False,
}
def with_update_format(self, update_format: JSONUpdateFormat) -> Self:
"""
Specifies the format of the data change events in the JSON data stream.
"""
self.config["update_format"] = update_format.__str__()
return self
def with_array(self, array: bool) -> Self:
"""
Set to `True` if updates in this stream are packaged into JSON arrays.
Example: `[{"id": 1, "name": "Alice"}, {"id": 2, "name": "Bob"}]`
"""
self.config["array"] = array
return self
def to_dict(self):
"""
Serialize to a dict to be used in the API request.
:meta private:
"""
return {
"name": "json",
"config": self.config
}
class CSVFormat(Format):
"""
Used to represent data ingested and output from Feldera in the CSV format.
https://www.feldera.com/docs/api/csv
"""
def to_dict(self) -> dict:
"""
Serialize to a dict to be used in the API request.
:meta private:
"""
return {
"name": "csv"
}
class AvroFormat(Format):
"""
Avro output format configuration.
:param config:
A dictionary that contains the entire configuration for the Avro format.
:param schema:
Avro schema used to encode output records.
Specified as a string containing schema definition in JSON format.
This schema must match precisely the SQL view definition, including nullability of columns.
:param skip_schema_id:
Set `True` if the serialized message should only contain the data and
not contain the magic byte + schema ID. `False` by default.
The first 5 bytes of the Avro message are the magic byte and 4-byte schema ID.
https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html#wire-format
:param registry_urls:
List of schema registry URLs. When non-empty, the connector will
post the schema to the registry and use the schema id returned
by the registry. Otherwise, schema id 0 is used.
:param registry_headers:
Custom headers that will be added to every call to the schema registry.
This option requires `registry_urls` to be set.
:param registry_proxy:
Proxy that will be used to access the schema registry.
Requires `registry_urls` to be set.
:param registry_timeout_secs:
Timeout in seconds used to connect to the registry.
Requires `registry_urls` to be set.
:param registry_username:
Username used to authenticate with the registry.
Requires `registry_urls` to be set. This option is mutually exclusive with
token-based authentication (see `registry_authorization_token`).
:param registry_password:
Password used to authenticate with the registry.
Requires `registry_urls` to be set.
:param registry_authorization_token:
Token used to authenticate with the registry.
Requires `registry_urls` to be set. This option is mutually exclusive with
password-based authentication (see `registry_username` and `registry_password`).
"""
def __init__(
self,
config: Optional[dict] = None,
schema: Optional[str] = None,
skip_schema_id: Optional[bool] = False,
registry_urls: Optional[list[str]] = None,
registry_headers: Optional[Mapping[str, str]] = None,
registry_proxy: Optional[str] = None,
registry_timeout_secs: Optional[int] = None,
registry_username: Optional[str] = None,
registry_password: Optional[str] = None,
registry_authorization_token: Optional[str] = None,
):
config = config or {}
self.__dict__.update(config)
self.schema = schema
self.skip_schema_id = skip_schema_id
self.registry_urls = registry_urls
self.registry_headers = registry_headers
self.registry_proxy = registry_proxy
self.registry_timeout_secs = registry_timeout_secs
self.registry_username = registry_username
self.registry_password = registry_password
self.registry_authorization_token = registry_authorization_token
def with_schema(self, schema: str | dict) -> Self:
"""
Avro schema used to encode output records.
Specified as a string containing schema definition in JSON format.
This schema must match precisely the SQL view definition, including nullability of columns.
"""
if isinstance(schema, dict):
schema = json.dumps(schema)
self.schema = schema
return self
def with_skip_schema_id(self, skip_schema_id: bool) -> Self:
"""
Set `True` if the serialized message should only contain the data and
not contain the magic byte + schema ID. `False` by default.
The first 5 bytes of the Avro message are the magic byte and 4-byte schema ID.
https://docs.confluent.io/platform/current/schema-registry/fundamentals/serdes-develop/index.html#wire-format
"""
self.skip_schema_id = skip_schema_id
return self
def with_registry_urls(self, registry_urls: list[str]) -> Self:
"""
List of schema registry URLs.
When non-empty, the connector will post the schema to the registry and use the schema id returned
by the registry. Otherwise, schema id 0 is used.
"""
self.registry_urls = registry_urls
return self
def with_registry_headers(self, registry_headers: Mapping[str, str]) -> Self:
"""
Custom headers that will be added to every call to the schema registry.
This option requires `registry_urls` to be set.
"""
self.registry_headers = registry_headers
return self
def with_registry_proxy(self, registry_proxy: str) -> Self:
"""
Proxy that will be used to access the schema registry.
Requires `registry_urls` to be set.
"""
self.registry_proxy = registry_proxy
return self
def with_registry_timeout_secs(self, registry_timeout_secs: int) -> Self:
"""
Timeout in seconds used to connect to the registry.
Requires `registry_urls` to be set.
"""
if registry_timeout_secs < 0:
raise ValueError("registry_timeout_secs must be a positive integer")
self.registry_timeout_secs = registry_timeout_secs
return self
def with_registry_username(self, registry_username: str) -> Self:
"""
Username used to authenticate with the registry.
Requires `registry_urls` to be set. This option is mutually exclusive with
token-based authentication (see `registry_authorization_token`).
"""
if self.registry_authorization_token is not None:
raise ValueError("registry_username is mutually exclusive with registry_authorization_token")
self.registry_username = registry_username
return self
def with_registry_password(self, registry_password: str) -> Self:
"""
Password used to authenticate with the registry.
Requires `registry_urls` to be set. This option is mutually exclusive with
token-based authentication (see `registry_authorization_token`).
"""
if self.registry_authorization_token is not None:
raise ValueError("registry_password is mutually exclusive with registry_authorization_token")
self.registry_password = registry_password
return self
def with_registry_authorization_token(self, registry_authorization_token: str) -> Self:
"""
Token used to authenticate with the registry.
Requires `registry_urls` to be set. This option is mutually exclusive with
password-based authentication (see `registry_username` and `registry_password`).
"""
if self.registry_username is not None or self.registry_password is not None:
raise ValueError("registry_authorization_token is mutually exclusive with registry_username")
self.registry_authorization_token = registry_authorization_token
return self
def to_dict(self) -> dict:
"""
Serialize to a dict to be used in the API request.
:meta private:
"""
return {
"name": "avro",
"config": self.__dict__
}