|
1 |
| -.. _compression: |
| 1 | +.. _java-compression: |
2 | 2 | .. _network-compression:
|
3 | 3 |
|
4 |
| -=================== |
5 |
| -Network Compression |
6 |
| -=================== |
| 4 | +======================== |
| 5 | +Compress Network Traffic |
| 6 | +======================== |
7 | 7 |
|
8 |
| -You can enable a driver option to compress messages which reduces the amount |
9 |
| -of data passed over the network between MongoDB and your application. |
| 8 | +.. contents:: On this page |
| 9 | + :local: |
| 10 | + :backlinks: none |
| 11 | + :depth: 1 |
| 12 | + :class: singlecol |
10 | 13 |
|
11 |
| -The driver supports the following algorithms: |
| 14 | +.. facet:: |
| 15 | + :name: genre |
| 16 | + :values: reference |
| 17 | + |
| 18 | +.. meta:: |
| 19 | + :keywords: zlib, zstandard, zstd, snappy |
12 | 20 |
|
13 |
| -1. `Snappy <https://google.github.io/snappy/>`__: available in MongoDB 3.4 and later. |
| 21 | +Overview |
| 22 | +-------- |
14 | 23 |
|
15 |
| -2. `Zlib <https://zlib.net/>`__: available in MongoDB 3.6 and later. |
| 24 | +In this guide, you can learn how to use the {+driver-short+} to enable network |
| 25 | +compression. The driver provides a connection option to compress messages, which |
| 26 | +reduces the amount of data passed over the network between MongoDB and your application. |
16 | 27 |
|
17 |
| -3. `Zstandard <https://github.com/facebook/zstd/>`__: available in MongoDB 4.2 and later. |
| 28 | +The driver supports the following compression algorithms: |
| 29 | + |
| 30 | +- `Snappy <https://google.github.io/snappy/>`__: Available in {+mdb-server+} v3.4 and later. |
| 31 | +- `Zlib <https://zlib.net/>`__: Available in {+mdb-server+} v3.6 and later. |
| 32 | +- `Zstandard <https://github.com/facebook/zstd/>`__: Available in {+mdb-server+} v4.2 and later. |
18 | 33 |
|
19 | 34 | The driver tests against the following versions of these libraries:
|
20 | 35 |
|
21 | 36 | - ``{+snappyVersion+}``
|
22 | 37 | - ``{+zstdVersion+}``
|
23 | 38 |
|
24 |
| -If you specify multiple compression algorithms, the driver selects the |
25 |
| -first one in the list supported by the MongoDB instance to which it is |
26 |
| -connected. |
| 39 | +If you specify multiple compression algorithms, the driver selects the first one |
| 40 | +in the list supported by your MongoDB instance. |
27 | 41 |
|
28 | 42 | .. note::
|
29 | 43 |
|
30 | 44 | Applications that require Snappy or Zstandard compression must
|
31 |
| - :ref:`add explicit dependencies <compression-dependencies>` for those |
32 |
| - algorithms. |
| 45 | + add explicit dependencies for those algorithms. To learn more, |
| 46 | + see the :ref:`java-compression-dependencies` section of this guide. |
33 | 47 |
|
34 |
| -.. _enable-compression: |
| 48 | +.. _java-compression-specify: |
35 | 49 |
|
36 | 50 | Specify Compression Algorithms
|
37 | 51 | ------------------------------
|
38 | 52 |
|
39 | 53 | You can enable compression for the connection to your MongoDB instance
|
40 |
| -by specifying the algorithms in one of two ways: adding the parameter to your |
41 |
| -connection string using ``ConnectionString`` or by calling the method in the |
42 |
| -``MongoClientSettings.Builder`` class. |
43 |
| - |
44 |
| -.. tabs:: |
| 54 | +by specifying the algorithms in one of the following ways: |
45 | 55 |
|
46 |
| - .. tab:: ConnectionString |
47 |
| - :tabid: connectionstring |
48 |
| - |
49 |
| - To enable compression using the `ConnectionString <{+api+}/apidocs/mongodb-driver-core/com/mongodb/ConnectionString.html>`__, |
50 |
| - add the parameter ``compressors`` in the connection string passed to |
51 |
| - ``MongoClients.create()``. You can specify one or more compression |
52 |
| - algorithms, separating them with commas: |
| 56 | +- Use the ``compressors`` parameter in your connection string. |
| 57 | +- Chain the ``compressorList()`` method to the ``MongoClientSettings.builder()`` method. |
53 | 58 |
|
54 |
| - .. code-block:: java |
| 59 | +This example shows how to specify the Snappy, Zstandard, and Zlib compression |
| 60 | +algorithms. Select the :guilabel:`Connection String` or :guilabel:`MongoClientSettings` |
| 61 | +tab to see the corresponding syntax: |
55 | 62 |
|
56 |
| - ConnectionString connectionString = new ConnectionString("mongodb+srv://<db_username>:<db_password>@<cluster-url>/?compressors=snappy,zlib,zstd"); |
57 |
| - MongoClient mongoClient = MongoClients.create(connectionString); |
| 63 | +.. tabs:: |
58 | 64 |
|
59 |
| - Specify compression algorithms using the following strings: |
| 65 | + .. tab:: Connection String |
| 66 | + :tabid: connectionstring |
60 | 67 |
|
61 |
| - - "snappy" for `Snappy <https://google.github.io/snappy/>`__ compression |
62 |
| - - "zlib" for `Zlib <https://zlib.net/>`__ compression |
63 |
| - - "zstd" for `Zstandard <https://github.com/facebook/zstd/>`__ compression |
| 68 | + .. literalinclude:: /includes/connect/NetworkCompression.java |
| 69 | + :start-after: start-specify-connection-string |
| 70 | + :end-before: end-specify-connection-string |
| 71 | + :language: java |
64 | 72 |
|
65 | 73 | .. tab:: MongoClientSettings
|
66 | 74 | :tabid: mongoclientsettings
|
67 | 75 |
|
68 |
| - To enable compression using the `MongoClientSettings <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.html>`__, |
69 |
| - pass the `compressorList() <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoClientSettings.Builder.html#compressorList(java.util.List)>`__ |
70 |
| - builder method a list of `MongoCompressor <{+api+}/apidocs/mongodb-driver-core/com/mongodb/MongoCompressor.html>`__ |
71 |
| - instances. You can specify one or more compression algorithms in the list: |
72 |
| - |
73 |
| - .. code-block:: java |
74 |
| - :emphasize-lines: 2-4 |
75 |
| - |
76 |
| - MongoClientSettings settings = MongoClientSettings.builder() |
77 |
| - .compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor(), |
78 |
| - MongoCompressor.createZlibCompressor(), |
79 |
| - MongoCompressor.createZstdCompressor())) |
80 |
| - .build(); |
81 |
| - MongoClient client = MongoClients.create(settings); |
| 76 | + .. literalinclude:: /includes/connect/NetworkCompression.java |
| 77 | + :start-after: start-specify-client-settings |
| 78 | + :end-before: end-specify-client-settings |
| 79 | + :language: java |
82 | 80 |
|
83 |
| -.. _compression-dependencies: |
| 81 | +.. _java-compression-dependencies: |
84 | 82 |
|
85 | 83 | Compression Algorithm Dependencies
|
86 | 84 | ----------------------------------
|
87 | 85 |
|
88 |
| -The JDK supports `Zlib <https://zlib.net/>`__ compression natively, but |
89 |
| -`Snappy <https://google.github.io/snappy/>`__ and |
90 |
| -`Zstandard <https://github.com/facebook/zstd/>`__ depend on open source |
91 |
| -implementations. See |
92 |
| -`snappy-java <https://github.com/xerial/snappy-java>`__ and |
93 |
| -`zstd-java <https://github.com/luben/zstd-jni>`__ for details. |
| 86 | +The JDK natively supports `Zlib <https://zlib.net/>`__ compression. However, |
| 87 | +Snappy and Zstandard depend on open source Java implementations. To learn more |
| 88 | +about these implementations, see the following GitHub repositories: |
| 89 | + |
| 90 | +- `snappy-java <https://github.com/xerial/snappy-java>`__ |
| 91 | +- `zstd-java <https://github.com/luben/zstd-jni>`__ |
| 92 | + |
| 93 | +API Documentation |
| 94 | +----------------- |
94 | 95 |
|
| 96 | +To learn more about any of the methods or types discussed in this |
| 97 | +guide, see the following API documentation: |
95 | 98 |
|
| 99 | +- `MongoClient <{+api+}/apidocs/mongodb-driver-sync/com/mongodb/client/MongoClient.html>`__ |
| 100 | +- `createSnappyCompressor() <{+core-api+}/com/mongodb/MongoCompressor.html#createSnappyCompressor()>`__ |
| 101 | +- `createZlibCompressor() <{+core-api+}/com/mongodb/MongoCompressor.html#createZlibCompressor()>`__ |
| 102 | +- `createZstdCompressor() <{+core-api+}/com/mongodb/MongoCompressor.html#createZstdCompressor()>`__ |
0 commit comments