无法解析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())
}
}
}
}
在很多例子中,我已经使用<-符号来起作用。 我认为这是Intelliji的问题,然后重新启动。我猜这是将对象强制转换为其他类型的问题。
答案 0 :(得分:1)
Consumer_Record.forEach(i => {
println("Supplier id = "+String.valueOf(i.value().getID())+ "Supplier name = " +i.value().getID())
})
对我来说很好。
String
除外,没有getID()
方法。
如果要使用for(i <- Consumer_Record.asScala)
语法,则可以使用for
,但是必须添加import scala.collection.JavaConverters._
。
答案 1 :(得分: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())
}
}