Updated on 2023-02-28 GMT+08:00

KafkaConsumer

Path

com.roma.apic.livedata.client.v1.KafkaConsumer

Description

This class is used to consume Kafka messages.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
importClass(com.roma.apic.livedata.client.v1.KafkaConsumer);
importClass(com.roma.apic.livedata.config.v1.KafkaConfig);

var kafka_brokers = '1.1.1.1:26330,2.2.2.2:26330'
var topic = 'YourKafkaTopic'
var group = 'YourKafkaGroupId'

function execute(data) {
    var config = KafkaConfig.getConfig(kafka_brokers, group)
    var consumer = new KafkaConsumer(config)
    var records = consumer.consume(topic, 5000, 10);
    var res = []
    var iter = records.iterator()
    while (iter.hasNext()) {
        res.push(iter.next())
    }
    return JSON.stringify(res);
}

Constructor Details

public KafkaConsumer(Map configs)

Constructs a Kafka message consumer.

Parameter: configs indicates configuration information of the Kafka.

Method List

Returned Type

Method and Description

List<String>

consume(String topic, long timeout, long maxItems)

Consume messages.

Method Details

  • public List<String> consume(String topic, long timeout, long maxItems)

    Consume messages.

    Input Parameter

    • topic indicates a message queue.
    • timeout indicates the read timeout interval.
    • maxItems indicates the maximum number of messages that can be read.

    Returns

    Message array that has been consumed by Kafka. The content of multiple messages forms an array.