- Building Data Streaming Applications with Apache Kafka
- Manish Kumar Chanchal Singh
- 282字
- 2022-07-12 10:38:15
Java Kafka producer example
We have covered different configurations and APIs in previous sections. Let's start coding one simple Java producer, which will help you create your own Kafka producer.
Prerequisite
- IDE: We recommend that you use a Scala-supported IDE such as IDEA, NetBeans, or Eclipse. We have used JetBrains IDEA:
https://www.jetbrains.com/idea/download/. - Build tool: Maven, Gradle, or others. We have used Maven to build our project.
- Pom.xml: Add Kafka dependency to the pom file:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka_2.11</artifactId>
<version>0.10.0.0</version>
</dependency>
Java:
import java.util.Properties;
import java.util.concurrent.Future;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.clients.producer.RecordMetadata;
public class DemoProducer {
public static void main(final String[] args) {
Properties producerProps = new Properties();
producerProps.put("bootstrap.servers", "localhost:9092");
producerProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer");
producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer");
producerProps.put("acks", "all");
producerProps.put("retries", 1);
producerProps.put("batch.size", 20000);
producerProps.put("linger.ms", 1);
producerProps.put("buffer.memory", 24568545);
KafkaProducer<String, String> producer = new KafkaProducer<String, String>(producerProps);
for (int i = 0; i < 2000; i++) {
ProducerRecord data = new ProducerRecord<String, String>("test1", "Hello this is record " + i);
Future<RecordMetadata> recordMetadata = producer.send(data);
}
producer.close();
}
}
Scala:
import java.util.Properties
import org.apache.kafka.clients.producer._
object DemoProducer extends App {
override def main(args: Array[String]): Unit = {
val producerProps = new Properties()
producerProps.put("bootstrap.servers", "localhost:9092")
producerProps.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
producerProps.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
producerProps.put("client.id", "SampleProducer")
producerProps.put("acks", "all")
producerProps.put("retries", new Integer(1))
producerProps.put("batch.size", new Integer(16384))
producerProps.put("linger.ms", new Integer(1))
producerProps.put("buffer.memory", new Integer(133554432))
val producer = new KafkaProducer[String, String](producerProps)
for (a <- 1 to 2000) {
val record: ProducerRecord[String, String] = new ProducerRecord("test1", "Hello this is record"+a)
producer.send(record);
}
producer.close()
}
}
The preceding example is a simple Java producer where we are producing string data without a key. We have also hardcoded the topic name, which probably can be read through configuration file or as an command line input. To understand producer, we have kept it simple. However, we will see good examples in upcoming chapters where we will follow good coding practice.
推薦閱讀
- 軟件安全技術(shù)
- MongoDB for Java Developers
- YARN Essentials
- jQuery Mobile移動(dòng)應(yīng)用開(kāi)發(fā)實(shí)戰(zhàn)(第3版)
- 速學(xué)Python:程序設(shè)計(jì)從入門(mén)到進(jìn)階
- Visual Studio Code 權(quán)威指南
- Java程序設(shè)計(jì)與項(xiàng)目案例教程
- 代碼閱讀
- 軟件體系結(jié)構(gòu)
- Java程序設(shè)計(jì)教程
- Java 11 and 12:New Features
- Android應(yīng)用開(kāi)發(fā)攻略
- 美麗洞察力:從化妝品行業(yè)看顧客需求洞察
- Java Web入門(mén)很輕松(微課超值版)
- Spring MVC Cookbook