forked from blockchain-etl/ethereum-etl
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
67 additions
and
3 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,46 @@ | ||
import collections | ||
import json | ||
import logging | ||
|
||
from kafka import KafkaProducer | ||
|
||
from blockchainetl.jobs.exporters.converters.composite_item_converter import CompositeItemConverter | ||
|
||
|
||
class KafkaItemExporter: | ||
|
||
def __init__(self, connection_url, item_type_to_topic_mapping, converters=()): | ||
self.connection_url = connection_url | ||
self.item_type_to_topic_mapping = item_type_to_topic_mapping | ||
self.converter = CompositeItemConverter(converters) | ||
self.producer = KafkaProducer(bootstrap_servers=connection_url) | ||
|
||
def open(self): | ||
pass | ||
|
||
def export_items(self, items): | ||
for item in items: | ||
self.export_item(item) | ||
|
||
def export_item(self, item): | ||
item_type = item.get('type') | ||
if item_type is not None and item_type in self.item_type_to_topic_mapping: | ||
data = json.dumps(item).encode('utf-8') | ||
return self.producer.send(self.item_type_to_topic_mapping[item_type], value=data) | ||
else: | ||
logging.warning('Topic for item type "{}" is not configured.'.format(item_type)) | ||
|
||
def convert_items(self, items): | ||
for item in items: | ||
yield self.converter.convert_item(item) | ||
|
||
def close(self): | ||
pass | ||
|
||
|
||
def group_by_item_type(items): | ||
result = collections.defaultdict(list) | ||
for item in items: | ||
result[item.get('type')].append(item) | ||
|
||
return result |
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