-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathSimpleKafkaConsumer.scala
37 lines (29 loc) · 1.21 KB
/
SimpleKafkaConsumer.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
package io.gatling.consumer
import java.util
import org.apache.kafka.clients.consumer.{ConsumerConfig, ConsumerRecord, ConsumerRecords, KafkaConsumer}
import org.apache.kafka.common.serialization.StringDeserializer
object SimpleKafkaConsumer extends App {
val kafkaTopic = "test_topic"
val kafkaBrokers = "localhost:9092"
val props = new util.HashMap[String, Object]()
props.put(ConsumerConfig.BOOTSTRAP_SERVERS_CONFIG, kafkaBrokers)
props.put(ConsumerConfig.VALUE_DESERIALIZER_CLASS_CONFIG,
classOf[StringDeserializer])
props.put(ConsumerConfig.KEY_DESERIALIZER_CLASS_CONFIG,
classOf[StringDeserializer])
props.put("group.id", "test-group")
props.put("enable.auto.commit", "true")
props.put("auto.commit.interval.ms", "1000")
props.put("session.timeout.ms", "30000")
val consumer = new KafkaConsumer[String, String](props)
consumer.subscribe(java.util.Arrays.asList(kafkaTopic))
while (true) {
val records: ConsumerRecords[String, String] = consumer.poll(100)
val recordsIterator = records.iterator()
while (recordsIterator.hasNext) {
val currentRecord: ConsumerRecord[String, String] = recordsIterator.next()
println(currentRecord.value())
}
Thread.sleep(1000)
}
}