[英]KafkaProducer not successfully sending message into the queue
I have built a small testing environment on my Windows PC and write down the following code for testing kafka (using kafka_2.10:0.9.0.1 from org.apache.kafka). 我在我的Windows PC上构建了一个小测试环境,并记下以下代码来测试kafka(使用org.apache.kafka中的kafka_2.10:0.9.0.1)。
package iii.functiontesting;
import java.text.ParseException;
import java.util.Properties;
import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerRecord;
/**
* Hello world!
*
*/
public class test4
{
public static void main( String[] args ) throws ParseException
{
Properties producerProps=new Properties();
producerProps.put("bootstrap.servers", "localhost:9092");
producerProps.put("serializer.class",org.apache.kafka.common.serialization.StringSerializer.class.getName());
producerProps.put("key.serializer",org.apache.kafka.common.serialization.StringSerializer.class.getName());
producerProps.put("value.serializer",org.apache.kafka.common.serialization.StringSerializer.class.getName());
producerProps.put("request.required.acks","1");
KafkaProducer<String,String> kafkawriter= new KafkaProducer<String,String>(producerProps);
ProducerRecord<String,String> msg=new ProducerRecord<>("TEST3","ImKey","teststring1");
kafkawriter.send(msg);
}
}
I use the following command to check whether the message is correctly written into the queue 我使用以下命令检查消息是否正确写入队列
D:\\Work\\kafkaenv\\kafka_2.10-0.9.0.1\\bin\\windows>.\\kafka-console-consumer.bat --zookeeper localhost:2181 --topic TEST3 --from-beginning
D:\\ Work \\ kafkaenv \\ kafka_2.10-0.9.0.1 \\ bin \\ windows>。\\ kafka-console-consumer.bat --zookeeper localhost:2181 --topic TEST3 - from-beginning
However, I found that the kafka-console-consumer shows nothing. 但是,我发现kafka-console-consumer没有显示任何内容。
I have doubted that my kafka server doesn't run properly, so I use console-producer to test. 我怀疑我的kafka服务器运行不正常,所以我使用console-producer进行测试。
D:\\Work\\kafkaenv\\kafka_2.10-0.9.0.1\\bin\\windows>.\\kafka-console-producer.bat --broker-list localhost:9092 --topic TEST3
D:\\ Work \\ kafkaenv \\ kafka_2.10-0.9.0.1 \\ bin \\ windows>。\\ kafka-console-producer.bat --broker-list localhost:9092 --topic TEST3
aaaaa
AAAAA
This time I can see the aaaaa is clearly shown under the console-consumer. 这次我可以看到aaaaa清楚地显示在控制台消费者之下。 I cannot figure out what happens.
我无法弄清楚会发生什么。 Can anyone help me?
谁能帮我?
You have to call either KafkaProducer#flush
[or] KafkaProducer#close
method before terminating the program. 在终止程序之前,您必须调用
KafkaProducer#flush
[或] KafkaProducer#close
方法。
Actually, the producer buffers the records before sending it to the broker. 实际上,生产者在将记录发送给代理之前缓冲记录。 See
buffer.memory
and batch.size
in the Kafka Producer configuration 请参阅Kafka Producer配置中的
buffer.memory
和batch.size
kafkawriter.send(msg);
kafkawriter.close();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.