tfio.experimental.mongodb.MongoDBWriter

Write documents to mongoDB.

Used in the notebooks

Used in the tutorials

The writer can be used to store documents in mongoDB while dealing with tensorflow based models and inference outputs. Without loss of generality, consider an ML model that is being used for inference. The outputs of inference can be modelled into a structured record by enriching the schema with additional information( for ex: metadata about input data and the semantics of the inference etc.) and can be stored in mongo collections for persistence or future analysis.

To make a connection and write the documents to the mongo collections, the tfio.experimental.mongodb.MongoDBWriter API can be used.

Example:

URI = "mongodb://mongoadmin:default_password@localhost:27017"
DATABASE = "tfiodb"
COLLECTION = "test"
writer = tfio.experimental.mongodb.MongoDBWriter(
    uri=URI, database=DATABASE, collection=COLLECTION
)
for i in range(1000):
   data = {"key{}".format(i): "value{}".format(i)}
   writer.write(data)

uri A string, representing the uri of the mongo server or replicaset. To connect to a MongoDB server with username and password based authentication, the following uri pattern can be used. Ex: "mongodb://mongoadmin:default_password@localhost:27017".

Connecting to a replica set is much like connecting to a standalone MongoDB server. Simply specify the replica set name using the ?replicaSet=myreplset URI option. Ex: "mongodb://host01:27017,host02:27017,host03:27017/?replicaSet=myreplset"

Connection to a secure cluster via CA certs can be achieved by setting the respective TLS options to the URI. Ex: "mongodb://host01:27017/?tls=true&sslCertificateAuthorityFile=/opt/ca.pem"

Additional information on writing uri's can be found here:

database A string, representing the database in the standalone MongoDB server or a replica set to connect to.
collection A string, representing the collection from which the documents have to be retrieved.

Methods

write

View source

Insert a single json document