|
| 1 | +# MIT License |
| 2 | +# |
| 3 | +# Copyright (c) 2022 CoinStats LLC |
| 4 | +# |
| 5 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 6 | +# of this software and associated documentation files (the "Software"), to deal |
| 7 | +# in the Software without restriction, including without limitation the rights |
| 8 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the Software is |
| 10 | +# furnished to do so, subject to the following conditions: |
| 11 | +# |
| 12 | +# The above copyright notice and this permission notice shall be included in all |
| 13 | +# copies or substantial portions of the Software. |
| 14 | +# |
| 15 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 20 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 21 | +# SOFTWARE. |
| 22 | + |
| 23 | +import json |
| 24 | +import typing as t |
| 25 | +import uuid |
| 26 | +from itertools import zip_longest |
| 27 | + |
| 28 | +import boto3 |
| 29 | + |
| 30 | +_KINESIS_BATCH_LIMIT = 500 |
| 31 | + |
| 32 | + |
| 33 | +def _uuid_partition_key(_: dict) -> str: |
| 34 | + return uuid.uuid4().hex |
| 35 | + |
| 36 | + |
| 37 | +class KinesisItemExporter: |
| 38 | + def __init__( |
| 39 | + self, |
| 40 | + stream_name: str, |
| 41 | + partition_key_callable: t.Callable[[dict], str] = _uuid_partition_key, |
| 42 | + ): |
| 43 | + import boto3 |
| 44 | + self._stream_name = stream_name |
| 45 | + self._partition_key_callable = partition_key_callable |
| 46 | + self._kinesis_client = None # initialized in .open |
| 47 | + |
| 48 | + def open(self) -> None: |
| 49 | + self._kinesis_client = boto3.client('kinesis') |
| 50 | + |
| 51 | + def export_items(self, items: t.Iterable[dict]) -> None: |
| 52 | + sentinel = object() |
| 53 | + chunks = zip_longest( |
| 54 | + *(iter(items),) * _KINESIS_BATCH_LIMIT, |
| 55 | + fillvalue=sentinel, |
| 56 | + ) |
| 57 | + for chunk in chunks: |
| 58 | + self._kinesis_client.put_records( |
| 59 | + StreamName=self._stream_name, |
| 60 | + Records=[ |
| 61 | + { |
| 62 | + 'Data': _serialize_item(item), |
| 63 | + 'PartitionKey': self._partition_key_callable(item), |
| 64 | + } |
| 65 | + for item in chunk |
| 66 | + if item is not sentinel |
| 67 | + ], |
| 68 | + ) |
| 69 | + |
| 70 | + def export_item(self, item: dict) -> None: |
| 71 | + self._kinesis_client.put_record( |
| 72 | + StreamName=self._stream_name, |
| 73 | + Data=_serialize_item(item), |
| 74 | + PartitionKey=self._partition_key_callable(item), |
| 75 | + ) |
| 76 | + |
| 77 | + def close(self): |
| 78 | + pass |
| 79 | + |
| 80 | + |
| 81 | +def _serialize_item(item: dict) -> bytes: |
| 82 | + return json.dumps(item).encode() |
0 commit comments