ingestio 0.0.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ingestio/__init__.py +32 -0
- ingestio/basics.py +18 -0
- ingestio/callback/__init__.py +1 -0
- ingestio/callback/all.py +12 -0
- ingestio/callback/core.py +49 -0
- ingestio/callback/io.py +42 -0
- ingestio/connector/__init__.py +1 -0
- ingestio/connector/all.py +7 -0
- ingestio/connector/core.py +24 -0
- ingestio/connector/http.py +55 -0
- ingestio/connector/local.py +48 -0
- ingestio/core.py +125 -0
- ingestio/py.typed +0 -0
- ingestio-0.0.1.dist-info/METADATA +163 -0
- ingestio-0.0.1.dist-info/RECORD +17 -0
- ingestio-0.0.1.dist-info/WHEEL +4 -0
- ingestio-0.0.1.dist-info/licenses/LICENSE +201 -0
ingestio/__init__.py
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
"""Small, composable library for building data ingestion pipelines."""
|
|
2
|
+
|
|
3
|
+
import logging
|
|
4
|
+
|
|
5
|
+
from ingestio.basics import (
|
|
6
|
+
Callback,
|
|
7
|
+
CancelProcessException,
|
|
8
|
+
CancelStepException,
|
|
9
|
+
Process,
|
|
10
|
+
Reader,
|
|
11
|
+
ReadSource,
|
|
12
|
+
Step,
|
|
13
|
+
Writer,
|
|
14
|
+
WriteTarget,
|
|
15
|
+
)
|
|
16
|
+
|
|
17
|
+
__version__ = "0.0.1"
|
|
18
|
+
|
|
19
|
+
logging.getLogger(__name__).addHandler(logging.NullHandler())
|
|
20
|
+
|
|
21
|
+
__all__ = [
|
|
22
|
+
"Callback",
|
|
23
|
+
"CancelProcessException",
|
|
24
|
+
"CancelStepException",
|
|
25
|
+
"Process",
|
|
26
|
+
"ReadSource",
|
|
27
|
+
"Reader",
|
|
28
|
+
"Step",
|
|
29
|
+
"WriteTarget",
|
|
30
|
+
"Writer",
|
|
31
|
+
"__version__",
|
|
32
|
+
]
|
ingestio/basics.py
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
"""Import surface for the core."""
|
|
2
|
+
|
|
3
|
+
from ingestio.callback.core import Callback, CancelProcessException, CancelStepException
|
|
4
|
+
from ingestio.callback.io import ReadSource, WriteTarget
|
|
5
|
+
from ingestio.connector.core import Reader, Writer
|
|
6
|
+
from ingestio.core import Process, Step
|
|
7
|
+
|
|
8
|
+
__all__ = [
|
|
9
|
+
"Callback",
|
|
10
|
+
"CancelProcessException",
|
|
11
|
+
"CancelStepException",
|
|
12
|
+
"Process",
|
|
13
|
+
"ReadSource",
|
|
14
|
+
"Reader",
|
|
15
|
+
"Step",
|
|
16
|
+
"WriteTarget",
|
|
17
|
+
"Writer",
|
|
18
|
+
]
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Callbacks: the code of a process stage."""
|
ingestio/callback/all.py
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"""Import surface for the callback subpackage."""
|
|
2
|
+
|
|
3
|
+
from ingestio.callback.core import Callback, CancelProcessException, CancelStepException
|
|
4
|
+
from ingestio.callback.io import ReadSource, WriteTarget
|
|
5
|
+
|
|
6
|
+
__all__ = [
|
|
7
|
+
"Callback",
|
|
8
|
+
"CancelProcessException",
|
|
9
|
+
"CancelStepException",
|
|
10
|
+
"ReadSource",
|
|
11
|
+
"WriteTarget",
|
|
12
|
+
]
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""`Callback` base class and the cancel exceptions."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import TYPE_CHECKING
|
|
6
|
+
|
|
7
|
+
if TYPE_CHECKING:
|
|
8
|
+
from ingestio.core import Process
|
|
9
|
+
|
|
10
|
+
__all__ = ["Callback", "CancelProcessException", "CancelStepException"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class CancelStepException(Exception):
|
|
14
|
+
"""Skip the remaining callbacks of the current step and move on to the next step."""
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class CancelProcessException(Exception):
|
|
18
|
+
"""Stop the process cleanly, without treating the run as a crash."""
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Callback:
|
|
22
|
+
"""One stage of a process.
|
|
23
|
+
|
|
24
|
+
A step runs its callbacks in three passes: every `before`, then every `run`, then every
|
|
25
|
+
`after`. `after` runs even when the step or the process was cancelled.
|
|
26
|
+
|
|
27
|
+
Callbacks exchange data through `self.process.state` rather than through arguments and
|
|
28
|
+
return values.
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
#: Set by `Process` when the callback is attached. Unbound before that.
|
|
32
|
+
process: Process
|
|
33
|
+
|
|
34
|
+
def before(self) -> None:
|
|
35
|
+
"""Run before any callback of this step does its work."""
|
|
36
|
+
|
|
37
|
+
def run(self) -> None:
|
|
38
|
+
"""Do the work of this stage."""
|
|
39
|
+
|
|
40
|
+
def after(self) -> None:
|
|
41
|
+
"""Run after every callback of this step, including when the step was cancelled."""
|
|
42
|
+
|
|
43
|
+
@property
|
|
44
|
+
def name(self) -> str:
|
|
45
|
+
"""Display name used in logs."""
|
|
46
|
+
return type(self).__name__
|
|
47
|
+
|
|
48
|
+
def __repr__(self) -> str:
|
|
49
|
+
return self.name
|
ingestio/callback/io.py
ADDED
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"""Callbacks that move data between a connector and the shared state."""
|
|
2
|
+
|
|
3
|
+
from __future__ import annotations
|
|
4
|
+
|
|
5
|
+
from typing import Any
|
|
6
|
+
|
|
7
|
+
from ingestio.callback.core import Callback
|
|
8
|
+
from ingestio.connector.core import Reader, Writer
|
|
9
|
+
|
|
10
|
+
__all__ = ["ReadSource", "WriteTarget"]
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ReadSource(Callback):
|
|
14
|
+
"""Reads from a connector and stores the payload in the shared state.
|
|
15
|
+
|
|
16
|
+
Args:
|
|
17
|
+
reader: Connector to read from.
|
|
18
|
+
into: Key of `Process.state` that receives the payload.
|
|
19
|
+
"""
|
|
20
|
+
|
|
21
|
+
def __init__(self, reader: Reader[Any], *, into: str = "records") -> None:
|
|
22
|
+
self.reader = reader
|
|
23
|
+
self.into = into
|
|
24
|
+
|
|
25
|
+
def run(self) -> None:
|
|
26
|
+
self.process.state[self.into] = self.reader.read()
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class WriteTarget(Callback):
|
|
30
|
+
"""Writes a value from the shared state through a connector.
|
|
31
|
+
|
|
32
|
+
Args:
|
|
33
|
+
writer: Connector to write to.
|
|
34
|
+
key: Key of `Process.state` holding the value to write.
|
|
35
|
+
"""
|
|
36
|
+
|
|
37
|
+
def __init__(self, writer: Writer[Any], *, key: str = "records") -> None:
|
|
38
|
+
self.writer = writer
|
|
39
|
+
self.key = key
|
|
40
|
+
|
|
41
|
+
def run(self) -> None:
|
|
42
|
+
self.writer.write(self.process.state[self.key])
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
"""Connectors: the I/O resources a callback uses."""
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
"""Import surface for the connector subpackage."""
|
|
2
|
+
|
|
3
|
+
from ingestio.connector.core import Reader, Writer
|
|
4
|
+
from ingestio.connector.http import JsonApiReader
|
|
5
|
+
from ingestio.connector.local import JsonLinesWriter
|
|
6
|
+
|
|
7
|
+
__all__ = ["JsonApiReader", "JsonLinesWriter", "Reader", "Writer"]
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"""`Reader` and `Writer`: the two kinds of connector.
|
|
2
|
+
|
|
3
|
+
A connector is injected into the callback that needs it, so a single callback may combine
|
|
4
|
+
several. The payload type is unconstrained.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from typing import Protocol, TypeVar
|
|
8
|
+
|
|
9
|
+
__all__ = ["Reader", "Writer"]
|
|
10
|
+
|
|
11
|
+
T_co = TypeVar("T_co", covariant=True)
|
|
12
|
+
T_contra = TypeVar("T_contra", contravariant=True)
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class Reader(Protocol[T_co]):
|
|
16
|
+
"""Reads data from a source."""
|
|
17
|
+
|
|
18
|
+
def read(self) -> T_co: ...
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class Writer(Protocol[T_contra]):
|
|
22
|
+
"""Writes data to a destination."""
|
|
23
|
+
|
|
24
|
+
def write(self, data: T_contra) -> None: ...
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"""Reader for public HTTP APIs.
|
|
2
|
+
|
|
3
|
+
Built on `urllib` so the package keeps no runtime dependencies. Endpoints requiring
|
|
4
|
+
authentication are not supported.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
import urllib.request
|
|
11
|
+
from typing import Any
|
|
12
|
+
from urllib.parse import urlparse
|
|
13
|
+
|
|
14
|
+
__all__ = ["JsonApiReader"]
|
|
15
|
+
|
|
16
|
+
_ALLOWED_SCHEMES = frozenset({"http", "https"})
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
class JsonApiReader:
|
|
20
|
+
"""Fetches a JSON endpoint and returns the decoded payload.
|
|
21
|
+
|
|
22
|
+
Args:
|
|
23
|
+
url: Endpoint to read from. Only `http` and `https` are accepted.
|
|
24
|
+
timeout: Seconds to wait before giving up.
|
|
25
|
+
headers: Extra request headers. `Accept: application/json` is sent by default.
|
|
26
|
+
encoding: Fallback used when the response declares no charset.
|
|
27
|
+
"""
|
|
28
|
+
|
|
29
|
+
def __init__(
|
|
30
|
+
self,
|
|
31
|
+
url: str,
|
|
32
|
+
*,
|
|
33
|
+
timeout: float = 30.0,
|
|
34
|
+
headers: dict[str, str] | None = None,
|
|
35
|
+
encoding: str = "utf-8",
|
|
36
|
+
) -> None:
|
|
37
|
+
scheme = urlparse(url).scheme.lower()
|
|
38
|
+
if scheme not in _ALLOWED_SCHEMES:
|
|
39
|
+
raise ValueError(
|
|
40
|
+
f"unsupported URL scheme {scheme!r}: expected one of {sorted(_ALLOWED_SCHEMES)}"
|
|
41
|
+
)
|
|
42
|
+
self.url = url
|
|
43
|
+
self.timeout = timeout
|
|
44
|
+
self.headers = {"Accept": "application/json", **(headers or {})}
|
|
45
|
+
self.encoding = encoding
|
|
46
|
+
|
|
47
|
+
def read(self) -> Any:
|
|
48
|
+
"""Perform the request and decode the JSON body."""
|
|
49
|
+
request = urllib.request.Request(self.url, headers=self.headers)
|
|
50
|
+
with urllib.request.urlopen(request, timeout=self.timeout) as response:
|
|
51
|
+
charset = response.headers.get_content_charset() or self.encoding
|
|
52
|
+
return json.loads(response.read().decode(charset))
|
|
53
|
+
|
|
54
|
+
def __repr__(self) -> str:
|
|
55
|
+
return f"{type(self).__name__}(url={self.url!r})"
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Writer for local volumes.
|
|
2
|
+
|
|
3
|
+
A Databricks volume is reachable as an ordinary path, so the same writer serves a local
|
|
4
|
+
disk and a cluster.
|
|
5
|
+
"""
|
|
6
|
+
|
|
7
|
+
from __future__ import annotations
|
|
8
|
+
|
|
9
|
+
import json
|
|
10
|
+
from collections.abc import Iterable, Mapping
|
|
11
|
+
from pathlib import Path
|
|
12
|
+
from typing import Any
|
|
13
|
+
|
|
14
|
+
__all__ = ["JsonLinesWriter"]
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
class JsonLinesWriter:
|
|
18
|
+
"""Writes records as newline-delimited JSON, one object per line.
|
|
19
|
+
|
|
20
|
+
Args:
|
|
21
|
+
path: Destination file. Parent directories are created when missing.
|
|
22
|
+
append: Append to the file instead of replacing it.
|
|
23
|
+
encoding: Text encoding of the output file.
|
|
24
|
+
"""
|
|
25
|
+
|
|
26
|
+
def __init__(
|
|
27
|
+
self,
|
|
28
|
+
path: Path | str,
|
|
29
|
+
*,
|
|
30
|
+
append: bool = False,
|
|
31
|
+
encoding: str = "utf-8",
|
|
32
|
+
) -> None:
|
|
33
|
+
self.path = Path(path)
|
|
34
|
+
self.append = append
|
|
35
|
+
self.encoding = encoding
|
|
36
|
+
|
|
37
|
+
def write(self, data: Iterable[Mapping[str, Any]]) -> None:
|
|
38
|
+
"""Write every record as one JSON line."""
|
|
39
|
+
self.path.parent.mkdir(parents=True, exist_ok=True)
|
|
40
|
+
mode = "a" if self.append else "w"
|
|
41
|
+
with self.path.open(mode, encoding=self.encoding) as handle:
|
|
42
|
+
for record in data:
|
|
43
|
+
# default=str keeps datetimes and similar values writable.
|
|
44
|
+
handle.write(json.dumps(record, ensure_ascii=False, default=str))
|
|
45
|
+
handle.write("\n")
|
|
46
|
+
|
|
47
|
+
def __repr__(self) -> str:
|
|
48
|
+
return f"{type(self).__name__}(path={str(self.path)!r})"
|
ingestio/core.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
"""`Process` and `Step`: the orchestrator and the points of a process.
|
|
2
|
+
|
|
3
|
+
`Process` owns the shared state that callbacks read and write, the ordered list of steps,
|
|
4
|
+
and the run loop. `Step` names a point of the process and holds the callbacks that run
|
|
5
|
+
there.
|
|
6
|
+
|
|
7
|
+
Steps run in list order, and callbacks within a step run in list order.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import logging
|
|
13
|
+
import time
|
|
14
|
+
import uuid
|
|
15
|
+
from collections.abc import Iterable, Mapping
|
|
16
|
+
from dataclasses import dataclass, field
|
|
17
|
+
from datetime import UTC, datetime
|
|
18
|
+
from typing import Any
|
|
19
|
+
|
|
20
|
+
from ingestio.callback.core import Callback, CancelProcessException, CancelStepException
|
|
21
|
+
|
|
22
|
+
__all__ = ["Process", "Step"]
|
|
23
|
+
|
|
24
|
+
logger = logging.getLogger(__name__)
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
@dataclass(frozen=True)
|
|
28
|
+
class Step:
|
|
29
|
+
"""A named point of a process, holding the callbacks that run there."""
|
|
30
|
+
|
|
31
|
+
name: str
|
|
32
|
+
callbacks: tuple[Callback, ...] = field(default=())
|
|
33
|
+
|
|
34
|
+
def __init__(self, name: str, callbacks: Iterable[Callback] = ()) -> None:
|
|
35
|
+
if not name.strip():
|
|
36
|
+
raise ValueError("step name must be a non-empty string")
|
|
37
|
+
object.__setattr__(self, "name", name)
|
|
38
|
+
object.__setattr__(self, "callbacks", tuple(callbacks))
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class Process:
|
|
42
|
+
"""Runs an ordered list of steps, carrying the state every callback shares.
|
|
43
|
+
|
|
44
|
+
Args:
|
|
45
|
+
steps: Steps to run, in order.
|
|
46
|
+
state: Initial shared state.
|
|
47
|
+
run_id: Identifier for this run. Generated when omitted.
|
|
48
|
+
log: Logger to write to.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def __init__(
|
|
52
|
+
self,
|
|
53
|
+
steps: Iterable[Step],
|
|
54
|
+
*,
|
|
55
|
+
state: Mapping[str, Any] | None = None,
|
|
56
|
+
run_id: str | None = None,
|
|
57
|
+
log: logging.Logger | None = None,
|
|
58
|
+
) -> None:
|
|
59
|
+
self.steps = self._validated(steps)
|
|
60
|
+
self.state: dict[str, Any] = dict(state or {})
|
|
61
|
+
self.run_id = run_id or uuid.uuid4().hex[:8]
|
|
62
|
+
self.started_at: datetime | None = None
|
|
63
|
+
self.cancelled = False
|
|
64
|
+
self._log = log or logger
|
|
65
|
+
for step in self.steps:
|
|
66
|
+
for callback in step.callbacks:
|
|
67
|
+
callback.process = self
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def _validated(steps: Iterable[Step]) -> tuple[Step, ...]:
|
|
71
|
+
"""Reject an unusable process before anything runs."""
|
|
72
|
+
ordered = tuple(steps)
|
|
73
|
+
if not ordered:
|
|
74
|
+
raise ValueError("a process needs at least one step")
|
|
75
|
+
seen: set[str] = set()
|
|
76
|
+
for step in ordered:
|
|
77
|
+
if step.name in seen:
|
|
78
|
+
raise ValueError(f"duplicate step name: {step.name!r}")
|
|
79
|
+
seen.add(step.name)
|
|
80
|
+
return ordered
|
|
81
|
+
|
|
82
|
+
def run(self) -> Process:
|
|
83
|
+
"""Run every step in order. Returns self, so the state stays reachable."""
|
|
84
|
+
self.started_at = datetime.now(UTC)
|
|
85
|
+
self.cancelled = False
|
|
86
|
+
started = time.perf_counter()
|
|
87
|
+
self._log.info("[%s] process started | %d steps", self.run_id, len(self.steps))
|
|
88
|
+
|
|
89
|
+
try:
|
|
90
|
+
for position, step in enumerate(self.steps, start=1):
|
|
91
|
+
self._run_step(step, position)
|
|
92
|
+
except CancelProcessException as exc:
|
|
93
|
+
self.cancelled = True
|
|
94
|
+
self._log.info("[%s] process cancelled: %s", self.run_id, exc)
|
|
95
|
+
|
|
96
|
+
self._log.info("[%s] process finished | %.3fs", self.run_id, time.perf_counter() - started)
|
|
97
|
+
return self
|
|
98
|
+
|
|
99
|
+
def _run_step(self, step: Step, position: int) -> None:
|
|
100
|
+
"""Run the step in three passes: every `before`, every `run`, then every `after`.
|
|
101
|
+
|
|
102
|
+
`after` runs whether the step completed, was cancelled, or the process is stopping.
|
|
103
|
+
"""
|
|
104
|
+
started = time.perf_counter()
|
|
105
|
+
self._log.info(
|
|
106
|
+
"[%s] > %s (%d/%d) | %d callbacks",
|
|
107
|
+
self.run_id,
|
|
108
|
+
step.name,
|
|
109
|
+
position,
|
|
110
|
+
len(self.steps),
|
|
111
|
+
len(step.callbacks),
|
|
112
|
+
)
|
|
113
|
+
try:
|
|
114
|
+
for callback in step.callbacks:
|
|
115
|
+
callback.before()
|
|
116
|
+
for callback in step.callbacks:
|
|
117
|
+
callback.run()
|
|
118
|
+
except CancelStepException as exc:
|
|
119
|
+
self._log.info("[%s] %s cancelled: %s", self.run_id, step.name, exc)
|
|
120
|
+
finally:
|
|
121
|
+
for callback in step.callbacks:
|
|
122
|
+
callback.after()
|
|
123
|
+
self._log.info(
|
|
124
|
+
"[%s] < %s | %.3fs", self.run_id, step.name, time.perf_counter() - started
|
|
125
|
+
)
|
ingestio/py.typed
ADDED
|
File without changes
|
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
Metadata-Version: 2.5
|
|
2
|
+
Name: ingestio
|
|
3
|
+
Version: 0.0.1
|
|
4
|
+
Summary: Small, composable library for building data ingestion pipelines
|
|
5
|
+
Project-URL: Repository, https://github.com/gustavoueti/ingestio
|
|
6
|
+
Author-email: Gustavo Ueti <gustavo.ueti@gmail.com>
|
|
7
|
+
License-Expression: Apache-2.0
|
|
8
|
+
License-File: LICENSE
|
|
9
|
+
Classifier: Development Status :: 3 - Alpha
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
12
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
13
|
+
Classifier: Topic :: Database
|
|
14
|
+
Classifier: Typing :: Typed
|
|
15
|
+
Requires-Python: >=3.11
|
|
16
|
+
Description-Content-Type: text/markdown
|
|
17
|
+
|
|
18
|
+
# ingestio
|
|
19
|
+
|
|
20
|
+
Small, composable library for building data ingestion pipelines.
|
|
21
|
+
|
|
22
|
+
`ingestio` describes **any data movement process** as a sequence of steps. It does not
|
|
23
|
+
assume your data is a table, that your architecture has bronze and silver layers, or that
|
|
24
|
+
your processing is batch. Those are things you build with it, not things it imposes.
|
|
25
|
+
|
|
26
|
+
> **Status: early alpha (0.0.1).** The core is functional and tested, the API is not stable
|
|
27
|
+
> yet.
|
|
28
|
+
|
|
29
|
+
## Install
|
|
30
|
+
|
|
31
|
+
```bash
|
|
32
|
+
pip install ingestio
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
No runtime dependencies.
|
|
36
|
+
|
|
37
|
+
## The idea
|
|
38
|
+
|
|
39
|
+
Three pieces, and the ordering is always yours:
|
|
40
|
+
|
|
41
|
+
| | |
|
|
42
|
+
|---|---|
|
|
43
|
+
| **Step** | a named point of the process; holds the callbacks that run there |
|
|
44
|
+
| **Callback** | the code of one stage |
|
|
45
|
+
| **Connector** | a reader or a writer, injected into the callback that needs it |
|
|
46
|
+
|
|
47
|
+
Steps run in list order, and callbacks inside a step run in list order. There is no
|
|
48
|
+
priority attribute and no hidden reordering: the sequence you write is the sequence that
|
|
49
|
+
runs.
|
|
50
|
+
|
|
51
|
+
The library ships the engine, one reader, one writer and two generic callbacks. The stages
|
|
52
|
+
that are specific to your ingestion are yours to write — that is the point.
|
|
53
|
+
|
|
54
|
+
## Example: a public API into a volume
|
|
55
|
+
|
|
56
|
+
```python
|
|
57
|
+
from ingestio import Process, ReadSource, Step, WriteTarget
|
|
58
|
+
from ingestio.connector.all import JsonApiReader, JsonLinesWriter
|
|
59
|
+
|
|
60
|
+
process = Process([
|
|
61
|
+
Step("extract", [
|
|
62
|
+
ReadSource(JsonApiReader("https://api.example.com/v1/orders")),
|
|
63
|
+
]),
|
|
64
|
+
Step("load", [
|
|
65
|
+
WriteTarget(JsonLinesWriter("/Volumes/main/bronze/orders.jsonl")),
|
|
66
|
+
]),
|
|
67
|
+
]).run()
|
|
68
|
+
|
|
69
|
+
print(len(process.state["records"]))
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
A Databricks volume is an ordinary path, so the same code runs on a laptop and on a
|
|
73
|
+
cluster.
|
|
74
|
+
|
|
75
|
+
## Writing a callback
|
|
76
|
+
|
|
77
|
+
A callback is a class with three optional moments. A step runs every `before`, then every
|
|
78
|
+
`run`, then every `after`, so a callback gets a real boundary around the whole step while
|
|
79
|
+
keeping list order within each pass.
|
|
80
|
+
|
|
81
|
+
Callbacks exchange data through `process.state` instead of passing a payload down a chain,
|
|
82
|
+
so a callback that only observes does not have to hand the data back.
|
|
83
|
+
|
|
84
|
+
```python
|
|
85
|
+
from datetime import UTC, datetime
|
|
86
|
+
|
|
87
|
+
from ingestio import Callback
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
class AddLineageColumns(Callback):
|
|
91
|
+
"""Stamp every record with where it came from and which run produced it."""
|
|
92
|
+
|
|
93
|
+
def __init__(self, source: str, key: str = "records") -> None:
|
|
94
|
+
self.source = source
|
|
95
|
+
self.key = key
|
|
96
|
+
|
|
97
|
+
def run(self) -> None:
|
|
98
|
+
ingested_at = datetime.now(UTC).isoformat()
|
|
99
|
+
self.process.state[self.key] = [
|
|
100
|
+
{
|
|
101
|
+
**record,
|
|
102
|
+
"_ingested_at": ingested_at,
|
|
103
|
+
"_source": self.source,
|
|
104
|
+
"_batch_id": self.process.run_id,
|
|
105
|
+
}
|
|
106
|
+
for record in self.process.state[self.key]
|
|
107
|
+
]
|
|
108
|
+
```
|
|
109
|
+
|
|
110
|
+
Drop it into the step where it belongs:
|
|
111
|
+
|
|
112
|
+
```python
|
|
113
|
+
Step("load", [
|
|
114
|
+
AddLineageColumns(source="orders-api"),
|
|
115
|
+
WriteTarget(JsonLinesWriter("/Volumes/main/bronze/orders.jsonl")),
|
|
116
|
+
])
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The column names are yours. `ingestio` has no opinion about them.
|
|
120
|
+
|
|
121
|
+
## Controlling failure
|
|
122
|
+
|
|
123
|
+
The library gives you the vocabulary; the policy is yours.
|
|
124
|
+
|
|
125
|
+
```python
|
|
126
|
+
from ingestio import Callback, CancelProcessException
|
|
127
|
+
|
|
128
|
+
|
|
129
|
+
class SkipWhenEmpty(Callback):
|
|
130
|
+
def run(self) -> None:
|
|
131
|
+
if not self.process.state["records"]:
|
|
132
|
+
raise CancelProcessException("nothing to ingest")
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
- `CancelStepException` — skip the rest of this step, carry on with the next one.
|
|
136
|
+
- `CancelProcessException` — stop the process cleanly.
|
|
137
|
+
- anything else — propagates and kills the run, which is a legitimate choice you make by
|
|
138
|
+
not catching it.
|
|
139
|
+
|
|
140
|
+
`after` runs in every case, so cleanup stays reliable.
|
|
141
|
+
|
|
142
|
+
## Logging
|
|
143
|
+
|
|
144
|
+
Standard `logging`, never `print`, and every record carries the run id — so concurrent
|
|
145
|
+
ingestions on the same cluster stay readable.
|
|
146
|
+
|
|
147
|
+
```
|
|
148
|
+
[a3f7c9] process started | 2 steps
|
|
149
|
+
[a3f7c9] > extract (1/2) | 1 callbacks
|
|
150
|
+
[a3f7c9] < extract | 0.412s
|
|
151
|
+
[a3f7c9] > load (2/2) | 2 callbacks
|
|
152
|
+
[a3f7c9] < load | 0.019s
|
|
153
|
+
[a3f7c9] process finished | 0.431s
|
|
154
|
+
```
|
|
155
|
+
|
|
156
|
+
## Roadmap
|
|
157
|
+
|
|
158
|
+
More connectors (object storage, Delta), and recipes — an optional declarative way to
|
|
159
|
+
assemble a process, never a required one.
|
|
160
|
+
|
|
161
|
+
## License
|
|
162
|
+
|
|
163
|
+
Apache-2.0
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
ingestio/__init__.py,sha256=w5tvE3Ogyeqb1vjSg2HoL_8qmJV2FTVXoA3e3yNuO_s,552
|
|
2
|
+
ingestio/basics.py,sha256=Gfp48B8xeex3YJT-Y9LtobjTdLJyAxDOsyK459rE0SA,453
|
|
3
|
+
ingestio/core.py,sha256=TbS4tBiLKwxO001v6jDPyztQjMVttGMyCPjZIL2i25U,4304
|
|
4
|
+
ingestio/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
5
|
+
ingestio/callback/__init__.py,sha256=R8291Um0WXPZjPossbq2pwqVSoavkFZVddJhZk96VL0,46
|
|
6
|
+
ingestio/callback/all.py,sha256=5qSIiFWn7Yi2Nhvtp683L528Rg1mxpd48c1W0-epwMI,322
|
|
7
|
+
ingestio/callback/core.py,sha256=PV0wT_-ocvrPG7wXjOst09l5xWwPxXF_-KkGgCiB0AU,1397
|
|
8
|
+
ingestio/callback/io.py,sha256=co0FU36P185aSh0pj2z6gUm6ygvrNcbv7Ypdj6xFT_E,1151
|
|
9
|
+
ingestio/connector/__init__.py,sha256=jogih0oBwnfJ65ouIpVODyPg3aV2MCCbi0odeoYFmyw,53
|
|
10
|
+
ingestio/connector/all.py,sha256=4YXn9Z6YeoX0S4lGxJbXxwlsqb6M5uV2jtl70wrINgM,274
|
|
11
|
+
ingestio/connector/core.py,sha256=Yi0OHqeOtyrdHT39sMLabswR7MY19IjVKD9MZ7dA0lw,581
|
|
12
|
+
ingestio/connector/http.py,sha256=9fN2I6nA60GtvW2qCl3bRwqVTKlgvIDj7ytgippMXjM,1800
|
|
13
|
+
ingestio/connector/local.py,sha256=n4u3opO01qPW-TRq9DhyWtEthlv3Ech7faHA45efX6w,1469
|
|
14
|
+
ingestio-0.0.1.dist-info/METADATA,sha256=KQl_UIu0cnWvhMTC91ocOMxmUa1kgBspVMfGYm6Ox50,4803
|
|
15
|
+
ingestio-0.0.1.dist-info/WHEEL,sha256=zOwg4jB6zX2kU910N-cMawjivD6tO8NEWvE12je1bVk,87
|
|
16
|
+
ingestio-0.0.1.dist-info/licenses/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
|
|
17
|
+
ingestio-0.0.1.dist-info/RECORD,,
|
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|