-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat: Replace Parquet File Writer with Gzipped Jsonl File Writer (#60)
- Loading branch information
1 parent
b0a98fe
commit 81d1b9c
Showing
11 changed files
with
253 additions
and
72 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 |
---|---|---|
@@ -1,13 +1,16 @@ | ||
from __future__ import annotations | ||
|
||
from .base import FileWriterBase, FileWriterBatchHandle, FileWriterConfigBase | ||
from .jsonl import JsonlWriter, JsonlWriterConfig | ||
from .parquet import ParquetWriter, ParquetWriterConfig | ||
|
||
|
||
__all__ = [ | ||
"FileWriterBatchHandle", | ||
"FileWriterBase", | ||
"FileWriterConfigBase", | ||
"JsonlWriter", | ||
"JsonlWriterConfig", | ||
"ParquetWriter", | ||
"ParquetWriterConfig", | ||
] |
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,68 @@ | ||
# Copyright (c) 2023 Airbyte, Inc., all rights reserved. | ||
|
||
"""A Parquet cache implementation.""" | ||
from __future__ import annotations | ||
|
||
import gzip | ||
from pathlib import Path | ||
from typing import TYPE_CHECKING, cast | ||
|
||
import orjson | ||
import ulid | ||
from overrides import overrides | ||
|
||
from airbyte._file_writers.base import ( | ||
FileWriterBase, | ||
FileWriterBatchHandle, | ||
FileWriterConfigBase, | ||
) | ||
|
||
|
||
if TYPE_CHECKING: | ||
import pyarrow as pa | ||
|
||
|
||
class JsonlWriterConfig(FileWriterConfigBase): | ||
"""Configuration for the Snowflake cache.""" | ||
|
||
# Inherits `cache_dir` from base class | ||
|
||
|
||
class JsonlWriter(FileWriterBase): | ||
"""A Jsonl cache implementation.""" | ||
|
||
config_class = JsonlWriterConfig | ||
|
||
def get_new_cache_file_path( | ||
self, | ||
stream_name: str, | ||
batch_id: str | None = None, # ULID of the batch | ||
) -> Path: | ||
"""Return a new cache file path for the given stream.""" | ||
batch_id = batch_id or str(ulid.ULID()) | ||
config: JsonlWriterConfig = cast(JsonlWriterConfig, self.config) | ||
target_dir = Path(config.cache_dir) | ||
target_dir.mkdir(parents=True, exist_ok=True) | ||
return target_dir / f"{stream_name}_{batch_id}.jsonl.gz" | ||
|
||
@overrides | ||
def _write_batch( | ||
self, | ||
stream_name: str, | ||
batch_id: str, | ||
record_batch: pa.Table, | ||
) -> FileWriterBatchHandle: | ||
"""Process a record batch. | ||
Return the path to the cache file. | ||
""" | ||
_ = batch_id # unused | ||
output_file_path = self.get_new_cache_file_path(stream_name) | ||
|
||
with gzip.open(output_file_path, "w") as jsonl_file: | ||
for record in record_batch.to_pylist(): | ||
jsonl_file.write(orjson.dumps(record) + b"\n") | ||
|
||
batch_handle = FileWriterBatchHandle() | ||
batch_handle.files.append(output_file_path) | ||
return batch_handle |
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
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
Oops, something went wrong.