12.12. 在 Debezium 事件记录中转换时区值
当 Debezium 发出事件记录时,记录中时间戳字段的时区值可能会因数据源的类型和配置而异。要在数据处理管道和应用程序内保持数据一致性和精度,您可以使用 Timezone Converter
SMT 来确保事件记录使用一致的时区来代表时间戳数据。
SMT 使用 converted.timezone
配置选项将指定字段的值转换为目标时区。您可以将目标时区指定为地理时区,例如 America/New_York
,或指定为 UTC 偏移,如 +02:00
。假设记录的字段是 UTC。除了指定的时区,SMT 还提供配置选项,以使用 include.list
和 exclude.list
配置选项从时区转换中包含或排除特定字段。
SMT 支持所有 Debezium 和 Kafka Connect 临时和非临时类型。
以下主题提供详情:
要遵守夏天节省的时间,您必须在 converted.timezone
配置选项中指定地理时区。如果您指定了 UTC 偏移,则转换应用 UTC 的固定偏移,对于观察到夏时时间的区域没有准确。当将时间戳字段转换为没有观察到夏天保存时间的特定时区时,提供固定的 UTC 偏移非常有用。
include.list
和 exclude.list
配置选项是互斥的。您必须只指定其中一个选项。
12.12.1. 示例:基本 Debezium 时区转换器 SMT 配置
在 Kafka Connect 配置中为连接器配置 TimezoneConverter
SMT,将事件记录中的基于时间的字段转换为目标时区。
例如,要将事件记录中的所有时间戳字段从 UTC 转换为 Pacific/Easter
时区,请将以下行添加到您的连接器配置中:
transforms=convertTimezone transforms.convertTimezone.type=io.debezium.transforms.TimezoneConverter transforms.convertTimezone.converted.timezone=Pacific/Easter
12.12.1.1. 将 TimezoneConverter
SMT 应用到 Debezium 事件记录的影响
以下示例演示了 TimezoneConverter
转换如何修改事件记录中的时间戳字段。第一个示例显示了一个 Debezium 事件记录,它不是由转换处理;记录会保留其原始时间戳值。下一个示例显示了应用转换后的同一事件记录。根据 基本配置示例中 指定的配置,SMT 将源消息中时间戳字段的原始 UTC 值转换为 Pacific/Easter
timezone 值。
例 12.3. TimezoneConverter
转换处理前的事件记录值
created_at
字段的值显示 UTC 时间。
{ "before": null, "after": { "id": 1, "first_name": "Anne", "last_name": "Kretchmar", "email": "annek@noanswer.org", "created_at": "2011-01-11T16:40:30.123456789+00:00" }, "source": { "version": "2.5.4.Final", "connector": "postgresql", "name": "PostgreSQL_server", "ts_ms": 1559033904863, "snapshot": true, "db": "postgres", "sequence": "[\"24023119\",\"24023128\"]", "schema": "public", "table": "customers", "txId": 555, "lsn": 24023128, "xmin": null }, "op": "c", "ts_ms": 1559033904863 }
例 12.4. TimezoneConverter
转换处理后的事件记录值
SMT 将 created_at
字段的原始 UTC 值转换为 基本配置 示例中指定的目标 Pacific/Easter
时区中的时间。SMT 还会添加 event_timestamp
字段。
{ "before": null, "after": { "id": 1, "first_name": "Anne", "last_name": "Kretchmar", "email": "annek@noanswer.org", "created_at": "2011-01-11T11:40:30.123456789-05:00" }, "source": { "version": "2.5.4.Final", "connector": "postgresql", "name": "PostgreSQL_server", "ts_ms": 1559033904863, "snapshot": true, "db": "postgres", "sequence": "[\"24023119\",\"24023128\"]", "schema": "public", "table": "customers", "txId": 555, "lsn": 24023128, "xmin": null, "id": 100 }, "op": "c", "ts_ms": 1559033904863, "event_timestamp": 1626102708861 }
12.12.2. 示例:高级 Debezium 时区转换器 SMT 配置
您可以将 SMT 配置为仅转换特定字段,而不是转换事件记录中的所有时间戳字段。以下示例演示了如何在 SMT 配置中使用 include.list
选项,来仅在事件记录中转换 created_at
和 updated_at
时间戳字段。以下配置使用固定偏移(而非地理时区设计器)将时间从 UTC 转换为 +05:30
。
transforms=convertTimezone transforms.convertTimezone.type=io.debezium.transforms.TimezoneConverter transforms.convertTimezone.converted.timezone=+05:30 transforms.convertTimezone.include.list=source:customers:created_at,customers:updated_at
在某些情况下,您可能想要从时区转换中排除特定的时间戳字段。例如,要从时区转换中排除事件记录中的 updated_at
时间戳字段,请使用 exclude.list
配置选项,如下例所示:
transforms=convertTimezone transforms.convertTimezone.type=io.debezium.transforms.TimezoneConverter transforms.convertTimezone.converted.timezone=+05:30 transforms.convertTimezone.exclude.list=source:customers:updated_at
12.12.3. 用于配置 Debezium 时区转换器转换的选项
下表列出了 TimezoneConverter
SMT 的配置选项。
属性 | 描述 | 类型 | 重要性 |
指定应将时间戳字段转换为的目标时区的字符串。目标时区可以指定为地理时区,如 | string | high | |
以逗号分隔的规则列表,用于指定 SMT 包含用于时区转换的字段。使用以下格式指定规则:
| list | 中 | |
以逗号分隔的规则列表,用于指定要从时区转换中排除的字段。使用以下格式指定规则:
| list | 中 |