Scala如何订阅多个kafka主题

时间:2018-12-24 23:27:29

标签: scala apache-kafka kafka-consumer-api

我想在scala中将字符串arry / list转换为util.Collection [String]对象。我尝试了多种方法,但没有解决。

import org.apache.kafka.clients.consumer.KafkaConsumer


object KafkaConsumerApp {

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

    val prop:Properties = new Properties()
    prop.put("bootstrap.servers","192.168.1.100:9092,192.168.1.141:9092,192.168.1.113:9092,192.168.1.118:9092")
    prop.put("key.deserializer","org.apache.kafka.common.serialization.StringDeserializer")
    prop.put("value.deserializer","org.apache.kafka.common.serialization.StringDeserializer")

    val consumer = new KafkaConsumer(prop)

    val topics = List[String] ("my_topic_partition","my_topic_partition")
    val a = Collections.singletonList(topics)

    consumer.subscribe(a)

  }
}

consumer.subscribe(a)返回编译时错误

Error:(24, 14) overloaded method value subscribe with alternatives:
  (x$1: java.util.regex.Pattern)Unit <and>
  (x$1: java.util.Collection[String])Unit
 cannot be applied to (java.util.List[List[String]])
    consumer.subscribe(a)

1 个答案:

答案 0 :(得分:3)

您不需要制作count() def count(month, data): month_count = 0 for row in data[1:]: if isinstance(row[s], str): row[2] = datetime.datetime.fromtimestamp(float(row[2])) if row[2].month = month: month_count += 1 return month_count Singleton已经是List

List

如果您需要使用Java,只需像Collection那样放val: List[String] topics = List("my_topic_partition","my_topic_partition") consumer.subscribe(topics) 并使用导入.asJava