要使用Scala与Kafka构建实时数据管道,您可以按照以下步骤操作:
libraryDependencies += "org.apache.kafka" % "kafka-clients" % "2.0.0"
import java.util.Properties
import org.apache.kafka.clients.consumer.KafkaConsumer
import org.apache.kafka.clients.producer.{KafkaProducer, ProducerRecord}
// Kafka 生产者
val props = new Properties()
props.put("bootstrap.servers", "localhost:9092")
props.put("key.serializer", "org.apache.kafka.common.serialization.StringSerializer")
props.put("value.serializer", "org.apache.kafka.common.serialization.StringSerializer")
val producer = new KafkaProducer[String, String](props)
val topic = "test"
val key = "key1"
val value = "value1"
val record = new ProducerRecord[String, String](topic, key, value)
producer.send(record)
producer.close()
// Kafka 消费者
val consumerProps = new Properties()
consumerProps.put("bootstrap.servers", "localhost:9092")
consumerProps.put("group.id", "test-group")
consumerProps.put("key.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
consumerProps.put("value.deserializer", "org.apache.kafka.common.serialization.StringDeserializer")
val consumer = new KafkaConsumer[String, String](consumerProps)
val topics = List("test")
consumer.subscribe(topics)
while (true) {
val records = consumer.poll(100)
for (record <- records.asScala) {
println(record.key() + ": " + record.value())
}
}
通过以上步骤,您可以使用Scala与Kafka构建实时数据管道。希望以上信息对您有所帮助。