正如我使用url创建简单的kafka消费者:https://gist.github.com/akhil/6dfda8a04e33eff91a20。
在该链接中,要打印已消费的记录,请使用单词" asScala" ,那是没有确定的。并且,请告诉我,如何迭代返回类型:ConsumerRecord [String,String],这是poll()方法的返回类型。
import java.util
import java.util.Properties
import org.apache.kafka.clients.consumer.{ConsumerRecords, KafkaConsumer}
object KafkaConsumerEx extends App {
val topic_name = "newtopic55"
val consumer_group = "KafkaConsumerBatch"
val prot = new Properties()
prot.put("bootstrap.servers","localhost:9092")
prot.put("group.id",consumer_group)
prot.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
prot.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer")
val kfk_consumer = new KafkaConsumer[String,String](prot)
kfk_consumer.subscribe(util.Collections.singleton(topic_name))
println("here")
while(true){
val consumer_record : ConsumerRecords[String, String] = kfk_consumer.poll(100)
println("records count : " + consumer_record.count())
println("records partitions: " + consumer_record.partitions())
consumer_record.iterator().
}
}

先谢谢。
答案 0 :(得分:5)
你可以轻松地做到这一点
for (record <- consumer_record.iterator()) {
println(s"Here's your $record")
}
请记住添加此导入:
import scala.collection.JavaConversions._
答案 1 :(得分:0)
添加另一个答案,因为scala.collection.JavaConversions
已被弃用,如here所述。
对于这个问题,代码可能像
import scala.collection.JavaConverters._
for (record <- asScalaIterator(consumer_record.iterator)) {
println(s"Here's your $record")
}
答案 2 :(得分:0)
while(true){
val consumer_records = kfk_consumer.poll(100)
val record_iter=consumer_record.iterator()
while(record_iter.hasNext())
{
record=record_iter.next()
println("records partitions: " + record.partition()
"records_data:" + record.value())
}
}