1

Cannot resolve symbol for Foreach

import java.util._
import org.apache.kafka.clients.consumer._
import org.apache.kafka.common.serialization.Deserializer

object ConsumerExample {

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

    val T_Name = "CarSensor"
    val T_Group_Name = "CarSensorGroup"
    val props = new Properties()

    props.put("bootstrap.servers", "localhost:9092,localhost:9093,localhost:9094")
    props.put("group.id",T_Group_Name)
    props.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
    props.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")

    val Kafka_Consumer=new KafkaConsumer[String,String](props)
    Kafka_Consumer.subscribe (Arrays.asList(T_Name))
    while(true)
    {
      val Consumer_Record=Kafka_Consumer.poll(100) //ConsumerRecords Object
      //  val RecordList=Consumer_Record.toString
      for( i <- Consumer_Record)
      { //**This place is where Cannot resolve symbol for Foreach issue shows up for <- symbol.**
        println("Supplier id = "+String.valueOf(i.value().getID())+ "Supplier name = " +i.value().getID())
      }
    }
  }
}

I have used <- symbol in many examples before it worked. I thought it was an issue with Intelliji and restarted it. Its a problem in object getting casted to different type I guess.

2 Answers 2

2
  Consumer_Record.forEach(i => {
    println("Supplier id = "+String.valueOf(i.value().getID())+ "Supplier name = " +i.value().getID())
  })

works fine for me.

Except String doesn't have getID() method.

You can use for(i <- Consumer_Record.asScala) if you want for syntax, but you have to add import scala.jdk.CollectionConverters._.

Sign up to request clarification or add additional context in comments.

Comments

0
val Kafka_Consumer=new KafkaConsumer[String,String](props)

Kafka_Consumer.subscribe(Arrays.asList(T_Name))

while(true) {

  val Consumer_Record=Kafka_Consumer.poll(100) //ConsumerRecords Object

  for( i <- Consumer_Record.asScala) {
    println("Supplier id = "+String.valueOf(i.value())+ " Supplier name = " +i.key())
  }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.