|
| 1 | +import logging |
| 2 | +from pathlib import Path |
| 3 | +from time import sleep |
| 4 | +from typing import Generator, Optional, Union |
| 5 | + |
| 6 | +from quixstreams.models import Topic, TopicConfig |
| 7 | +from quixstreams.sources import Source |
| 8 | + |
| 9 | +from .compressions import CompressionName |
| 10 | +from .formats import FORMATS, Format, FormatName |
| 11 | + |
| 12 | +__all__ = ("FileSource",) |
| 13 | + |
| 14 | +logger = logging.getLogger(__name__) |
| 15 | + |
| 16 | + |
| 17 | +class FileSource(Source): |
| 18 | + """ |
| 19 | + Ingest a set of local files into kafka by iterating through the provided folder and |
| 20 | + processing all nested files within it. |
| 21 | +
|
| 22 | + Expects folder and file structures as generated by the related Quix Streams File |
| 23 | + Sink Connector: |
| 24 | +
|
| 25 | + my_topics/ |
| 26 | + ├── topic_a/ |
| 27 | + │ ├── 0/ |
| 28 | + │ │ ├── 0000.ext |
| 29 | + │ │ └── 0011.ext |
| 30 | + │ └── 1/ |
| 31 | + │ ├── 0003.ext |
| 32 | + │ └── 0016.ext |
| 33 | + └── topic_b/ |
| 34 | + └── etc... |
| 35 | +
|
| 36 | + Intended to be used with a single topic (ex: topic_a), but will recursively read |
| 37 | + from whatever entrypoint is passed to it. |
| 38 | +
|
| 39 | + File format structure depends on the file format. |
| 40 | +
|
| 41 | + See the `.formats` and `.compressions` modules to see what is supported. |
| 42 | +
|
| 43 | + Example: |
| 44 | +
|
| 45 | + from quixstreams import Application |
| 46 | + from quixstreams.sources.community.file import FileSource |
| 47 | +
|
| 48 | + app = Application(broker_address="localhost:9092", auto_offset_reset="earliest") |
| 49 | + source = FileSource( |
| 50 | + filepath="/path/to/my/topic_folder", |
| 51 | + file_format="json", |
| 52 | + file_compression="gzip", |
| 53 | + ) |
| 54 | + sdf = app.dataframe(source=source).print(metadata=True) |
| 55 | +
|
| 56 | + if __name__ == "__main__": |
| 57 | + app.run() |
| 58 | + """ |
| 59 | + |
| 60 | + def __init__( |
| 61 | + self, |
| 62 | + filepath: Union[str, Path], |
| 63 | + file_format: Union[Format, FormatName], |
| 64 | + file_compression: Optional[CompressionName] = None, |
| 65 | + as_replay: bool = True, |
| 66 | + name: Optional[str] = None, |
| 67 | + shutdown_timeout: float = 10, |
| 68 | + ): |
| 69 | + """ |
| 70 | + :param filepath: a filepath to recursively read through; it is recommended to |
| 71 | + provide the path to a given topic folder (ex: `/path/to/topic_a`). |
| 72 | + :param file_format: what format the message files are in (ex: json, parquet). |
| 73 | + Optionally, can provide a `Format` instance if more than file_compression |
| 74 | + is necessary to define (file_compression will then be ignored). |
| 75 | + :param file_compression: what compression is used on the given files, if any. |
| 76 | + :param as_replay: Produce the messages with the original time delay between them. |
| 77 | + Otherwise, produce the messages as fast as possible. |
| 78 | + NOTE: Time delay will only be accurate per partition, NOT overall. |
| 79 | + :param name: The name of the Source application (Default: last folder name). |
| 80 | + :param shutdown_timeout: Time in seconds the application waits for the source |
| 81 | + to gracefully shutdown |
| 82 | + """ |
| 83 | + self._filepath = Path(filepath) |
| 84 | + self._formatter = _get_formatter(file_format, file_compression) |
| 85 | + self._as_replay = as_replay |
| 86 | + self._previous_timestamp = None |
| 87 | + self._previous_partition = None |
| 88 | + super().__init__( |
| 89 | + name=name or self._filepath.name, shutdown_timeout=shutdown_timeout |
| 90 | + ) |
| 91 | + |
| 92 | + def _replay_delay(self, current_timestamp: int): |
| 93 | + """ |
| 94 | + Apply the replay speed by calculating the delay between messages |
| 95 | + based on their timestamps. |
| 96 | + """ |
| 97 | + if self._previous_timestamp is not None: |
| 98 | + time_diff = (current_timestamp - self._previous_timestamp) / 1000 |
| 99 | + if time_diff > 0: |
| 100 | + logger.debug(f"Sleeping for {time_diff} seconds...") |
| 101 | + sleep(time_diff) |
| 102 | + self._previous_timestamp = current_timestamp |
| 103 | + |
| 104 | + def _get_partition_count(self) -> int: |
| 105 | + return len([f for f in self._filepath.iterdir()]) |
| 106 | + |
| 107 | + def default_topic(self) -> Topic: |
| 108 | + """ |
| 109 | + Uses the file structure to generate the desired partition count for the |
| 110 | + internal topic. |
| 111 | + :return: the original default topic, with updated partition count |
| 112 | + """ |
| 113 | + topic = super().default_topic() |
| 114 | + topic.config = TopicConfig( |
| 115 | + num_partitions=self._get_partition_count(), replication_factor=1 |
| 116 | + ) |
| 117 | + return topic |
| 118 | + |
| 119 | + def _check_file_partition_number(self, file: Path): |
| 120 | + """ |
| 121 | + Checks whether the next file is the start of a new partition so the timestamp |
| 122 | + tracker can be reset. |
| 123 | + """ |
| 124 | + partition = int(file.parent.name) |
| 125 | + if self._previous_partition != partition: |
| 126 | + self._previous_timestamp = None |
| 127 | + self._previous_partition = partition |
| 128 | + logger.debug(f"Beginning reading partition {partition}") |
| 129 | + |
| 130 | + def _produce(self, record: dict): |
| 131 | + kafka_msg = self._producer_topic.serialize( |
| 132 | + key=record["_key"], |
| 133 | + value=record["_value"], |
| 134 | + timestamp_ms=record["_timestamp"], |
| 135 | + ) |
| 136 | + self.produce( |
| 137 | + key=kafka_msg.key, value=kafka_msg.value, timestamp=kafka_msg.timestamp |
| 138 | + ) |
| 139 | + |
| 140 | + def run(self): |
| 141 | + while self._running: |
| 142 | + for file in _file_finder(self._filepath): |
| 143 | + logger.info(f"Reading files from topic {self._filepath.name}") |
| 144 | + self._check_file_partition_number(file) |
| 145 | + for record in self._formatter.file_read(file): |
| 146 | + if self._as_replay: |
| 147 | + self._replay_delay(record["_timestamp"]) |
| 148 | + self._produce(record) |
| 149 | + self.flush() |
| 150 | + return |
| 151 | + |
| 152 | + |
| 153 | +def _get_formatter( |
| 154 | + formatter: Union[Format, FormatName], compression: Optional[CompressionName] |
| 155 | +) -> Format: |
| 156 | + if isinstance(formatter, Format): |
| 157 | + return formatter |
| 158 | + elif format_obj := FORMATS.get(formatter): |
| 159 | + return format_obj(compression=compression) |
| 160 | + |
| 161 | + allowed_formats = ", ".join(FormatName.__args__) |
| 162 | + raise ValueError( |
| 163 | + f'Invalid format name "{formatter}". ' |
| 164 | + f"Allowed values: {allowed_formats}, " |
| 165 | + f"or an instance of a subclass of `Format`." |
| 166 | + ) |
| 167 | + |
| 168 | + |
| 169 | +def _file_finder(filepath: Path) -> Generator[Path, None, None]: |
| 170 | + if filepath.is_dir(): |
| 171 | + for i in sorted(filepath.iterdir(), key=lambda x: x.name): |
| 172 | + yield from _file_finder(i) |
| 173 | + else: |
| 174 | + yield filepath |
0 commit comments