Created
March 9, 2018 01:45
-
-
Save sandboxws/08b9c5e373b94056733f8a662d9a2fa2 to your computer and use it in GitHub Desktop.
WIP MongoDB Apache Beam Sink for Python
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
__all__ = ['WriteToMongo'] | |
import json | |
from pymongo import MongoClient | |
from apache_beam.transforms import PTransform | |
from apache_beam.io import iobase | |
class _MongoSink(iobase.Sink): | |
"""A :class:`~apache_beam.io.iobase.Sink`.""" | |
def __init__(self, connection_string, database, collection): | |
self._connection_string = connection_string | |
self._database = database | |
self._collection = collection | |
self._client = None | |
@property | |
def client(self): | |
if self._client: | |
return self._client | |
self._client = MongoClient(self._connection_string) | |
return self._client | |
def initialize_write(self): | |
return self.client[self._database][self._collection] | |
def open_writer(self, init_result, uid): | |
return _WriteToMongo(init_result, uid) | |
def finalize_write(self, init_result, writer_results): | |
print '>>>>>>>>>>>>>>>>>>> finalize_write' | |
class _WriteToMongo(iobase.Writer): | |
"""A :class:`~apache_beam.io.iobase.Writer` for writing to MongoDB.""" | |
def __init__(self, init_result, uid): | |
self._collection = init_result | |
self._uid = uid | |
def write(self, document): | |
document = json.loads(document) | |
self._collection.insert_one(document) | |
def close(self): | |
print '>>>>>>>>>>>>>>>>>>>> close' | |
class WriteToMongo(PTransform): | |
"""A :class:`~apache_beam.transforms.PTransform` wrapper for _MongoSink.""" | |
def __init__(self, connection_string, database, collection): | |
super(WriteToMongo, self).__init__() | |
self._connection_string = connection_string | |
self._database = database | |
self._collection = collection | |
self._sink = _MongoSink( | |
self._connection_string, | |
self._database, | |
self._collection | |
) | |
def expand(self, pcoll): | |
return pcoll | iobase.Write(self._sink) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment