|
2 | 2 | Quick Start
|
3 | 3 | ===========
|
4 | 4 |
|
5 |
| -asdf |
| 5 | +.. default-domain:: mongodb |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +Overview |
| 14 | +-------- |
| 15 | + |
| 16 | +In this guide, you can learn how to configure the MongoDB Kafka Connector to |
| 17 | +perform the following actions within an Apache Kafka and Kafka Connect data pipeline: |
| 18 | + |
| 19 | +#. :ref:`Receive documents inserted into a MongoDB collection <quick-start-source-connector>` |
| 20 | +#. :ref:`Add a field to those documents using an aggregation pipeline <quick-start-source-connector>` |
| 21 | +#. :ref:`Insert those documents into a different collection in the same MongoDB |
| 22 | + cluster <quick-start-sink-connector>` |
| 23 | + |
| 24 | +For more information on Apache Kafka and Kafka Connect, read our |
| 25 | +introductory page on :doc:`Kafka and Kafka Connect <introduction/kafka-connect>`. |
| 26 | + |
| 27 | +Requirements |
| 28 | +------------ |
| 29 | + |
| 30 | +This guide uses the following tools: |
| 31 | + |
| 32 | +- `Docker Platform <https://docs.docker.com/get-docker/>`__ *required* |
| 33 | +- `Git <https://git-scm.com/book/en/v2/Getting-Started-Installing-Git>`__ *optional* |
| 34 | + |
| 35 | +If you do not have any of these tools installed on your computer, you |
| 36 | +can install a tool by clicking on the tool's name and following the linked |
| 37 | +installation instructions. |
| 38 | + |
| 39 | +.. tip:: Read the Docker Documentation |
| 40 | + |
| 41 | + This guide uses some Docker specific terminology. If you are new to Docker |
| 42 | + and would like a comprehensive introduction, read through Docker's official |
| 43 | + `Get Started Guide <https://docs.docker.com/get-started/>`__. |
| 44 | + |
| 45 | +Sample Pipeline |
| 46 | +~~~~~~~~~~~~~~~ |
| 47 | + |
| 48 | +In this guide you receive a sample data pipeline so that you can use the MongoDB |
| 49 | +Kafka Connector. To access and use the files that define your sample data pipeline, |
| 50 | +clone the quick start repository and change your directory to the root of the |
| 51 | +repository using the following commands: |
| 52 | + |
| 53 | +<TODO: decide on final location for this example> |
| 54 | + |
| 55 | +.. code-block:: bash |
| 56 | + |
| 57 | + git clone https://github.com/biniona-mongodb/MongoKafkaLite |
| 58 | + cd MongoKafkaLite |
| 59 | + |
| 60 | +.. note:: Download as a ZIP |
| 61 | + |
| 62 | + If you would rather download the repository that defines the pipeline as a |
| 63 | + ZIP file, you can do that by |
| 64 | + `clicking this link <https://github.com/biniona-mongodb/MongoKafkaLite/archive/refs/heads/main.zip>`__. |
| 65 | + |
| 66 | +Start the Pipeline |
| 67 | +------------------ |
| 68 | + |
| 69 | +The sample pipeline consists of the following tools running in Docker containers |
| 70 | +on your computer: |
| 71 | + |
| 72 | +- A MongoDB replica set |
| 73 | +- An Apache Kafka instance |
| 74 | +- A Kafka Connect instance with the MongoDB Kafka Connector installed |
| 75 | +- A Zookeeper instance (Zookeeper is a dependency of Apache Kafka) |
| 76 | + |
| 77 | +The following diagram shows the architecture of the sample pipeline. The solid |
| 78 | +lines represent connections between tools that you receive pre-configured. The |
| 79 | +dotted lines represent connections you will add in the following sections of |
| 80 | +this guide. The numbers, from lowest to highest, show the path of a message |
| 81 | +through the pipeline: |
| 82 | + |
| 83 | +.. figure:: /includes/figures/mongo-kafka-connect.png |
| 84 | + :alt: Architecture diagram of sample pipeline. |
| 85 | + |
| 86 | +To download and run the sample pipeline, execute the following command from |
| 87 | +the root of the quick start repository: |
| 88 | + |
| 89 | +.. code-block:: bash |
| 90 | + |
| 91 | + docker-compose -p quickstart up -d |
| 92 | + |
| 93 | +.. note:: How long does the download take? |
| 94 | + |
| 95 | + In total, the Docker images for the quick start require about 2.4 GB of space. |
| 96 | + The following list shows how long it takes to download the images with |
| 97 | + different internet speeds: |
| 98 | + |
| 99 | + - 40 megabits per second: 8 minutes |
| 100 | + - 20 megabits per second: 16 minutes |
| 101 | + - 10 megabits per second: 32 minutes |
| 102 | + |
| 103 | +Set Up Connectors |
| 104 | +----------------- |
| 105 | + |
| 106 | +To set up connectors in the sample pipeline, you first need to enter a shell |
| 107 | +in your Docker environment using the following command: |
| 108 | + |
| 109 | +.. code-block:: bash |
| 110 | + |
| 111 | + docker exec -it shell /bin/bash |
| 112 | + |
| 113 | +Once you are in the shell you should see a prompt that looks like this: |
| 114 | + |
| 115 | +.. code-block:: none |
| 116 | + :copyable: false |
| 117 | + |
| 118 | + [MongoDB Kafka Connector Quick Start] |
| 119 | + |
| 120 | +Install the Source Connector |
| 121 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 122 | + |
| 123 | +In the quick start shell, run the following command to set up |
| 124 | +the source connector using the Kafka Connect REST API: |
| 125 | + |
| 126 | +.. _quick-start-source-connector: |
| 127 | + |
| 128 | +.. code-block:: bash |
| 129 | + |
| 130 | + curl -X POST \ |
| 131 | + -H "Content-Type: application/json" \ |
| 132 | + --data ' |
| 133 | + {"name": "mongo-source", |
| 134 | + "config": { |
| 135 | + "connector.class":"com.mongodb.kafka.connect.MongoSourceConnector", |
| 136 | + "connection.uri":"mongodb://mongo1:27017/?replicaSet=rs0", |
| 137 | + "database":"quickstart", |
| 138 | + "collection":"source", |
| 139 | + "pipeline":"[{\"$match\": {\"operationType\": \"insert\"}}, {$addFields : {\"fullDocument.travel\":\"I went through Kafka!\"}}]" |
| 140 | + } |
| 141 | + } |
| 142 | + ' \ |
| 143 | + http://connect:8083/connectors -w "\n" |
| 144 | + |
| 145 | +.. note:: Failed To Connect |
| 146 | + |
| 147 | + It takes about two minutes for the Kafka Connect REST API to come online. If |
| 148 | + you receive the following error, wait about two minutes and try again: |
| 149 | + |
| 150 | + .. code-block:: none |
| 151 | + :copyable: false |
| 152 | + |
| 153 | + ... |
| 154 | + curl: (7) Failed to connect to connect port 8083: Connection refused |
| 155 | + |
| 156 | +The preceding command creates a new connector in Kafka Connect and specifies the |
| 157 | +following properties of the connector: |
| 158 | + |
| 159 | +- The class Kafka Connect uses to instantiate the connector |
| 160 | +- The URI, database, and collection of the MongoDB instance the |
| 161 | + connector receives data from |
| 162 | +- An aggregation pipeline that adds a field ``travel`` with the value |
| 163 | + ``"I went through Kafka!"`` to inserted documents passing through the connector |
| 164 | + |
| 165 | +To confirm that you successfully added the source connector, run the following |
| 166 | +command to print all connectors installed on your Kafka Connect deployment: |
| 167 | + |
| 168 | +.. code-block:: bash |
| 169 | + |
| 170 | + curl -X GET http://connect:8083/connectors |
| 171 | + |
| 172 | +You should see the following output: |
| 173 | + |
| 174 | +.. code-block:: none |
| 175 | + :copyable: false |
| 176 | + |
| 177 | + ["mongo-source"] |
| 178 | + |
| 179 | +For more information on source connectors, see our |
| 180 | +:doc:`guide on source connectors <source-connector>`. |
| 181 | + |
| 182 | +For more information on aggregation pipelines, see the |
| 183 | +:manual:`MongoDB manual entry on aggregation pipelines </core/aggregation-pipeline/>`. |
| 184 | + |
| 185 | +Install the Sink Connector |
| 186 | +~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 187 | + |
| 188 | +In the quick start shell, run the following command to set up the sink |
| 189 | +connector using the Kafka Connect REST API: |
| 190 | + |
| 191 | +.. _quick-start-sink-connector: |
| 192 | + |
| 193 | +.. code-block:: bash |
| 194 | + |
| 195 | + curl -X POST \ |
| 196 | + -H "Content-Type: application/json" \ |
| 197 | + --data ' |
| 198 | + {"name": "mongo-sink", |
| 199 | + "config": { |
| 200 | + "connector.class":"com.mongodb.kafka.connect.MongoSinkConnector", |
| 201 | + "connection.uri":"mongodb://mongo1:27017/?replicaSet=rs0", |
| 202 | + "database":"quickstart", |
| 203 | + "collection":"sink", |
| 204 | + "topics":"quickstart.source", |
| 205 | + "change.data.capture.handler": "com.mongodb.kafka.connect.sink.cdc.mongodb.ChangeStreamHandler" |
| 206 | + } |
| 207 | + } |
| 208 | + ' \ |
| 209 | + http://connect:8083/connectors -w "\n" |
| 210 | + |
| 211 | +The preceding command creates a new connector in Kafka Connect and specifies the |
| 212 | +following properties of the connector: |
| 213 | + |
| 214 | +- The class Kafka Connect uses to instantiate the connector |
| 215 | +- The URI, database, and collection of the MongoDB instance the |
| 216 | + connector writes data to |
| 217 | +- The Apache Kafka topic the connector receives data from |
| 218 | +- A change data capture handler for MongoDB change data capture events |
| 219 | + |
| 220 | +To confirm that you successfully installed the source and sink connectors, run |
| 221 | +the following command to print all connectors installed on your Kafka Connect |
| 222 | +deployment: |
| 223 | + |
| 224 | +.. code-block:: bash |
| 225 | + |
| 226 | + curl -X GET http://connect:8083/connectors |
| 227 | + |
| 228 | +You should see the following output: |
| 229 | + |
| 230 | +.. code-block:: none |
| 231 | + :copyable: false |
| 232 | + |
| 233 | + ["mongo-source", "mongo-sink"] |
| 234 | + |
| 235 | +For more information on sink connectors, see our |
| 236 | +:doc:`guide on sink connectors <sink-connector>`. |
| 237 | + |
| 238 | +For more information on change data capture events, see our |
| 239 | +:doc:`guide on change data capture events <sink-connector/fundamentals/change-data-capture>`. |
| 240 | + |
| 241 | +Test the Connectors |
| 242 | +~~~~~~~~~~~~~~~~~~~ |
| 243 | + |
| 244 | +To test the connectors, enter the MongoDB shell from the quick start |
| 245 | +shell: |
| 246 | + |
| 247 | +.. code-block:: bash |
| 248 | + |
| 249 | + mongosh mongodb://mongo1:27017/?replicaSet=rs0 |
| 250 | + |
| 251 | +From within the MongoDB shell, insert a document into the ``source`` |
| 252 | +collection of the ``quickstart`` database using the following commands: |
| 253 | + |
| 254 | +.. code-block:: javascript |
| 255 | + |
| 256 | + use quickstart |
| 257 | + db.source.insertOne({"hello":"world"}) |
| 258 | + |
| 259 | +After you insert a document into the ``source`` collection, check the ``sink`` |
| 260 | +collection to make sure the source and sink connectors correctly handled the |
| 261 | +insertion: |
| 262 | + |
| 263 | +.. code-block:: javascript |
| 264 | + |
| 265 | + db.sink.find() |
| 266 | + |
| 267 | +You should see output that resembles the following: |
| 268 | + |
| 269 | +.. code-block:: json |
| 270 | + :copyable: false |
| 271 | + |
| 272 | + [ |
| 273 | + { |
| 274 | + _id: ObjectId("60f87b2f018bb14f13106be1"), |
| 275 | + hello: 'world', |
| 276 | + travel: 'I went through Kafka!' |
| 277 | + } |
| 278 | + ] |
| 279 | + |
| 280 | +In the preceding output you can see a copy of the document inserted into the |
| 281 | +``source`` collection in the ``sink`` collection with the extra field ``travel`` |
| 282 | +added by the aggregation pipeline defined in the source connector. |
| 283 | + |
| 284 | +Once you have finished exploring the connector in the MongoDB shell, you can |
| 285 | +exit the MongoDB shell with the following command: |
| 286 | + |
| 287 | +.. code-block:: bash |
| 288 | + |
| 289 | + exit |
| 290 | + |
| 291 | +Stop the Pipeline |
| 292 | +----------------- |
| 293 | + |
| 294 | +To conserve resources on your computer, make sure to stop the sample pipeline |
| 295 | +once you are done exploring this example. |
| 296 | + |
| 297 | +Before you stop the sample pipeline, make sure to exit the quick start |
| 298 | +shell. You can exit the quick start shell with the following command: |
| 299 | + |
| 300 | +.. code-block:: bash |
| 301 | + |
| 302 | + exit |
| 303 | + |
| 304 | +To stop the sample pipeline and remove containers and images, run the |
| 305 | +following command: |
| 306 | + |
| 307 | +.. code-block:: bash |
| 308 | + |
| 309 | + docker-compose -p quickstart down --rmi 'all' |
| 310 | + |
| 311 | +Next Steps |
| 312 | +---------- |
| 313 | + |
| 314 | +If you would like to explore any of the MongoDB Kafka Connector features |
| 315 | +mentioned above, you can find more information on them throughout our |
| 316 | +documentation. |
| 317 | + |
| 318 | +For more information on sink connectors in the MongoDB Kafka |
| 319 | +Connector, such as an in-depth discussion of change data capture handlers, |
| 320 | +see :doc:`our sink connector section <sink-connector>`. |
| 321 | + |
| 322 | +For more information on source connectors in the MongoDB Kafka |
| 323 | +Connector, such as a complete list of source connector configuration properties, |
| 324 | +see :doc:`our source connector section <source-connector>`. |
0 commit comments