这里是Jasko,我是Kafka的新朋友。如您所见,我开发了一个“主题生产者”,这是一个简单的示例。我的目的是向客户发送JSON格式的消息。然后,我创建了第一个JSON,下一步是将其转换为消息。我将其构建为.jar格式的项目-但由于使用Kerberos系统,因此无法对其进行测试。我的代码或配置可能出问题了。我在测试时遇到问题。**
包装测试
import java.util.Properties
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerConfig, ProducerRecord}
object TestKafkaProducer {
def main(args: Array[String]): Unit = {
val broker = "your.web.host`enter code here`:9092"
val topic = "testTopic"
val props = new Properties()
//which specifies a list of host/port pairs to use for establishing the initial connection to the Kafka cluster**strong text**
props.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, broker)
//default serialization and deserialization libraries for the record key-value pairs
props.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
props.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, "org.apache.kafka.common.serialization.StringSerializer")
props
val producer = new KafkaProducer[String, String](props)
// TODO pass json object
def sendMessages(): Unit = {
println("Get Object json ")
val message = "{\"schema\":{\"type\":\"string\",\"optional\":false},\"payload\":\"foo\"}"
while (!message.isEmpty) {
val record = new ProducerRecord[String, String](topic, "1", message)
producer.send(record)
}
producer.close()
}
}
}