|
| 1 | +.. _kafka-docker-example: |
| 2 | + |
| 3 | +====================================== |
| 4 | +MongoDB Kafka Connector Docker Example |
| 5 | +====================================== |
| 6 | + |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +.. contents:: On this page |
| 10 | + :local: |
| 11 | + :backlinks: none |
| 12 | + :depth: 1 |
| 13 | + :class: singlecols |
| 14 | + |
| 15 | +This guide provides an end-to-end setup of MongoDB and Kafka Connect to |
| 16 | +demonstrate the functionality of the MongoDB Kafka Source and Sink |
| 17 | +Connectors. |
| 18 | + |
| 19 | +In this example, we create the following Kafka Connectors: |
| 20 | + |
| 21 | +.. list-table:: |
| 22 | + :header-rows: 1 |
| 23 | + |
| 24 | + * - Connector |
| 25 | + - Data Source |
| 26 | + - Destination |
| 27 | + |
| 28 | + * - Confluent Connector: |
| 29 | + `Datagen <https://github.com/confluentinc/kafka-connect-datagen>`_ |
| 30 | + - `Avro random generator |
| 31 | + <https://github.com/confluentinc/avro-random-generator>`_ |
| 32 | + - Kafka topic: pageviews |
| 33 | + |
| 34 | + * - Sink Connector: **mongo-sink** |
| 35 | + - Kafka topic: ``pageviews`` |
| 36 | + - MongoDB collection: ``test.pageviews`` |
| 37 | + |
| 38 | + * - Source Connector: **mongo-source** |
| 39 | + - MongoDB collection: ``test.pageviews`` |
| 40 | + - Kafka topic: ``mongo.test.pageviews`` |
| 41 | + |
| 42 | +* The **Datagen Connector** creates random data using the |
| 43 | + **Avro random generator** and publishes it to the Kafka topic "pageviews". |
| 44 | + |
| 45 | +* The **mongo-sink** connector reads data from the "pageviews" topic and |
| 46 | + writes it to MongoDB in the "test.pageviews" collection. |
| 47 | + |
| 48 | +* The **mongo-source** connector produces change events for the |
| 49 | + "test.pageviews" collection and publishes them to the |
| 50 | + "mongo.test.pageviews" collection. |
| 51 | + |
| 52 | +Requirements |
| 53 | +------------ |
| 54 | + |
| 55 | +Linux/Unix-based OS |
| 56 | +~~~~~~~~~~~~~~~~~~~ |
| 57 | +* `Docker <https://docs.docker.com/install/#supported-platforms>`_ 18.09 or later |
| 58 | +* `Docker Compose <https://docs.docker.com/compose/install/>`_ 1.24 or later |
| 59 | + |
| 60 | +MacOS |
| 61 | +~~~~~ |
| 62 | + |
| 63 | +* `Docker Desktop Community Edition (Mac) |
| 64 | + <https://docs.docker.com/docker-for-mac/install/>`_ 2.1.0.1 or later |
| 65 | + |
| 66 | +Windows |
| 67 | +~~~~~~~ |
| 68 | + |
| 69 | +* `Docker Desktop Community Edition (Windows) |
| 70 | + <https://docs.docker.com/docker-for-windows/install/>`_ 2.1.0.1 or later |
| 71 | + |
| 72 | +How to Run the Example |
| 73 | +---------------------- |
| 74 | + |
| 75 | +Clone the `mongo-kafka <https://github.com/mongodb/mongo-kafka>`_ repository |
| 76 | +from GitHub: |
| 77 | + |
| 78 | +.. code-block:: shell |
| 79 | + |
| 80 | + git clone https://github.com/mongodb/mongo-kafka.git |
| 81 | + |
| 82 | +Change directory to the ``docker`` directory |
| 83 | + |
| 84 | +.. code-block:: shell |
| 85 | + |
| 86 | + cd mongo-kafka/docker/ |
| 87 | + |
| 88 | +Start the shell script, **run.sh**: |
| 89 | + |
| 90 | +.. code-block:: shell |
| 91 | + |
| 92 | + ./run.sh |
| 93 | + |
| 94 | +The shell script executes the following sequence of commands: |
| 95 | + |
| 96 | +#. Run the ``docker-compose up`` command |
| 97 | + |
| 98 | + The ``docker-compose`` command installs and starts the following |
| 99 | + applications in a new docker container: |
| 100 | + |
| 101 | + * Zookeeper |
| 102 | + * Kafka |
| 103 | + * Confluent Schema Registry |
| 104 | + * Confluent Kafka Connect |
| 105 | + * Confluent Control Center |
| 106 | + * Confluent KSQL Server |
| 107 | + * Kafka Rest Proxy |
| 108 | + * Kafka Topics UI |
| 109 | + * MongoDB replica set (three nodes: **mongo1**, **mongo2**, and |
| 110 | + **mongo3**) |
| 111 | + |
| 112 | +#. Wait for MongoDB, Kafka, Kafka Connect to become ready |
| 113 | +#. Register the Confluent Datagen Connector |
| 114 | +#. Register the MongoDB Kafka Sink Connector |
| 115 | +#. Register the MongoDB Kafka Source Connector |
| 116 | + |
| 117 | +.. note:: |
| 118 | + |
| 119 | + You may need to increase the RAM resource limits for Docker if the script |
| 120 | + fails. Use the `docker-compose stop <docker-compose-stop>` command to |
| 121 | + stop any running instances of docker if the script did not complete |
| 122 | + successfully. |
| 123 | + |
| 124 | +Once the services have been started by the shell script, the Datagen Connector |
| 125 | +publishes new events to Kafka at short intervals which triggers the |
| 126 | +following cycle: |
| 127 | + |
| 128 | +#. The Datagen Connector publishes new events to Kafka |
| 129 | +#. The Sink Connector writes the events into MongoDB |
| 130 | +#. The Source Connector writes the change stream messages back into Kafka |
| 131 | + |
| 132 | +To view the Kafka topics, open the Kafka Control Center at |
| 133 | +http://localhost:9021/ and navigate to the cluster topics. |
| 134 | + |
| 135 | +* The ``pageviews`` topic should contain documents added by the Datagen |
| 136 | + Connector that resemble the following: |
| 137 | + |
| 138 | + .. code-block:: json |
| 139 | + |
| 140 | + { |
| 141 | + "viewtime": { |
| 142 | + "$numberLong": "81" |
| 143 | + }, |
| 144 | + "pageid": "Page_1", |
| 145 | + "userid": "User_8" |
| 146 | + } |
| 147 | + |
| 148 | +* The ``mongo.test.pageviews`` topic should contain change events that |
| 149 | + resemble the following: |
| 150 | + |
| 151 | + .. code-block:: json |
| 152 | + |
| 153 | + { |
| 154 | + "_id": { |
| 155 | + "_data": "<resumeToken>" |
| 156 | + }, |
| 157 | + "operationType": "insert", |
| 158 | + "clusterTime": { |
| 159 | + "$timestamp": { |
| 160 | + "t": 1563461814, |
| 161 | + "i": 4 |
| 162 | + } |
| 163 | + }, |
| 164 | + "fullDocument": { |
| 165 | + "_id": { |
| 166 | + "$oid": "5d3088b6bafa7829964150f3" |
| 167 | + }, |
| 168 | + "viewtime": { |
| 169 | + "$numberLong": "81" |
| 170 | + }, |
| 171 | + "pageid": "Page_1", |
| 172 | + "userid": "User_8" |
| 173 | + }, |
| 174 | + "ns": { |
| 175 | + "db": "test", |
| 176 | + "coll": "pageviews" |
| 177 | + }, |
| 178 | + "documentKey": { |
| 179 | + "_id": { |
| 180 | + "$oid": "5d3088b6bafa7829964150f3" |
| 181 | + } |
| 182 | + } |
| 183 | + } |
| 184 | + |
| 185 | +Next, explore the collection data in the MongoDB replica set: |
| 186 | + |
| 187 | +* In your local shell, navigate to the ``docker`` directory from which you |
| 188 | + ran the ``docker-compose`` commands and connect to the `mongo1` MongoDB |
| 189 | + instance using the following command: |
| 190 | + |
| 191 | + .. code-block:: shell |
| 192 | + |
| 193 | + docker-compose exec mongo1 /usr/bin/mongo |
| 194 | + |
| 195 | +* If you insert or update a document in the ``test.pageviews``, the Source |
| 196 | + Connector publishes a change event document to the |
| 197 | + ``mongo.test.pageviews`` Kafka topic. |
| 198 | + |
| 199 | +.. _docker-compose-stop: |
| 200 | + |
| 201 | +To stop the docker containers and all the processes running on them, use |
| 202 | +Ctrl-C in the shell running the script, or the following command: |
| 203 | + |
| 204 | +.. code-block:: shell |
| 205 | + |
| 206 | + docker-compose stop |
| 207 | + |
| 208 | +To remove the docker containers and images completely, use the following |
| 209 | +command: |
| 210 | + |
| 211 | +.. code-block:: shell |
| 212 | + |
| 213 | + docker-compose down |
0 commit comments