[英]Compiling a custom producer for Kafka
I've downloaded Kafka from https://www.apache.org/dyn/closer.cgi?path=/kafka/0.8.1.1/kafka_2.8.0-0.8.1.1.tgz and set up a kafka cluster on my machine using VMs. 我从https://www.apache.org/dyn/closer.cgi?path=/kafka/0.8.1.1/kafka_2.8.0-0.8.1.1.tgz下载了Kafka,并在我的机器上设置了一个kafka集群虚拟机。 The cluster is working fine - it was tested using the console producer and consumer provided with the kafka package.
集群工作正常 - 它使用随kafka软件包提供的控制台生产者和消费者进行测试。
Now, I've implemented a custom Producer class for Kafka. 现在,我已经为Kafka实现了一个自定义的Producer类。 But I couldn't figure out how to compile this class and what the dependencies are.
但我无法弄清楚如何编译这个类以及依赖是什么。
Questions 问题
Could someone explain how I need to go about fetching the dependencies for the Producer, building the class and running it? 有人可以解释我需要如何获取Producer的依赖项,构建类并运行它?
Do I need sbt to build it? 我需要sbt来构建它吗? I couldn't find any online resources which clearly explained how to go about building a custom kafka producer class.
我找不到任何明确解释如何构建自定义kafka生产者类的在线资源。
Following are the packages imported in the Producer class: 以下是在Producer类中导入的包:
org.apache.kafka.clients.producer.Callback;
org.apache.kafka.clients.producer.KafkaProducer;
org.apache.kafka.clients.producer.ProducerConfig;
org.apache.kafka.clients.producer.ProducerRecord;
org.apache.kafka.clients.producer.RecordMetadata;
org.apache.kafka.common.record.Records
Thanks in advance 提前致谢
I developed a custom Kafka producer, as a Maven project, dependency I used was: 我开发了一个自定义Kafka生成器,作为Maven项目,我使用的依赖是:
<dependency>
<groupId>org.apache.kafka</groupId>
<artifactId>kafka-clients</artifactId>
<version>0.8.2.0</version>
</dependency>
Imports I used: 我使用的进口:
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.ByteArraySerializer;
import org.apache.kafka.common.serialization.StringSerializer;
A snippet of my producer message sending code: 我的生产者消息发送代码的片段:
Properties props = new Properties();
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, zkConnection);
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class);
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, ByteArraySerializer.class);
byte[] byteData = null;
File myInputFile = new File(...);
try (InputStream inputStream = new FileInputStream(myInputFile)) {
byteData = IOUtils.toByteArray(inputStream);
}
try (KafkaProducer<String, byte[]> producer = new KafkaProducer<String, byte[]>(props)) {
producer.send(new ProducerRecord<String, byte[]>(topic, byteData));
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.