From 299d8d5e232252c33a62429c8343b9993f144813 Mon Sep 17 00:00:00 2001 From: lanyuanxiaoyao Date: Mon, 4 Mar 2024 09:08:46 +0800 Subject: [PATCH] =?UTF-8?q?fix(sync):=20=E4=BF=AE=E5=A4=8Ddecimal=E7=B1=BB?= =?UTF-8?q?=E5=9E=8B=E5=90=8D=E7=A7=B0=E9=87=8D=E5=A4=8D=E5=AF=BC=E8=87=B4?= =?UTF-8?q?schema=E8=A7=A3=E6=9E=90=E6=8A=A5=E9=94=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Schema中的类型名称不能重复,比如有一个decimal_15如果后面的字段再出现同样的类型名称,就会报错 --- .../sync/functions/type/TypeConverterV2.java | 6 ++-- .../lanyuanxiaoyao/service/sync/TestType.java | 28 +++++++++++++++++++ utils/sync/src/test/resources/logback.xml | 11 ++++++++ 3 files changed, 42 insertions(+), 3 deletions(-) create mode 100644 utils/sync/src/test/java/com/lanyuanxiaoyao/service/sync/TestType.java create mode 100644 utils/sync/src/test/resources/logback.xml diff --git a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/type/TypeConverterV2.java b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/type/TypeConverterV2.java index 3e1b4c5..6955ebb 100644 --- a/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/type/TypeConverterV2.java +++ b/utils/sync/src/main/java/com/lanyuanxiaoyao/service/sync/functions/type/TypeConverterV2.java @@ -1,6 +1,7 @@ package com.lanyuanxiaoyao.service.sync.functions.type; import cn.hutool.core.util.ObjectUtil; +import cn.hutool.core.util.StrUtil; import com.lanyuanxiaoyao.service.common.Constants; import com.lanyuanxiaoyao.service.common.entity.TableMeta; import java.math.BigDecimal; @@ -34,8 +35,8 @@ public class TypeConverterV2 implements TypeConverter { public static final Schema FLOAT_SCHEMA = create(Type.FLOAT); public static final Schema DOUBLE_SCHEMA = create(Type.DOUBLE); public static final Schema STRING_SCHEMA = create(Type.STRING); - public static final Function FIXED_SCHEMA = length -> createFixed("decimal_" + length, null, null, length); - public static final BiFunction DECIMAL_SCHEMA = (length, scala) -> LogicalTypes.decimal(length, scala).addToSchema(FIXED_SCHEMA.apply(length)); + public static final BiFunction FIXED_SCHEMA = (length, scala) -> createFixed(StrUtil.format("decimal_{}_{}", length, scala), null, null, length); + public static final BiFunction DECIMAL_SCHEMA = (length, scala) -> LogicalTypes.decimal(length, scala).addToSchema(FIXED_SCHEMA.apply(length, scala)); public static final BiFunction NULLABLE_DECIMAL_SCHEMA = (length, scala) -> createUnion(NULL_SCHEMA, DECIMAL_SCHEMA.apply(length, scala)); public static final Schema NULLABLE_BOOLEAN_SCHEMA = createUnion(NULL_SCHEMA, BOOLEAN_SCHEMA); public static final Schema NULLABLE_INT_SCHEMA = createUnion(NULL_SCHEMA, INT_SCHEMA); @@ -43,7 +44,6 @@ public class TypeConverterV2 implements TypeConverter { public static final Schema NULLABLE_FLOAT_SCHEMA = createUnion(NULL_SCHEMA, FLOAT_SCHEMA); public static final Schema NULLABLE_DOUBLE_SCHEMA = createUnion(NULL_SCHEMA, DOUBLE_SCHEMA); public static final Schema NULLABLE_STRING_SCHEMA = createUnion(NULL_SCHEMA, STRING_SCHEMA); - public static final Function NULLABLE_FIXED_SCHEMA = length -> createUnion(NULL_SCHEMA, FIXED_SCHEMA.apply(length)); private static final Pattern BOOLEAN_REGEX = Pattern.compile("^boolean|bool$"); private static final Pattern INT_REGEX = Pattern.compile("^(tinyint|smallint|int|smallserial|integer)(\\(\\d+\\))?$"); private static final Pattern LONG_REGEX = Pattern.compile("^(bigint unsigned)|((bigint|serial|long)(\\(\\d+\\))?)$"); diff --git a/utils/sync/src/test/java/com/lanyuanxiaoyao/service/sync/TestType.java b/utils/sync/src/test/java/com/lanyuanxiaoyao/service/sync/TestType.java new file mode 100644 index 0000000..fd2bc8c --- /dev/null +++ b/utils/sync/src/test/java/com/lanyuanxiaoyao/service/sync/TestType.java @@ -0,0 +1,28 @@ +package com.lanyuanxiaoyao.service.sync; + +import cn.hutool.http.HttpUtil; +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import com.lanyuanxiaoyao.service.common.entity.TableMeta; +import com.lanyuanxiaoyao.service.sync.functions.type.TypeConverterV2; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +/** + * @author lanyuanxiaoyao + * @date 2024-03-01 + */ +public class TestType { + private static final Logger logger = LoggerFactory.getLogger(TestType.class); + + public static void main(String[] args) throws JsonProcessingException { + ObjectMapper mapper = new ObjectMapper(); + String content = HttpUtil + .createGet("http://b12s7.hdp.dc:15123/info/table_meta/detail?flink_job_id=1749270121369489408&alias=irms_main_pr_type_type") + .basicAuth("AxhEbscwsJDbYMH2", "cYxg3b4PtWoVD5SjFayWxtnSVsjzRsg4") + .execute() + .body(); + TableMeta meta = mapper.readValue(content, TableMeta.class); + logger.info("{}", new TypeConverterV2().convertToSchema(meta).toString()); + } +} diff --git a/utils/sync/src/test/resources/logback.xml b/utils/sync/src/test/resources/logback.xml new file mode 100644 index 0000000..7014617 --- /dev/null +++ b/utils/sync/src/test/resources/logback.xml @@ -0,0 +1,11 @@ + + + + %d{yyyy-MM-dd HH:mm:ss.SSS} %p [%t] %logger #@# %m%n%ex{full} + + + + + + + \ No newline at end of file