从地图功能(SCALA)中写入kafka主题?
是否可以从地图方法
中发布主题// Main Function
def main(args: Array[String]) {
...
// some list
val list_ = ("a", "b", "c", "d")
// Setup Properties
val props = new Properties()
props.setProperty("zookeeper.connect", zookeeper_url + ":" + zookeeper_port)
props.setProperty("bootstrap.servers", broker_url + ":" + broker_port)
props.setProperty("auto.offset.reset", "earliest")
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
...
// Connect to Source
val input_stream = env.addSource(new FlinkKafkaConsumer09[String](topic_in, new SimpleStringSchema(), properties))
// Process each Record
val stream = input_stream.map(x=> {
// loop through list "list_" -> variable in in Main
// and publish to topic_out
// -- THIS IS MY CURRENT ISSUE !!!)
// -- Does not work (No compile issue)
//
var producer2 = new KafkaProducer[String, String](props)
var record = new ProducerRecord(topic_out, "KEY", list(i))
producer2.send(record)
producer2.flush()
// ... Other process and return processed string
})
// publish to different topic of proccessed input string (Works)
stream.addSink(new FlinkKafkaProducer09[String](broker_url + ":" + broker_port, other_topic, new SimpleStringSchema()))
答案 0 :(得分:3)
不要在map函数中创建kafka生成器,也不要尝试写入map中的kafka主题。老实说,我不能引用任何说它是个坏主意的东西......但这是一个坏主意。
相反。将map函数更改为flatMap(请参阅此处的第一个示例:https://ci.apache.org/projects/flink/flink-docs-release-1.3/dev/datastream_api.html)。
所以在你的循环中,不是每个循环都创建一个kafka生成器,而是执行collector.collect(recordToPublishToKafka)
。
你的接收器会在收集它们时将它们发布出去。