File: compression.md

package info (click to toggle)
mongo-java-driver 3.6.3-2.1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 16,112 kB
  • sloc: java: 102,506; xml: 395; javascript: 250; sh: 43; makefile: 4
file content (110 lines) | stat: -rw-r--r-- 4,254 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
+++
date = "2017-06-16T22:05:03-04:00"
title = "Compression"
[menu.main]
  parent = "Async Connection Settings"
  identifier = "Async Compression"
  weight = 25
  pre = "<i class='fa'></i>"
+++

## Compression

The Java driver supports compression of messages to and from MongoDB servers.  The driver implements the two algorithms that are 
supported by MongoDB servers:

* [Snappy](https://google.github.io/snappy/): Snappy compression can be used when connecting to MongoDB servers starting with the 3.4 
release.
* [Zlib](https://zlib.net/): Zlib compression can be used when connecting to MongoDB servers starting with the 3.6 release.

The driver will negotiate which, if any, compression algorithm is used based on capabilities advertised by the server in
the [ismaster]({{<docsref "reference/command/isMaster/">}}) command response. 

### Specify compression via connection string

```java
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoClient;

```

To specify compression with a connection string, specify `compressors` as part of the connection string, as in:

```java
MongoClient mongoClient = MongoClients.create("mongodb://localhost/?compressors=snappy");
```

for Snappy compression, or

```java
MongoClient mongoClient = MongoClients.create("mongodb://localhost/?compressors=zlib");
```

for zlib compression, or 


```java
MongoClient mongoClient = MongoClients.create("mongodb://localhost/?compressors=snappy,zlib");
```

to configure multiple compressors. 

In all cases the driver will use the first compressor in the list for which the server advertises support. 

### Specify compression via `MongoClientSettings`

```java
import com.mongodb.connection.ClusterSettings;
import com.mongodb.async.client.MongoClientSettings;
import com.mongodb.async.client.MongoClients;
import com.mongodb.async.client.MongoClient;
import com.mongodb.MongoCompressor;
import java.util.Arrays;

```

To specify compression with [`MongoClientSettings`]({{<apiref "com/mongodb/async/client/MongoClientSettings">}}), set the `compressors` property 
to a list of `MongoCompressor` instances:

```java
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Arrays.asList(new ServerAddress("localhost"))).build();
MongoClientSettings settings = MongoClientSettings.builder()
                                                  .clusterSettings(clusterSettings);
                                                  .compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor()))
                                                  .build();
MongoClient client = MongoClients.create(settings);
```

for Snappy compression, or

```java
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Arrays.asList(new ServerAddress("localhost"))).build();
MongoClientSettings settings = MongoClientSettings.builder()
                                                  .clusterSettings(clusterSettings);
                                                  .compressorList(Arrays.asList(MongoCompressor.createZlibCompressor()))
                                                  .build();
MongoClient client = MongoClients.create(settings);
```

for zlib compression, or

```java
ClusterSettings clusterSettings = ClusterSettings.builder().hosts(Arrays.asList(new ServerAddress("localhost"))).build();
MongoClientSettings settings = MongoClientSettings.builder()
                                                  .clusterSettings(clusterSettings);
                                                  .compressorList(Arrays.asList(MongoCompressor.createSnappyCompressor(),
                                                                                MongoCompressor.createZlibCompressor()))
                                                  .build();
MongoClient client = MongoClients.create(settings);
```

to configure multiple compressors. 

As with configuration with a URI, the driver will use the first compressor in the list for which the server advertises support. 

### Dependencies

As the JDK has no built-in support for Snappy, the driver takes a dependency on an existing open-source Snappy implementation.  See the
[snappy-java Github repository](https://github.com/xerial/snappy-java) for details.