我的Kafka群集版本为0.11.0.3
这是我的scala代码,用于向Kafka集群生成数据:
val props = new Properties()
props.put("bootstrap.servers", "192.168.0.240:9092,192.168.0.213:9092,192.168.0.235:9092")
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
val producer = new KafkaProducer[String,String](props)
var counter = 0
var keyFlag = 0
while(true){
counter +=1
keyFlag +=1
val content: String = userlogs()
producer.send(new ProducerRecord[String, String]("test2", s"key-$keyFlag", content))
if(0 == counter%200){
counter = 0
Thread.sleep(2000)
}
}
producer.close()
}
def userlogs()={
// produce data...
}
kafka-client maven版本也是0.11.0.3。 但是下面的方法很有用:
./kafka-console-producer.sh --broker-list 192.168.0.240:9092,192.168.0.213:9092,192.168.0.235:9092 --topic test2