- 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.
推薦閱讀
- INSTANT Mock Testing with PowerMock
- Vue.js前端開發基礎與項目實戰
- C# 從入門到項目實踐(超值版)
- 數據結構與算法JavaScript描述
- Python GUI Programming Cookbook
- HTML5游戲開發案例教程
- GitLab Repository Management
- PHP+MySQL+Dreamweaver動態網站開發實例教程
- Python機器學習實戰
- 零基礎輕松學SQL Server 2016
- Learning OpenStack Networking(Neutron)(Second Edition)
- Hadoop大數據分析技術
- Qt 4開發實踐
- 奔跑吧 Linux內核
- AI自動化測試:技術原理、平臺搭建與工程實踐