debezium详解decimal.handling.mode

2023-12-20 14:03:17

欢迎关注留言,我是收集整理小能手

decimal.handling.mode配置的作用,是配置数据库中浮点类型映射类型,
数据库浮点类型包含以下:
mysql:double\float\decimal
oracle: number

以下是来自debezium官网的原文介绍:
https://debezium.io/documentation/reference/2.5/connectors/mysql.html#mysql-connector-properties
这个配置项在debezium的版本迭代中几乎没有变化

decimal.handling.mode

precise(默认值)

Specifies how the connector should handle values for DECIMAL and NUMERIC columns:

precise (the default) represents them precisely using java.math.BigDecimal values represented in change events in a binary form.

double represents them using double values, which may result in a loss of precision but is easier to use.

string encodes values as formatted strings, which is easy to consume but semantic information about the real type is lost.

下面提供一些示例数据,及数据使用方法
以mysql为示例

CREATE TABLE `doubleV` (
  `d18_0` decimal(18,0) DEFAULT NULL,
  `d18_5` decimal(18,5) DEFAULT NULL,
  `d18_10` decimal(18,10) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci;

INSERT INTO testdb.doubleV (d18_0,d18_5,d18_10)
	VALUES (345678901234567890,456789012345.67890,456789.0123456789);

采集作业配置

        {
            "name":"mysql_04",
            "config":{
                "connector.class":"io.debezium.connector.mysql.MySqlConnector",
                "tasks.max":"1",
                "database.hostname":"192.168.1.226",
                "database.port":"3306",
                "database.user":"root",
                "database.password":"123456",
                "database.server.name":"mysqlcdc",
                "table.include.list":"testdb.doubleV",
                "topic.prefix": "mysqlcdc",
                "database.server.id": "1",
                "decimal.handling.mode":"string",
                "schema.history.internal.kafka.bootstrap.servers":"192.168.1.103:9092",
                "schema.history.internal.kafka.topic":"mysqlcdc.history",
                "snapshot.mode":"schema_only",
                "include.schema.changes": "true"

            }
        }
字段名原始数据stringdoublepreciseprecise 反序列化后
d18_0345678901234567890“345678901234567890”3.4567890123456787E17“BMwZIRJvCtI=”345678901234567890
d18_5456789012345.67890“456789012345.67890”4.567890123456789E11“AKJIt/nRCtI=”456789012345.67890
d18_10456789.0123456789“456789.0123456789”456789.0123456789“EDp4zC6BFQ==”456789.0123456789
优点-可读性强类型一致-类型一致,精度不丢失
缺点-类型不一致丢失精度,可读性差可读性差需反序列化

    
    public static void main(String[] args) {
    	printDePrecise("BMwZIRJvCtI=", 0);
    	printDePrecise("AKJIt/nRCtI=", 5);
    	printDePrecise("EDp4zC6BFQ==", 10);
	}
    
    static void printDePrecise (String encoded, int scale) {
    	final BigDecimal decoded = new BigDecimal(new BigInteger(Base64.getDecoder().decode(encoded)), scale);
    	System.out.println(String.format("encoded: %s, scale: %s, decoded: %s", encoded, scale, decoded));
    }

在这里插入图片描述

文章来源:https://blog.csdn.net/a309450028a/article/details/135101586
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。