From 9b1c830820dc7cb4a812efff4631c20b75fa1e1d Mon Sep 17 00:00:00 2001 From: liaozan <378024053@qq.com> Date: Fri, 22 Dec 2023 11:59:45 +0800 Subject: [PATCH] Add utils for canalEvent convert --- .../starrocks-spring-boot-starter/pom.xml | 5 +++ .../starrocks/helper/ConvertUtils.java | 34 +++++++++++++++++++ 2 files changed, 39 insertions(+) create mode 100644 starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/helper/ConvertUtils.java diff --git a/starters/starrocks-spring-boot-starter/pom.xml b/starters/starrocks-spring-boot-starter/pom.xml index b1550da..e2d9910 100644 --- a/starters/starrocks-spring-boot-starter/pom.xml +++ b/starters/starrocks-spring-boot-starter/pom.xml @@ -18,5 +18,10 @@ com.schbrain.framework mybatis-spring-boot-starter + + org.springframework.kafka + spring-kafka + true + diff --git a/starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/helper/ConvertUtils.java b/starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/helper/ConvertUtils.java new file mode 100644 index 0000000..1a3db66 --- /dev/null +++ b/starters/starrocks-spring-boot-starter/src/main/java/com/schbrain/framework/autoconfigure/starrocks/helper/ConvertUtils.java @@ -0,0 +1,34 @@ +package com.schbrain.framework.autoconfigure.starrocks.helper; + +import com.fasterxml.jackson.databind.ObjectMapper; +import com.fasterxml.jackson.databind.PropertyNamingStrategies; +import com.schbrain.common.entity.CanalChangedEvent; +import com.schbrain.common.exception.BaseException; +import com.schbrain.common.util.*; +import org.apache.kafka.clients.consumer.ConsumerRecord; +import org.apache.kafka.clients.consumer.ConsumerRecords; + +import java.util.List; + +/** + * @author liaozan + * @since 2023/12/22 + */ +public class ConvertUtils { + + private static final ObjectMapper DESERIALIZER = JacksonUtils.getObjectMapper().copy().setPropertyNamingStrategy(PropertyNamingStrategies.SNAKE_CASE); + + public static Target convertTo(ConsumerRecord record, Class sourceType, Class targetType) { + CanalChangedEvent event = JacksonUtils.getObjectFromJson(record.value(), CanalChangedEvent.class); + if (event == null) { + throw new BaseException("CanalChangedEvent is null"); + } + Source source = DESERIALIZER.convertValue(event.getAfter(), sourceType); + return BeanCopyUtils.copy(source, targetType); + } + + public static List convertToList(ConsumerRecords records, Class sourceType, Class targetType) { + return StreamUtils.toList(records, record -> convertTo(record, sourceType, targetType)); + } + +} -- GitLab