我的Kafka Producer代码运行正常,没有任何异常,但是没有数据在代理中发送

时间:2018-09-19 11:08:54

标签: scala apache-kafka

我创建了主题“ test_topic_02”,并将数据手动写入代理,该代理成功。但是,当我使用以下代码生成数据时,将数据写入代理无效。

object KafkaProducer {

    private val log: slf4j.Logger = LoggerFactory.getLogger(this.getClass)
    Logger.getLogger("org").setLevel(Level.WARN)

    def main(args: Array[String]): Unit = {

        val topic = "test_topic_02"
        val brokers = "10.31.31.45:9092"
        val props = new Properties()
        var partition: Int = 0
        //partition
        val list: List[Int] = List(0, 1, 2, 3, 4)
        props.put("bootstrap.servers", brokers)
        props.put("client.id", "KafkaProducer")
        props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
        props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
        //create producer
        val producer: KafkaProducer[String, String] = new KafkaProducer[String, String](props)
        while (true) {
            for (i <- 1 to 100) {
                for (j <- list) {
                    partition = j
                    try {
                        val producerData = new ProducerRecord[String, String](topic, Integer.valueOf(partition), "message from simulator_" + Integer.toString(i), Integer.toString(i))
                        val future: Future[RecordMetadata] = producer.send(producerData)
                        println(producerData)
                        //implicit number to long 
                        future.get(long2Long(3), TimeUnit.SECONDS)
                        println("Message Sent Successfully")
                        Thread.sleep(1000)
                    } catch {
                        case e : Exception =>
                        log.error("Launching Failed")
                    }
                }
            }
        }
        println("Stop Producing Data")
        producer.close()
    }
}

1 个答案:

答案 0 :(得分:0)

您应该使用另一个send函数,该函数需要回调才能写入Kafka。在回调中,您可以查找异常或错误。

ProducerRecord<byte[],byte[]> record = new ProducerRecord<byte[],byte[]>("the-topic", key, value);
 producer.send(myRecord,
               new Callback() {
                   public void onCompletion(RecordMetadata metadata, Exception e) {
                       if(e != null) {
                          e.printStackTrace();
                       } else {
                          System.out.println("The offset of the record we just sent is: " + metadata.offset());
                       }
                   }
               });