Search in sources :

Example 1 with KafkaFlatMessage

use of com.alibaba.otter.canal.client.kafka.protocol.KafkaFlatMessage in project canal by alibaba.

the class KafkaOffsetCanalConnector method getFlatListWithoutAck.

/**
 * 获取Kafka消息,不确认
 *
 * @param timeout
 * @param unit
 * @param offset  消息偏移地址(-1为不偏移)
 * @return
 * @throws CanalClientException
 */
public List<KafkaFlatMessage> getFlatListWithoutAck(Long timeout, TimeUnit unit, long offset) throws CanalClientException {
    waitClientRunning();
    if (!running) {
        return new ArrayList<>();
    }
    if (offset > -1) {
        TopicPartition tp = new TopicPartition(topic, partition == null ? 0 : partition);
        kafkaConsumer2.seek(tp, offset);
    }
    ConsumerRecords<String, String> records = kafkaConsumer2.poll(unit.toMillis(timeout));
    if (!records.isEmpty()) {
        List<KafkaFlatMessage> flatMessages = new ArrayList<>();
        for (ConsumerRecord<String, String> record : records) {
            String flatMessageJson = record.value();
            FlatMessage flatMessage = JSON.parseObject(flatMessageJson, FlatMessage.class);
            KafkaFlatMessage message = new KafkaFlatMessage(flatMessage, record.offset());
            flatMessages.add(message);
        }
        return flatMessages;
    }
    return new ArrayList<>();
}
Also used : KafkaFlatMessage(com.alibaba.otter.canal.client.kafka.protocol.KafkaFlatMessage) FlatMessage(com.alibaba.otter.canal.protocol.FlatMessage) TopicPartition(org.apache.kafka.common.TopicPartition) KafkaFlatMessage(com.alibaba.otter.canal.client.kafka.protocol.KafkaFlatMessage) ArrayList(java.util.ArrayList)

Aggregations

KafkaFlatMessage (com.alibaba.otter.canal.client.kafka.protocol.KafkaFlatMessage)1 FlatMessage (com.alibaba.otter.canal.protocol.FlatMessage)1 ArrayList (java.util.ArrayList)1 TopicPartition (org.apache.kafka.common.TopicPartition)1