problem with ConsumerRecords

512 views
Skip to first unread message

bruno.c...@sjrp.unesp.br

unread,
Jan 17, 2017, 4:14:38 PM1/17/17
to kafka-clients
Hi! I'm doing a Kafka exercise using Scala to deal with the Consumer API.
But, I'm having a little problem: I can't iterate over the records that I get from my polling.
Follow the code:

val consumerTest: KafkaConsumer[String,String] = new KafkaConsumer[String, String](props)

consumerTest.subscribe(Collections
 
.singletonList("teste"))

try{

 
while(true) {

   
val records: ConsumerRecords[String, String] = consumerTest.poll(1000)

   
for(record <- records) {

     
println(s"offset = ${record.offset()}, key = ${record.key()}, value = ${record.value()}")
   
}
 
}
} catch {

 
case e: KafkaException => println("there was a exception")
} finally {

 
consumerTest.close()
}

The error I'm getting is: "value foreach is no a member of org.apache.kafka.clients.consumer.ConsumerRecords[String, String]", in the for statement.
I tried using the iterator method from ConsumerRecords, but I get the same error.

If someone could help me, I'll be really glad.

Thanks!

Ewen Cheslack-Postava

unread,
Jan 18, 2017, 12:42:26 AM1/18/17
to bruno.c...@sjrp.unesp.br, kafka-clients
ConsumerRecords is a Java Iterable. Scala's for loop will generally only work on Scala collections (or Java ones that go out of their way to implement the Scala compatibility. Add

import scala.collection.JavaConversions._

to your code to include an implicit conversion that will turn it into a Scala Iterable for you automatically.

-Ewen

--
You received this message because you are subscribed to the Google Groups "kafka-clients" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kafka-clients+unsubscribe@googlegroups.com.
To post to this group, send email to kafka-...@googlegroups.com.
Visit this group at https://groups.google.com/group/kafka-clients.
To view this discussion on the web visit https://groups.google.com/d/msgid/kafka-clients/88712524-9a9a-424b-9727-ec119d1d21d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

bruno.c...@sjrp.unesp.br

unread,
Jan 18, 2017, 2:39:02 AM1/18/17
to kafka-clients, bruno.c...@sjrp.unesp.br
Thank you very very much Ewen! It worked smoothly!
It is good to know this information, because it might be helpful with other problems that I may encounter in the future.

Thank you!
To unsubscribe from this group and stop receiving emails from it, send an email to kafka-client...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages