Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to compress data in producers when using spring kafka

I am currently sending data using spring-kafka like this:

val json = objectWriter.writeValueAsString(obj)
kafkaTemplate.send(topic, json)

How do i tell KafkaTemplate to use compress the json using snappy before sending?

like image 684
pdeva Avatar asked Oct 07 '19 21:10

pdeva


People also ask

How does Kafka compress data?

To make Kafka compression more effective, use batching. Kafka producers internally use a batching mechanism to send multiple messages in one batch over the network.

When I compress my messages from the producer side?

Message Compression is always done at the producer side, so there is no requirement to change the configurations at the consumer or broker side. In the figure, a producer batch of 200 MB is created. After compression, it is reduced to 101 MB. To compress the data, a 'compression.

Is the compression codecs supported in Kafka?

Kafka supports 4 compression codecs: none , gzip , lz4 and snappy .

How do I change batch size in Kafka?

size measures batch size in total bytes instead of the number of messages. It controls how many bytes of data to collect before sending messages to the Kafka broker. Set this as high as possible, without exceeding available memory. The default value is 16384.


2 Answers

In apache kafka there is producer config compression.type with valid values

The compression type for all data generated by the producer. The default is none (i.e. no compression). Valid values are none, gzip, snappy, lz4, or zstd

So you can set in producer configs

ProducerConfig.COMPRESSION_TYPE_CONFIG "snappy"

or by using properties

spring.kafka.producer.compression-type= # Compression type for all data generated by the producer.
like image 191
Deadpool Avatar answered Nov 14 '22 21:11

Deadpool


1.) Compression at broker level.

You can start your zookeeper server with --config compression.type=gzip at the end of the command or you can add this property in zookeeper config file.

2.) Compression at producer level

Set the property compression.type = gzip in config/producer.properties of your producer or start your producer with below property

--compression-codec 'gzip', 'snappy', 'lz4', or 'zstd'.
If specified without value, then it
defaults to 'gzip'

like image 33
Asif Malek Avatar answered Nov 14 '22 23:11

Asif Malek