-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
MongoDB CDC: Add support for MongoDB Change Streams to
ctk load table
- Loading branch information
Showing
9 changed files
with
398 additions
and
10 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
""" | ||
Basic relaying of a MongoDB Change Stream into CrateDB table. | ||
Documentation: | ||
- https://github.com/daq-tools/commons-codec/blob/main/doc/mongodb.md | ||
- https://www.mongodb.com/docs/manual/changeStreams/ | ||
- https://www.mongodb.com/developer/languages/python/python-change-streams/ | ||
""" | ||
|
||
import logging | ||
|
||
import pymongo | ||
import sqlalchemy as sa | ||
from commons_codec.transform.mongodb import MongoDBCDCTranslatorCrateDB | ||
|
||
from cratedb_toolkit.util import DatabaseAdapter | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class MongoDBCDCRelayCrateDB: | ||
""" | ||
Relay MongoDB Change Stream into CrateDB table. | ||
""" | ||
|
||
def __init__( | ||
self, | ||
mongodb_url: str, | ||
mongodb_database: str, | ||
mongodb_collection: str, | ||
cratedb_sqlalchemy_url: str, | ||
cratedb_table: str, | ||
): | ||
self.cratedb_adapter = DatabaseAdapter(cratedb_sqlalchemy_url, echo=True) | ||
self.mongodb_client: pymongo.MongoClient = pymongo.MongoClient(mongodb_url) | ||
self.mongodb_collection = self.mongodb_client[mongodb_database][mongodb_collection] | ||
self.table_name = self.cratedb_adapter.quote_relation_name(cratedb_table) | ||
self.cdc = MongoDBCDCTranslatorCrateDB(table_name=self.table_name) | ||
|
||
def start(self): | ||
""" | ||
Subscribe to change stream events, convert to SQL, and submit to CrateDB. | ||
""" | ||
with self.cratedb_adapter.engine.connect() as connection: | ||
connection.execute(sa.text(self.cdc.sql_ddl)) | ||
for sql in self.cdc_to_sql(): | ||
if sql: | ||
connection.execute(sa.text(sql)) | ||
connection.execute(sa.text(f"REFRESH TABLE {self.table_name};")) | ||
|
||
def cdc_to_sql(self): | ||
""" | ||
Subscribe to change stream events, and emit corresponding SQL statements. | ||
""" | ||
# Note that `.watch()` will block until events are ready for consumption, so | ||
# this is not a busy loop. Also note that the routine doesn't perform any sensible | ||
# error handling yet. | ||
while True: | ||
with self.mongodb_collection.watch(full_document="updateLookup") as change_stream: | ||
for change in change_stream: | ||
logger.info("MongoDB Change Stream event: %s" % change) | ||
yield self.cdc.to_sql(change) |
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
Oops, something went wrong.