- Building Data Streaming Applications with Apache Kafka
- Manish Kumar Chanchal Singh
- 444字
- 2022-07-12 10:38:14
Kafka Producer APIs
Kafka has provided you with a rich set of APIs to create applications to interact with it. We will go through Producer API details and understand its uses.
Creating a Kafka producer involves the following steps:
- Required configuration.
- Creating a producer object.
- Setting up a producer record.
- Creating a custom partition if required.
- Additional configuration.
Required configuration: In most applications, we first start with creating the initial configuration without which we cannot run the application. The following are three mandatory configuration parameters:
- bootstrap.servers: This contains a list of Kafka brokers addresses. The address is specified in terms of hostname:port. We can specify one or more broker detail, but we recommend that you provide at least two so that if one broker goes down, producer can use the other one.
- key.serializer: The message is sent to Kafka brokers in the form of a key-value pair. Brokers expect this key-value to be in byte arrays. So we need to tell producer which serializer class is to be used to convert this key-value object to a byte array. This property is set to tell the producer which class to use to serialize the key of the message.
Kafka provides us with three inbuilt serializer classes: ByteArraySerializer, StringSerializer, and IntegerSerializer. All these classes are present in the org.apache.kafka.common.serialization package and implement the serializer interface.
- value.serializer: This is similar to the key.serializer property, but this property tells the producer which class to use in order to serialize the value. You can implement your own serialize class and assign to this property.
Let's see how we do it in a programming context.
Here is how Java works for Producer APIs:
Properties producerProps = new Properties();
producerProps.put("bootstrap.servers", "broker1:port,broker2:port");
producerProps.put("key.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
producerProps.put("value.serializer",
"org.apache.kafka.common.serialization.StringSerializer");
KafkaProducer<String, String> producer = new KafkaProducer<String,String>(producerProps);
The Producer API in Scala:
val producerProps = new Properties()
producerProps.put("bootstrap.servers", "broker1:port,broker2:port");
producerProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
val producer = new KafkaProducer[String, String](producerProps)
The preceding code contains three specific points:
- Properties object: We start with creating a property object; this object contains the put method that is used to put the configuration key-value pair in place
- Serializer class: We will use StringSerializer for both key and value as our key and value will be of the string type
- Producer object: We create a producer object by passing the configuration object to it, which provides the producer with specific information about broker servers, serializer classes, and other configurations that we will see later
- 現(xiàn)代C++編程:從入門到實踐
- Bootstrap Site Blueprints Volume II
- 計算思維與算法入門
- WildFly:New Features
- 摩登創(chuàng)客:與智能手機和平板電腦共舞
- 信息可視化的藝術(shù):信息可視化在英國
- Vue.js前端開發(fā)基礎(chǔ)與項目實戰(zhàn)
- Go語言高效編程:原理、可觀測性與優(yōu)化
- Java持續(xù)交付
- Building a Recommendation Engine with Scala
- HDInsight Essentials(Second Edition)
- BIM概論及Revit精講
- 好好學Java:從零基礎(chǔ)到項目實戰(zhàn)
- Laravel Application Development Blueprints
- Go Systems Programming