Elffuss BraidHub

kikocis/java-driver public

3.1.x · 2.4 KiBRenderedRawPermalink
1## Compression
2
3Cassandra's binary protocol supports optional compression of
4transport-level requests and responses, for example:
5
6* a query with its serialized parameters;
7* a [page](../paging/) from a result set, i.e. a list of serialized
8 rows.
9
10It reduces network traffic at the cost of CPU overhead, therefore it
11will likely be beneficial when you have larger payloads.
12
13Two algorithms are available:
14[LZ4](https://github.com/jpountz/lz4-java) and
15[Snappy](https://code.google.com/p/snappy/).
16Both rely on third-party libraries, declared by the driver as *optional*
17dependencies. So If you use a build tool like Maven, you'll need to
18declare an explicit dependency to pull the appropriate library in your
19application's classpath. Then you configure compression at driver
20startup.
21
22### LZ4
23
24Maven dependency:
25
26```xml
27<dependency>
28 <groupId>net.jpountz.lz4</groupId>
29 <artifactId>lz4</artifactId>
30 <version>1.3.0</version>
31</dependency>
32```
33
34Always check the exact version of the library: go to the driver's
35[parent POM][pom] (change the URL to match your driver version) and look
36for the `lz4.version` property.
37
38Driver configuration:
39
40```java
41cluster = Cluster.builder()
42 .addContactPoint("127.0.0.1")
43 .withCompression(ProtocolOptions.Compression.LZ4)
44 .build();
45```
46
47LZ4-java has three internal implementations (from fastest to slowest):
48
49* JNI;
50* pure Java using sun.misc.Unsafe;
51* pure Java using only "safe" classes.
52
53It will pick the best implementation depending on what's possible on
54your platform. To find out which one was chosen, [enable INFO
55logs](../logging/) on the category
56`com.datastax.driver.core.FrameCompressor` and look for a log similar to
57this:
58
59```
60INFO com.datastax.driver.core.FrameCompressor - Using LZ4Factory:JNI
61```
62
63### Snappy
64
65Maven dependency:
66
67```xml
68<dependency>
69 <groupId>org.xerial.snappy</groupId>
70 <artifactId>snappy-java</artifactId>
71 <version>1.1.2.6</version>
72</dependency>
73```
74
75Always check the exact version of the library: go to the driver's
76[parent POM][pom] (change the URL to match your driver version) and look
77for the `snappy.version` property.
78
79Driver configuration:
80
81```java
82cluster = Cluster.builder()
83 .addContactPoint("127.0.0.1")
84 .withCompression(ProtocolOptions.Compression.SNAPPY)
85 .build();
86```
87
88[pom]: https://repo1.maven.org/maven2/com/datastax/cassandra/cassandra-driver-parent/3.1.4/cassandra-driver-parent-3.1.4.pom