forked from feldera/feldera
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfeldera_client.py
More file actions
513 lines (410 loc) · 16.5 KB
/
feldera_client.py
File metadata and controls
513 lines (410 loc) · 16.5 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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
from typing import Optional
import logging
import time
import json
from feldera.rest.config import Config
from feldera.rest.connector import Connector
from feldera.rest.attached_connector import AttachedConnector
from feldera.rest.program import Program
from feldera.rest.pipeline import Pipeline
from feldera.rest._httprequests import HttpRequests
def _prepare_boolean_input(value: bool) -> str:
return "true" if value else "false"
class FelderaClient:
"""
A client for the Feldera HTTP API
A client instance is needed for every Feldera API method to know the location of
Feldera and its permissions.
"""
def __init__(
self,
url: str,
api_key: Optional[str] = None,
timeout: Optional[int] = None,
) -> None:
"""
:param url: The url to Feldera API (ex: https://try.feldera.com)
:param api_key: The optional API key for Feldera
:param timeout: (optional) The amount of time in seconds that the client will wait for a response before timing
out.
"""
self.config = Config(url, api_key, timeout)
self.http = HttpRequests(self.config)
try:
self.programs()
except Exception as e:
logging.error(f"Failed to connect to Feldera API: {e}")
raise e
def programs(self) -> list[Program]:
"""
Get all programs
"""
resp = self.http.get(
path="/programs",
)
return [
Program(
name=program.get("name"),
code=program.get("code"),
description=program.get("description"),
) for program in resp
]
def get_program(self, name: str, with_code: bool = False) -> Program:
"""
Get a program by name
:param name: The name of the program
:param with_code: If True, the program code will be included in the response
"""
resp = self.http.get(
path=f"/programs/{name}",
params={
"with_code": with_code,
}
)
return Program(
name=resp.get("name"),
code=resp.get("code"),
description=resp.get("description"),
status=resp.get("status"),
version=resp.get("version"),
)
def compile_program(self, program: Program, config: Optional[dict] = None):
"""
Compiles a SQL program
:param program: The program to compile
:param config: Optional. The configuration for the compilation
"""
body = {
"code": program.code,
"description": program.description or "",
"config": config or {"profile": "optimized"}
}
resp = self.http.put(
path=f"/programs/{program.name}",
body=body,
)
program.id = resp.get("program_id")
program.version = resp.get("version")
while True:
status = self.get_program(program.name).status
if status == "Success":
break
elif status != "Pending" and status != "CompilingRust" and status != "CompilingSql":
# TODO: return a more detailed error message, make this a custom error
raise RuntimeError(f"Failed program compilation with status {status}")
logging.debug("still compiling %s, waiting for 5 more seconds", program.name)
time.sleep(5)
def delete_program(self, name: str):
"""
Deletes a program by name
:param name: The name of the program
"""
self.http.delete(
path=f"/programs/{name}",
)
def connectors(self) -> list[Connector]:
"""
Get all connectors
"""
resp = self.http.get(
path="/connectors",
)
return [
Connector(
name=connector.get("name"),
description=connector.get("description"),
config=connector.get("config"),
id=connector.get("connector_id"),
) for connector in resp
]
def get_connector(self, name: str) -> Connector:
"""
Get a connector by name
:param name: The name of the connector
"""
resp = self.http.get(
path=f"/connectors/{name}",
)
return Connector(
name=resp.get("name"),
description=resp.get("description"),
config=resp.get("config"),
id=resp.get("connector_id"),
)
def create_connector(self, connector: Connector):
"""
Create a connector.
Doesn't return anything, but sets the id of the connector.
:param connector: The connector to create
"""
body = {
"description": connector.description or "",
"config": connector.config
}
resp = self.http.put(
path=f"/connectors/{connector.name}",
body=body,
)
connector.id = resp.get("connector_id")
def delete_connector(self, name: str):
"""
Delete a connector by name
:param name: The name of the connector
"""
self.http.delete(
path=f"/connectors/{name}",
)
@staticmethod
def __pipeline_from_dict(pipeline: dict) -> Pipeline:
attached_connectors = pipeline.get("attached_connectors")
descriptor = pipeline.get("descriptor")
return Pipeline(
name=descriptor.get("name"),
program_name=descriptor.get("program_name"),
version=descriptor.get("version"),
description=descriptor.get("description"),
id=descriptor.get("id"),
config=descriptor.get("config"),
state=pipeline.get("state"),
attached_connectors=[
AttachedConnector(
connector_name=con.get("connector_name"),
is_input=con.get("is_input"),
relation_name=con.get("relation_name"),
name=con.get("name"),
)
for con in attached_connectors
] if attached_connectors else []
)
def pipelines(self) -> list[Pipeline]:
"""
Get all pipelines
"""
resp = self.http.get(
path="/pipelines",
)
return [self.__pipeline_from_dict(pipeline) for pipeline in resp]
def get_pipeline(self, name: str) -> Pipeline:
"""
Get a pipeline by name
:param name: The name of the pipeline
"""
resp = self.http.get(
path=f"/pipelines/{name}",
)
return self.__pipeline_from_dict(resp)
def create_pipeline(self, pipeline: Pipeline):
"""
Create a pipeline
:param pipeline: The pipeline to create
"""
body = {
"config": pipeline.config,
"description": pipeline.description or "",
"connectors": [c.to_json() for c in pipeline.attached_connectors],
"program_name": pipeline.program_name,
}
resp = self.http.put(
path=f"/pipelines/{pipeline.name}",
body=body,
)
pipeline.id = resp.get("pipeline_id")
def get_pipeline_config(self, name: str) -> dict:
"""
Get the configuration of a pipeline by name
"""
resp = self.http.get(
path=f"/pipelines/{name}/config",
)
return resp
def validate_pipeline(self, name: str) -> bool:
"""
Validate a pipeline.
Checks whether the pipeline is configured correctly.
This includes checking whether the pipeline references a valid compiled program,
whether the connectors reference valid tables/views in the program, and more.
:param name: The name of the pipeline
"""
resp = self.http.get(
path=f"/pipelines/{name}/validate",
)
# TODO: return an error description if invalid
return "success" in resp
def delete_pipeline(self, name: str):
"""
Deletes a pipeline by name
:param name: The name of the pipeline
"""
resp = self.http.delete(
path=f"/pipelines/{name}",
)
def get_pipeline_stats(self, name: str) -> dict:
"""
Get the pipeline metrics and performance counters
:param name: The name of the pipeline
"""
resp = self.http.get(
path=f"/pipelines/{name}/stats",
)
return resp
def start_pipeline(self, pipeline_name: str):
"""
Start a pipeline
:param pipeline_name: The name of the pipeline to start
"""
self.http.post(
path=f"/pipelines/{pipeline_name}/start",
)
while True:
status = self.get_pipeline(pipeline_name).state.get("current_status")
if status == "Running":
break
elif status == "Failed":
# TODO: return a more detailed error message
raise RuntimeError(f"Failed to start pipeline")
logging.debug("still starting %s, waiting for 100 more milliseconds", pipeline_name)
time.sleep(0.1)
def pause_pipeline(self, pipeline_name: str):
"""
Stop a pipeline
:param pipeline_name: The name of the pipeline to stop
"""
self.http.post(
path=f"/pipelines/{pipeline_name}/pause",
)
while True:
status = self.get_pipeline(pipeline_name).state.get("current_status")
if status == "Paused":
break
elif status == "Failed":
# TODO: return a more detailed error message
raise RuntimeError(f"Failed to pause pipeline")
logging.debug("still pausing %s, waiting for 100 more milliseconds", pipeline_name)
time.sleep(0.1)
def shutdown_pipeline(self, pipeline_name: str):
"""
Shutdown a pipeline
:param pipeline_name: The name of the pipeline to shutdown
"""
self.http.post(
path=f"/pipelines/{pipeline_name}/shutdown",
)
while True:
status = self.get_pipeline(pipeline_name).state.get("current_status")
if status == "Shutdown":
break
elif status == "Failed":
raise RuntimeError(f"Failed to shutdown pipeline")
logging.debug("still shutting down %s, waiting for 100 more milliseconds", pipeline_name)
time.sleep(0.1)
# TODO: better name for this method
def push_to_pipeline(
self,
pipeline_name: str,
table_name: str,
format: str,
data: list[list | str | dict],
array: bool = False,
force: bool = False,
update_format: str = "raw",
json_flavor: str = None,
serialize: bool = True,
):
"""
Insert data into a pipeline
:param pipeline_name: The name of the pipeline
:param table_name: The name of the table
:param format: The format of the data, either "json" or "csv"
:param array: True if updates in this stream are packed into JSON arrays, used in conjunction with the
"json" format
:param force: If True, the data will be inserted even if the pipeline is paused
:param update_format: JSON data change event format, used in conjunction with the "json" format,
the default value is "insert_delete", other supported formats: "weighted", "debezium", "snowflake", "raw"
:param json_flavor: JSON encoding used for individual table records, the default value is "default", other supported encodings:
"debezium_mysql", "snowflake", "kafka_connect_json_converter", "pandas"
:param data: The data to insert
:param serialize: If True, the data will be serialized to JSON. True by default
"""
if format not in ["json", "csv"]:
raise ValueError("format must be either 'json' or 'csv'")
if update_format not in ["insert_delete", "weighted", "debezium", "snowflake", "raw"]:
raise ValueError("update_format must be one of 'insert_delete', 'weighted', 'debezium', 'snowflake', 'raw'")
if json_flavor is not None and json_flavor not in ["default", "debezium_mysql", "snowflake", "kafka_connect_json_converter", "pandas"]:
raise ValueError("json_flavor must be one of 'default', 'debezium_mysql', 'snowflake', 'kafka_connect_json_converter', 'pandas'")
# python sends `True` which isn't accepted by the backend
array = _prepare_boolean_input(array)
force = _prepare_boolean_input(force)
params = {
"force": force,
"format": format,
}
if format == "json":
params["array"] = array
params["update_format"] = update_format
if json_flavor is not None:
params["json_flavor"] = json_flavor
content_type = "application/json"
if format == "csv":
content_type = "text/csv"
data = bytes(str(data), "utf-8")
self.http.post(
path=f"/pipelines/{pipeline_name}/ingress/{table_name}",
params=params,
content_type=content_type,
body=data,
serialize=serialize,
)
def listen_to_pipeline(
self,
pipeline_name: str,
table_name: str,
format: str,
mode: str = "watch",
backpressure: bool = True,
query: Optional[str] = None,
quantiles: Optional[int] = None,
array: bool = False,
timeout: Optional[float] = None,
):
"""
Listen for updates to views for pipeline, yields the chunks of data
:param pipeline_name: The name of the pipeline
:param table_name: The name of the table to listen to
:param format: The format of the data, either "json" or "csv"
:param mode: The mode to listen in, either "watch" or "snapshot"
:param backpressure: When the flag is True (the default), this method waits for the consumer to receive each
chunk and blocks the pipeline if the consumer cannot keep up. When this flag is False, the pipeline drops
data chunks if the consumer is not keeping up with its output. This prevents a slow consumer from slowing
down the entire pipeline.
:param quantiles: For 'quantiles' queries: the number of quantiles to output. The default value is 100
:param query: Query to execute on the table, either "table", "neighborhood" or "quantiles"
:param array: Set True to group updates in this stream into JSON arrays, used in conjunction with the
"json" format, the default value is False
:param timeout: The amount of time in seconds to listen to the stream for
"""
if mode not in ["watch", "snapshot"]:
raise ValueError("mode must be either 'watch' or 'snapshot'")
if query is not None and query not in ["table", "neighborhood", "quantiles"]:
raise ValueError("query must be either 'table', 'neighborhood' or 'quantiles'")
params = {
"mode": mode,
"format": format,
"backpressure": _prepare_boolean_input(backpressure),
}
if quantiles:
params["quantiles"] = quantiles
if format == "json":
params["array"] = _prepare_boolean_input(array)
resp = self.http.post(
path=f"/pipelines/{pipeline_name}/egress/{table_name}",
params=params,
stream=True,
)
end = time.time() + timeout if timeout else None
# Using the default chunk size below makes `iter_lines` extremely
# inefficient when dealing with long lines.
for chunk in resp.iter_lines(chunk_size=50000000):
if end and time.time() > end:
break
if chunk:
yield json.loads(chunk)