|
| 1 | +.. _source-usage-example-copy-existing-data: |
| 2 | + |
1 | 3 | ==================
|
2 | 4 | Copy Existing Data
|
3 | 5 | ==================
|
4 | 6 |
|
5 |
| -TODO: |
| 7 | +.. default-domain:: mongodb |
| 8 | + |
| 9 | +This usage example demonstrates how to copy data from a MongoDB collection to an |
| 10 | +{+ak+} topic using the {+mkc+}. |
| 11 | + |
| 12 | +Example |
| 13 | +------- |
| 14 | + |
| 15 | +Suppose you need to copy a MongoDB collection to {+ak+} and filter some of the data. |
| 16 | + |
| 17 | +Your requirements and your solutions are as follows: |
| 18 | + |
| 19 | +.. list-table:: |
| 20 | + :header-rows: 1 |
| 21 | + :widths: 50 50 |
| 22 | + |
| 23 | + * - Requirement |
| 24 | + - Solution |
| 25 | + |
| 26 | + * - Copy the ``customers`` collection of the ``shopping`` database in your |
| 27 | + MongoDB deployment onto an {+ak+} topic. |
| 28 | + - | See the :ref:`<source-usage-example-copy-existing-data-copy-data>` section of this guide. |
| 29 | + |
| 30 | + * - Only copy documents that have the value "Mexico" in the ``country`` field. |
| 31 | + - | See the :ref:`<source-usage-example-copy-existing-data-mask-data>` section of this guide. |
| 32 | + |
| 33 | +The ``customers`` collection contains the following documents: |
| 34 | + |
| 35 | +.. _usage-example-copy-sample-document: |
| 36 | + |
| 37 | +.. code-block:: json |
| 38 | + :copyable: false |
| 39 | + |
| 40 | + { |
| 41 | + "_id": 1, |
| 42 | + "country": "Mexico", |
| 43 | + "purchases": 2, |
| 44 | + "last_viewed": { "$date": "2021-10-31T20:30:00.245Z" } |
| 45 | + } |
| 46 | + { |
| 47 | + "_id": 2, |
| 48 | + "country": "Iceland", |
| 49 | + "purchases": 8, |
| 50 | + "last_viewed": { "$date": "2015-07-20T10:00:00.135Z" } |
| 51 | + } |
| 52 | + |
| 53 | +.. _source-usage-example-copy-existing-data-copy-data: |
| 54 | + |
| 55 | +Copy Data |
| 56 | +~~~~~~~~~ |
| 57 | + |
| 58 | +Copy the contents of the ``customers`` collection of the ``shopping`` database by |
| 59 | +specifying the following configuration options in your source connector: |
| 60 | + |
| 61 | +.. code-block:: properties |
| 62 | + |
| 63 | + database=shopping |
| 64 | + collection=customers |
| 65 | + copy.existing=true |
| 66 | + |
| 67 | +Your source connector copies your collection by creating change event documents |
| 68 | +that describe inserting each document into your collection. |
| 69 | + |
| 70 | +To learn more about change event documents, see the |
| 71 | +:ref:`Change Streams <source-connector-fundamentals-change-event>` guide. |
| 72 | + |
| 73 | +To learn more about the ``copy.existing`` option, see |
| 74 | +:ref:`<source-configuration-copy-existing>` in the {+mkc+}. |
| 75 | + |
| 76 | +.. _source-usage-example-copy-existing-data-mask-data: |
| 77 | + |
| 78 | +Filter Data |
| 79 | +~~~~~~~~~~~ |
| 80 | + |
| 81 | +You can filter data by specifying an aggregation pipeline in the |
| 82 | +``copy.existing.pipeline`` option of your source connector configuration. The |
| 83 | +following configuration specifies an aggregation pipeline that matches all |
| 84 | +documents with "Mexico" in the ``country`` field: |
| 85 | + |
| 86 | +.. code-block:: properties |
| 87 | + |
| 88 | + copy.existing.pipeline=[{ "$match": { "country": "Mexico" } }] |
| 89 | + |
| 90 | +To learn more about the ``copy.existing.pipeline`` option, see |
| 91 | +:ref:`<source-configuration-copy-existing>` in the {+mkc+}. |
| 92 | + |
| 93 | +To learn more about aggregation pipelines, see the following resources: |
| 94 | + |
| 95 | +- :ref:`<source-usage-example-custom-pipeline>` Usage Example |
| 96 | +- :manual:`Aggregation </aggregation>` in the MongoDB manual. |
| 97 | + |
| 98 | + |
| 99 | +Copy Data Configuration |
| 100 | +~~~~~~~~~~~~~~~~~~~~~~~ |
| 101 | + |
| 102 | +Your source connector configuration to copy the ``customers`` collection should |
| 103 | +look like this: |
| 104 | + |
| 105 | +.. literalinclude:: /includes/usage-examples/copy/source.properties |
| 106 | + :language: properties |
| 107 | + :emphasize-lines: 5,6 |
| 108 | + |
| 109 | +Once your connector copies your data, you see the following change event |
| 110 | +document corresponding to the |
| 111 | +:ref:`preceding sample collection <usage-example-copy-sample-document>` |
| 112 | +in the ``shopping.customers`` {+ak+} topic: |
| 113 | + |
| 114 | +.. literalinclude:: /includes/usage-examples/copy/payload.json |
| 115 | + :language: json |
| 116 | + :copyable: false |
| 117 | + :emphasize-lines: 6,7 |
| 118 | + |
| 119 | +.. note:: Write the Data in your Topic into a Collection |
| 120 | + |
| 121 | + Use a change data capture handler to convert change event documents in an |
| 122 | + {+ak+} topic into MongoDB write operations. To learn more, see the |
| 123 | + :ref:`Change Data Capture Handlers <sink-fundamentals-cdc-handler>` guide. |
0 commit comments