Skip to content

Instantly share code, notes, and snippets.

@gghez
Created July 26, 2017 08:09
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save gghez/eabb9407ed1659d1633731d349d725d7 to your computer and use it in GitHub Desktop.
Save gghez/eabb9407ed1659d1633731d349d725d7 to your computer and use it in GitHub Desktop.
MongoDB Hook for Airflow system.
from airflow.hooks.base_hook import BaseHook
from pymongo import MongoClient
class MongoDBHook(BaseHook):
def __init__(self, conn_id='mongodb_default'):
self.conn = self.get_connection(conn_id)
self.client = MongoClient(host=self.conn.host, port=self.conn.port)
def __getattr__(self, name):
return getattr(self.client, name)
def __getitem__(self, item):
return self.client[item]
@gghez
Copy link
Author

gghez commented Jul 26, 2017

You need to create a connection in Airflow UI with some other DB type like "MySQL" because it seems MongoDB does not worth to be listed.

@openp2pdesign
Copy link

There's an open issue for this in the Airflow development, I linked this gist there. There's also another similar gist here.
I look forward to see the hook with MongoDB working soon!

@gilsonqedu
Copy link

This hook works fine?

@pathakumesh
Copy link

There's no option to add a connection for 'MongoDB' from airflow admin UI. Can anyone confirm how to setup mongo connection properly?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment