tfio.experimental.mongodb.MongoDBWriter
Stay organized with collections
Save and categorize content based on your preferences.
Write documents to mongoDB.
tfio.experimental.mongodb.MongoDBWriter(
uri, database, collection
)
Used in the notebooks
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)
Args |
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
write(
doc
)
Insert a single json document
Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.
Last updated 2022-02-15 UTC.
[null,null,["Last updated 2022-02-15 UTC."],[],[],null,["# tfio.experimental.mongodb.MongoDBWriter\n\n\u003cbr /\u003e\n\n|----------------------------------------------------------------------------------------------------------------------------------------|\n| [View source on GitHub](https://github.com/tensorflow/io/blob/v0.24.0/tensorflow_io/python/experimental/mongodb_writer_ops.py#L24-L98) |\n\nWrite documents to mongoDB. \n\n tfio.experimental.mongodb.MongoDBWriter(\n uri, database, collection\n )\n\n### Used in the notebooks\n\n| Used in the tutorials |\n|---------------------------------------------------------------------------------------------------|\n| - [Tensorflow datasets from MongoDB collections](https://www.tensorflow.org/io/tutorials/mongodb) |\n\nThe writer can be used to store documents in mongoDB while dealing with tensorflow\nbased models and inference outputs. Without loss of generality, consider an ML\nmodel that is being used for inference. The outputs of inference can be modelled into\na structured record by enriching the schema with additional information( for ex: metadata\nabout input data and the semantics of the inference etc.) and can be stored in mongo\ncollections for persistence or future analysis.\n\nTo make a connection and write the documents to the mongo collections,\nthe [`tfio.experimental.mongodb.MongoDBWriter`](../../../tfio/experimental/mongodb/MongoDBWriter) API can be used.\n\n#### Example:\n\n URI = \"mongodb://mongoadmin:default_password@localhost:27017\"\n DATABASE = \"tfiodb\"\n COLLECTION = \"test\"\n writer = tfio.experimental.mongodb.MongoDBWriter(\n uri=URI, database=DATABASE, collection=COLLECTION\n )\n for i in range(1000):\n data = {\"key{}\".format(i): \"value{}\".format(i)}\n writer.write(data)\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n\u003cbr /\u003e\n\n| Args ---- ||\n|--------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `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\"`. \u003cbr /\u003e 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: - [libmongoc uri docs](http://mongoc.org/libmongoc/current/mongoc_uri_t.html) - [mongodb uri docs](https://docs.mongodb.com/manual/reference/connection-string/) |\n| `database` | A string, representing the database in the standalone MongoDB server or a replica set to connect to. |\n| `collection` | A string, representing the collection from which the documents have to be retrieved. |\n\nMethods\n-------\n\n### `write`\n\n[View source](https://github.com/tensorflow/io/blob/v0.24.0/tensorflow_io/python/experimental/mongodb_writer_ops.py#L86-L91) \n\n write(\n doc\n )\n\nInsert a single json document"]]