This is an automated email from the ASF dual-hosted git repository.

altay pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/beam.git


The following commit(s) were added to refs/heads/master by this push:
     new 8e989a3  [BEAM-5148] Optionally load bson (#9308)
8e989a3 is described below

commit 8e989a3822bd5819d9bac4405b7ef68b3a7ec7e2
Author: Ahmet Altay <[email protected]>
AuthorDate: Mon Aug 12 10:48:48 2019 -0700

    [BEAM-5148] Optionally load bson (#9308)
    
    * Optionally load bson
---
 sdks/python/apache_beam/io/mongodbio.py | 18 ++++++++++++++----
 1 file changed, 14 insertions(+), 4 deletions(-)

diff --git a/sdks/python/apache_beam/io/mongodbio.py 
b/sdks/python/apache_beam/io/mongodbio.py
index db3646b..00b3625 100644
--- a/sdks/python/apache_beam/io/mongodbio.py
+++ b/sdks/python/apache_beam/io/mongodbio.py
@@ -55,10 +55,6 @@ from __future__ import absolute_import
 
 import logging
 
-from bson import objectid
-from pymongo import MongoClient
-from pymongo import ReplaceOne
-
 import apache_beam as beam
 from apache_beam.io import iobase
 from apache_beam.io.range_trackers import OffsetRangeTracker
@@ -67,6 +63,20 @@ from apache_beam.transforms import PTransform
 from apache_beam.transforms import Reshuffle
 from apache_beam.utils.annotations import experimental
 
+try:
+  # Mongodb has its own bundled bson, which is not compatible with bson 
pakcage.
+  # (https://github.com/py-bson/bson/issues/82). Try to import objectid and if
+  # it fails because bson package is installed, MongoDB IO will not work but at
+  # least rest of the SDK will work.
+  from bson import objectid
+
+  # pymongo also internally depends on bson.
+  from pymongo import MongoClient
+  from pymongo import ReplaceOne
+except ImportError:
+  objectid = None
+  logging.warning("Could not find a compatible bson package.")
+
 __all__ = ['ReadFromMongoDB', 'WriteToMongoDB']
 
 

Reply via email to