Updated on 2023-05-17 GMT+08:00

KafkaConsumer

Path

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

Description

This class is used to consume Kafka messages.

Example

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 (maximum value: 30,000 ms). Set this parameter to a value less than the backend timeout of the frontend API.
  • 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.