|
| 1 | +.. _javars-compression: |
| 2 | + |
| 3 | +=========== |
| 4 | +Compression |
| 5 | +=========== |
| 6 | + |
| 7 | +.. contents:: On this page |
| 8 | + :local: |
| 9 | + :backlinks: none |
| 10 | + :depth: 2 |
| 11 | + :class: singlecol |
| 12 | + |
| 13 | +The {+driver-short+} supports compression of messages to and from MongoDB |
| 14 | +servers. The driver implements the three algorithms that are supported |
| 15 | +by MongoDB servers: |
| 16 | + |
| 17 | +- `Snappy <https://google.github.io/snappy/>`__: Snappy compression can |
| 18 | + be used when connecting to MongoDB servers running version 3.4 and later. |
| 19 | +- `Zlib <https://zlib.net/>`__: Zlib compression can be used when |
| 20 | + connecting to MongoDB servers running version 3.6 and later. |
| 21 | +- `Zstandard <https://github.com/facebook/zstd/>`__: Zstandard |
| 22 | + compression can be used when connecting to MongoDB servers running |
| 23 | + version 4.2 and later. |
| 24 | + |
| 25 | +The driver will negotiate which, if any, compression algorithm is used |
| 26 | +based on capabilities advertised by the server in the ``hello`` command |
| 27 | +response. |
| 28 | + |
| 29 | +Specify Compression By Using ConnectionString |
| 30 | +--------------------------------------------- |
| 31 | + |
| 32 | +Include the following import statements: |
| 33 | + |
| 34 | +.. code-block:: java |
| 35 | + |
| 36 | + import com.mongodb.ConnectionString; |
| 37 | + import com.mongodb.reactivestreams.client.MongoClients; |
| 38 | + import com.mongodb.reactivestreams.client.MongoClient; |
| 39 | + |
| 40 | +To specify compression within a ``ConnectionString``, specify |
| 41 | +compressors as part of the connection string. |
| 42 | + |
| 43 | +The following code specifies the Snappy compression algorithm: |
| 44 | + |
| 45 | +.. code-block:: java |
| 46 | + |
| 47 | + ConnectionString connectionString = |
| 48 | + new ConnectionString("mongodb://localhost/?compressors=snappy"); |
| 49 | + MongoClient mongoClient = MongoClients.create(connectionString); |
| 50 | + |
| 51 | +The following code specifies the Zlib compression algorithm: |
| 52 | + |
| 53 | +.. code-block:: java |
| 54 | + |
| 55 | + ConnectionString connectionString = |
| 56 | + new ConnectionString("mongodb://localhost/?compressors=zlib"); |
| 57 | + MongoClient mongoClient = MongoClients.create(connectionString); |
| 58 | + |
| 59 | +The following code specifies the Zstandard compression algorithm: |
| 60 | + |
| 61 | +.. code-block:: java |
| 62 | + |
| 63 | + ConnectionString connectionString = |
| 64 | + new ConnectionString("mongodb://localhost/?compressors=zstd"); |
| 65 | + MongoClient mongoClient = MongoClients.create(connectionString); |
| 66 | + |
| 67 | +The following code specifies multiple compression algorithms: |
| 68 | + |
| 69 | +.. code-block:: java |
| 70 | + |
| 71 | + ConnectionString connectionString = |
| 72 | + new ConnectionString("mongodb://localhost/?compressors=snappy,zlib,zstd"); |
| 73 | + MongoClient mongoClient = MongoClients.create(connectionString); |
| 74 | + |
| 75 | +In all cases, the driver uses the first compressor in the list for which |
| 76 | +the server has support. |
| 77 | + |
| 78 | +Specify Compression By Using MongoClientSettings |
| 79 | +------------------------------------------------ |
| 80 | + |
| 81 | +Include the following import statements: |
| 82 | + |
| 83 | +.. code-block:: java |
| 84 | + |
| 85 | + import com.mongodb.MongoClientSettings; |
| 86 | + import com.mongodb.MongoCompressor; |
| 87 | + import java.util.Arrays; |
| 88 | + |
| 89 | +To specify compression within a ``MongoClientSettings`` instance, set the ``compressors`` |
| 90 | +property to a list of ``MongoCompressor`` instances. |
| 91 | + |
| 92 | +The following code specifies the Snappy compression algorithm: |
| 93 | + |
| 94 | +.. code-block:: java |
| 95 | + |
| 96 | + MongoClientSettings settings = MongoClientSettings.builder() |
| 97 | + .compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor())) |
| 98 | + .build(); |
| 99 | + MongoClient client = MongoClients.create(settings); |
| 100 | + |
| 101 | +The following code specifies the Zlib compression algorithm: |
| 102 | + |
| 103 | +.. code-block:: java |
| 104 | + |
| 105 | + MongoClientSettings settings = MongoClientSettings.builder() |
| 106 | + .compressorList(Arrays.asList(MongoCompressor.createZlibCompressor())) |
| 107 | + .build(); |
| 108 | + MongoClient client = MongoClients.create(settings); |
| 109 | + |
| 110 | +The following code specifies the Zstandard compression algorithm: |
| 111 | + |
| 112 | +.. code-block:: java |
| 113 | + |
| 114 | + MongoClientSettings settings = MongoClientSettings.builder() |
| 115 | + .compressorList(Arrays.asList(MongoCompressor.createZstdCompressor())) |
| 116 | + .build(); |
| 117 | + MongoClient client = MongoClients.create(settings); |
| 118 | + |
| 119 | +The following code specifies multiple compression algorithms: |
| 120 | + |
| 121 | +.. code-block:: java |
| 122 | + |
| 123 | + MongoClientSettings settings = MongoClientSettings.builder() |
| 124 | + .compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor(), |
| 125 | + MongoCompressor.createZlibCompressor(), |
| 126 | + MongoCompressor.createZstdCompressor())) |
| 127 | + .build(); |
| 128 | + MongoClient client = MongoClients.create(settings); |
| 129 | + |
| 130 | +As with configuration that uses a URI, the driver uses the first |
| 131 | +compressor in the list for which the server has support. |
| 132 | + |
| 133 | +Dependencies |
| 134 | +------------ |
| 135 | + |
| 136 | +As the JDK has no built-in support for Snappy or Zstandard, the driver |
| 137 | +takes a dependency on existing open-source Snappy and Zstandard |
| 138 | +implementations. See the `snappy-java GitHub repository |
| 139 | +<https://github.com/xerial/snappy-java>`__ and the `zstd-java GitHub |
| 140 | +repository <https://github.com/luben/zstd-jni>`__ for details. |
0 commit comments