-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: break out default batch file writer into separate class (#1668
- Loading branch information
Ken Payne
authored
May 5, 2023
1 parent
2974649
commit e029e30
Showing
6 changed files
with
153 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
singer_sdk.batch.BaseBatcher | ||
============================ | ||
|
||
.. currentmodule:: singer_sdk.batch | ||
|
||
.. autoclass:: BaseBatcher | ||
:members: | ||
:special-members: __init__, __call__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
singer_sdk.batch.JSONLinesBatcher | ||
================================= | ||
|
||
.. currentmodule:: singer_sdk.batch | ||
|
||
.. autoclass:: JSONLinesBatcher | ||
:members: | ||
:special-members: __init__, __call__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
"""Batching utilities for Singer SDK.""" | ||
from __future__ import annotations | ||
|
||
import gzip | ||
import itertools | ||
import json | ||
import typing as t | ||
from abc import ABC, abstractmethod | ||
from uuid import uuid4 | ||
|
||
if t.TYPE_CHECKING: | ||
from singer_sdk.helpers._batch import BatchConfig | ||
|
||
_T = t.TypeVar("_T") | ||
|
||
|
||
def lazy_chunked_generator( | ||
iterable: t.Iterable[_T], | ||
chunk_size: int, | ||
) -> t.Generator[t.Iterator[_T], None, None]: | ||
"""Yield a generator for each chunk of the given iterable. | ||
Args: | ||
iterable: The iterable to chunk. | ||
chunk_size: The size of each chunk. | ||
Yields: | ||
A generator for each chunk of the given iterable. | ||
""" | ||
iterator = iter(iterable) | ||
while True: | ||
chunk = list(itertools.islice(iterator, chunk_size)) | ||
if not chunk: | ||
break | ||
yield iter(chunk) | ||
|
||
|
||
class BaseBatcher(ABC): | ||
"""Base Record Batcher.""" | ||
|
||
def __init__( | ||
self, | ||
tap_name: str, | ||
stream_name: str, | ||
batch_config: BatchConfig, | ||
) -> None: | ||
"""Initialize the batcher. | ||
Args: | ||
tap_name: The name of the tap. | ||
stream_name: The name of the stream. | ||
batch_config: The batch configuration. | ||
""" | ||
self.tap_name = tap_name | ||
self.stream_name = stream_name | ||
self.batch_config = batch_config | ||
|
||
@abstractmethod | ||
def get_batches( | ||
self, | ||
records: t.Iterator[dict], | ||
) -> t.Iterator[list[str]]: | ||
"""Yield manifest of batches. | ||
Args: | ||
records: The records to batch. | ||
Raises: | ||
NotImplementedError: If the method is not implemented. | ||
""" | ||
raise NotImplementedError | ||
|
||
|
||
class JSONLinesBatcher(BaseBatcher): | ||
"""JSON Lines Record Batcher.""" | ||
|
||
def get_batches( | ||
self, | ||
records: t.Iterator[dict], | ||
) -> t.Iterator[list[str]]: | ||
"""Yield manifest of batches. | ||
Args: | ||
records: The records to batch. | ||
Yields: | ||
A list of file paths (called a manifest). | ||
""" | ||
sync_id = f"{self.tap_name}--{self.stream_name}-{uuid4()}" | ||
prefix = self.batch_config.storage.prefix or "" | ||
|
||
for i, chunk in enumerate( | ||
lazy_chunked_generator( | ||
records, | ||
self.batch_config.batch_size, | ||
), | ||
start=1, | ||
): | ||
filename = f"{prefix}{sync_id}-{i}.json.gz" | ||
with self.batch_config.storage.fs() as fs: | ||
# TODO: Determine compression from config. | ||
with fs.open(filename, "wb") as f, gzip.GzipFile( | ||
fileobj=f, | ||
mode="wb", | ||
) as gz: | ||
gz.writelines( | ||
(json.dumps(record) + "\n").encode() for record in chunk | ||
) | ||
file_url = fs.geturl(filename) | ||
yield [file_url] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters