TIMESTAMP_EPOCH_UNIT_MISMATCH data type_error ai_generated true

时间戳反序列化失败,因为纪元以毫秒为单位但预期是秒

Timestamp deserialization fails because epoch is in milliseconds but expected in seconds

ID: data/timestamp-epoch-millis-vs-seconds

其他格式: JSON · Markdown 中文 · English
90%修复率
89%置信度
1证据数
2024-06-22首次发现

版本兼容性

版本状态引入弃用备注
Apache Kafka 3.5.0 active
Python datetime 3.11 active
Java 17 active

根因分析

数据源提供Unix纪元毫秒的时间戳(如1700000000000),但反序列化器期望秒(如1700000000),导致溢出或错误日期。

English

The data source provides timestamps as Unix epoch milliseconds (e.g., 1700000000000) but the deserializer expects seconds (e.g., 1700000000), causing overflow or incorrect dates.

generic

官方文档

https://docs.oracle.com/javase/8/docs/api/java/time/Instant.html

解决方案

  1. Detect the unit by checking the magnitude: if timestamp > 1e12, treat as milliseconds and divide by 1000. Example in Python: `if ts > 1e12: ts /= 1000`.
  2. Configure the deserializer to expect milliseconds explicitly, e.g., in Kafka Connect: `timestamp.converter=org.apache.kafka.connect.json.JsonConverter` with `converter.type=timestamp` and `converter.format=epoch.millis`.

无效尝试

常见但无效的做法:

  1. 70% 失败

    Dividing by 1000 in code may cause integer overflow if the timestamp is stored as a 32-bit integer.

  2. 80% 失败

    Assuming all timestamps are in milliseconds may break when some sources use seconds.