|
| 1 | +from azure.eventhub import EventHubConsumerClient |
| 2 | +from dataclasses import dataclass |
| 3 | +from mage_ai.shared.config import BaseConfig |
| 4 | +from mage_ai.streaming.sources.base import BaseSource |
| 5 | +from typing import Callable, List |
| 6 | +import traceback |
| 7 | + |
| 8 | + |
| 9 | +@dataclass |
| 10 | +class AzureEventHubConfig(BaseConfig): |
| 11 | + connection_str: str |
| 12 | + eventhub_name: str |
| 13 | + consumer_group: str = '$Default' |
| 14 | + |
| 15 | + |
| 16 | +class AzureEventHubSource(BaseSource): |
| 17 | + config_class = AzureEventHubConfig |
| 18 | + |
| 19 | + def init_client(self): |
| 20 | + self.consumer_client = EventHubConsumerClient.from_connection_string( |
| 21 | + conn_str=self.config.connection_str, |
| 22 | + consumer_group=self.config.consumer_group, |
| 23 | + eventhub_name=self.config.eventhub_name, |
| 24 | + ) |
| 25 | + |
| 26 | + def read(self, handler: Callable): |
| 27 | + try: |
| 28 | + def on_event(partition_context, event): |
| 29 | + print(f'Received event from partition: {partition_context.partition_id}.') |
| 30 | + print(f'Event: {event}') |
| 31 | + |
| 32 | + handler(dict(data=event.body_as_str())) |
| 33 | + |
| 34 | + with self.consumer_client: |
| 35 | + self.consumer_client.receive( |
| 36 | + on_event=on_event, |
| 37 | + on_partition_initialize=self.on_partition_initialize, |
| 38 | + on_partition_close=self.on_partition_close, |
| 39 | + on_error=self.on_error, |
| 40 | + starting_position='-1', # '-1' is from the beginning of the partition. |
| 41 | + ) |
| 42 | + except KeyboardInterrupt: |
| 43 | + print('Stopped receiving.') |
| 44 | + |
| 45 | + def batch_read(self, handler: Callable): |
| 46 | + try: |
| 47 | + def on_event_batch(partition_context, event_batch: List): |
| 48 | + if len(event_batch) == 0: |
| 49 | + return |
| 50 | + print(f'Partition {partition_context.partition_id},' |
| 51 | + f'Received count: {len(event_batch)}') |
| 52 | + print(f'Sample event: {event_batch[0]}') |
| 53 | + |
| 54 | + # Handle events |
| 55 | + try: |
| 56 | + handler([dict(data=e.body_as_str()) for e in event_batch]) |
| 57 | + except Exception as e: |
| 58 | + traceback.print_exc() |
| 59 | + raise e |
| 60 | + |
| 61 | + partition_context.update_checkpoint() |
| 62 | + |
| 63 | + with self.consumer_client: |
| 64 | + self.consumer_client.receive_batch( |
| 65 | + on_event_batch=on_event_batch, |
| 66 | + max_batch_size=100, |
| 67 | + on_partition_initialize=self.on_partition_initialize, |
| 68 | + on_partition_close=self.on_partition_close, |
| 69 | + on_error=self.on_error, |
| 70 | + starting_position='-1', # '-1' is from the beginning of the partition. |
| 71 | + ) |
| 72 | + except KeyboardInterrupt: |
| 73 | + print('Stopped receiving.') |
| 74 | + |
| 75 | + def test_connection(self): |
| 76 | + return True |
| 77 | + |
| 78 | + def on_partition_initialize(partition_context): |
| 79 | + print(f'Partition: {partition_context.partition_id} has been initialized.') |
| 80 | + |
| 81 | + def on_partition_close(partition_context, reason): |
| 82 | + print(f'Partition: {partition_context.partition_id} has been closed, ' |
| 83 | + f'reason for closing: {reason}.') |
| 84 | + |
| 85 | + def on_error(partition_context, error): |
| 86 | + if partition_context: |
| 87 | + print(f'An exception: {partition_context.partition_id} occurred during' |
| 88 | + f' receiving from Partition: {error}.') |
| 89 | + else: |
| 90 | + print(f'An exception: {error} occurred during the load balance process.') |
0 commit comments