无法将消息发送到kafka主题 [英] Not able to send messages to kafka topic
本文介绍了无法将消息发送到kafka主题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用Kafka,Play和Scala.这是我的代码,我要将邮件发送到kafka服务器,主题名称为" test-topic ".尽管没有在主题中看到发送的消息,但我没有收到任何错误这里有什么问题吗
I am using Kafka, Play as well as Scala. This is my code , where i want to send messages to kafka server,Topic name is "test-topic" . i am not getting any error though i haven't seen my sent messages in the topics is there anything wrong here
import kafka.producer.ProducerConfig
import java.util.Properties
import kafka.producer.Producer
import scala.util.Random
import kafka.producer.Producer
import kafka.producer.Producer
import kafka.producer.Producer
import kafka.producer.KeyedMessage
import java.util.Date
object KafkaProducerLocal extends App {
sendMessage
def sendMessage {
val topicName = "test-topic"
try {
val rnd = new Random()
val props = new Properties()
props.put("metadata.broker.list", "localhost:9092") //kafka
props.put("zk.connect", "localhost:2181"); //zookeeper
props.put("serializer.class", "kafka.serializer.StringEncoder")
props.put("producer.type", "async")
val config = new ProducerConfig(props)
val producer = new Producer[String, String](config)
val t = System.currentTimeMillis()
for (nEvents <- Range(0, 10)) {
val ip = "192.168.2." + rnd.nextInt(255);
val data = new KeyedMessage[String, String](topicName, ip, "Swapnil Test Data" + nEvents);
producer.send(data);
}
producer.close();
} catch {
case t: Throwable => t.printStackTrace()
}
}
}
推荐答案
您的代码没有错.
应用程序日志
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Verifying properties
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Property metadata.broker.list is overridden to localhost:9092
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Property producer.type is overridden to async
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Property serializer.class is overridden to kafka.serializer.StringEncoder
2016-04-19 01:12:34 WARN kafka.utils.Logging$class:83 - Property zk.connect is not valid
SLF4J: Class path contains multiple SLF4J bindings.
SLF4J: Found binding in [jar:file:/home/vishnu/.m2/repository/org/slf4j/slf4j-log4j12/1.7.12/slf4j-log4j12-1.7.12.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: Found binding in [jar:file:/home/vishnu/.m2/repository/ch/qos/logback/logback-classic/1.1.2/logback-classic-1.1.2.jar!/org/slf4j/impl/StaticLoggerBinder.class]
SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Shutting down producer
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Begin shutting down ProducerSendThread
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Fetching metadata from broker id:0,host:localhost,port:9092 with correlation id 0 for 1 topic(s) Set(topic-test)
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Connected to localhost:9092 for producing
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Disconnecting from localhost:9092
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Connected to HMECL001076:9092 for producing
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Shutdown ProducerSendThread complete
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Closing all sync producers
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Disconnecting from HMECL001076:9092
2016-04-19 01:12:34 INFO kafka.utils.Logging$class:68 - Producer shutdown completed in 298 ms
控制台消费者输出
/opt/kafka/kafka_2.10-0.8.2.1/bin/kafka-console-consumer.sh --zookeeper localhost:2181 --topic topic-test --property group.id cs1 --from-beginning
Swapnil Test Data3
Swapnil Test Data9
Swapnil Test Data2
Swapnil Test Data5
Swapnil Test Data6
Swapnil Test Data8
Swapnil Test Data0
Swapnil Test Data1
Swapnil Test Data4
Swapnil Test Data7
这篇关于无法将消息发送到kafka主题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文