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()
}
}
}
There is nothing wrong with your code.
Application Log
Console Consumer Output