This is an automated email from the ASF dual-hosted git repository. github-merge-queue[bot] pushed a commit to branch gh-readonly-queue/dev/pr-13370-e2597998a991f30fd7c13a634cdd07c7a0a5db44 in repository https://gitbox.apache.org/repos/asf/seatunnel.git
commit d1e1d79b9e09bf61ad57878b9887c7d173471f39 Author: corgy-w <[email protected]> AuthorDate: Fri Nov 20 09:49:49 2026 +0000 [Fix][Connector-V2][InfluxDB] Write processing-time and TIMESTAMP point times with millisecond precision (#13370) --- docs/en/connectors/sink/InfluxDB.md | 11 ++-- docs/zh/connectors/sink/InfluxDB.md | 4 +- .../influxdb/serialize/DefaultSerializer.java | 7 ++- .../influxdb/serialize/DefaultSerializerTest.java | 70 +++++++++++++++++++++- 4 files changed, 80 insertions(+), 12 deletions(-) diff --git a/docs/en/connectors/sink/InfluxDB.md b/docs/en/connectors/sink/InfluxDB.md index c198ff0973..57ad47f431 100644 --- a/docs/en/connectors/sink/InfluxDB.md +++ b/docs/en/connectors/sink/InfluxDB.md @@ -85,11 +85,12 @@ The InfluxDB user password. Configure it together with `username` when authentic ### key_time [string] -The field name in the upstream row that supplies the InfluxDB point timestamp. When omitted, the -sink uses the current processing time. The referenced field must be a `BIGINT` or a `STRING` -containing a numeric epoch value in the precision configured by the `epoch` option (default `n`, -nanoseconds), or a `TIMESTAMP` value, which is converted to epoch milliseconds using UTC and only -matches the point precision when `epoch` is set to `MS`. ISO-8601 timestamp strings are not parsed. +The field name in the upstream row that supplies the InfluxDB point timestamp. When `key_time` is +omitted, the sink uses the current processing time, which is written with millisecond precision. +The referenced field must be a `BIGINT` or a `STRING` containing a numeric epoch value in the +precision configured by the `epoch` option (default `n`, nanoseconds), or a `TIMESTAMP` value, +which is converted to epoch milliseconds using UTC and written with millisecond precision, so it +is correct for any `epoch` value. ISO-8601 timestamp strings are not parsed. ### key_tags [array] diff --git a/docs/zh/connectors/sink/InfluxDB.md b/docs/zh/connectors/sink/InfluxDB.md index a7b0f0dc29..d9609a4c98 100644 --- a/docs/zh/connectors/sink/InfluxDB.md +++ b/docs/zh/connectors/sink/InfluxDB.md @@ -85,9 +85,9 @@ InfluxDB 用户密码。 ### key_time [string] 指定 SeaTunnelRow 中作为 InfluxDB measurement 时间戳的字段名。 -如果未指定,则使用处理时间作为时间戳;该字段必须是 `BIGINT` 或内容为数字的 `STRING` +如果未指定,则使用处理时间作为时间戳,处理时间按毫秒精度写入;该字段必须是 `BIGINT` 或内容为数字的 `STRING` (数值按 `epoch` 选项配置的精度解释,默认 `n` 即纳秒),或 `TIMESTAMP` 类型 -(按 UTC 时区转换为 epoch 毫秒,仅当 `epoch` 设为 `MS` 时与写入精度一致),不支持 ISO-8601 时间戳字符串。 +(按 UTC 时区转换为 epoch 毫秒并按毫秒精度写入,因此对任意 `epoch` 取值都能得到正确的时间戳),不支持 ISO-8601 时间戳字符串。 ### key_tags [array] diff --git a/seatunnel-connectors-v2/connector-influxdb/src/main/java/org/apache/seatunnel/connectors/seatunnel/influxdb/serialize/DefaultSerializer.java b/seatunnel-connectors-v2/connector-influxdb/src/main/java/org/apache/seatunnel/connectors/seatunnel/influxdb/serialize/DefaultSerializer.java index 2a63e4103d..d3526434cb 100644 --- a/seatunnel-connectors-v2/connector-influxdb/src/main/java/org/apache/seatunnel/connectors/seatunnel/influxdb/serialize/DefaultSerializer.java +++ b/seatunnel-connectors-v2/connector-influxdb/src/main/java/org/apache/seatunnel/connectors/seatunnel/influxdb/serialize/DefaultSerializer.java @@ -115,7 +115,8 @@ public class DefaultSerializer implements Serializer { SeaTunnelRowType seaTunnelRowType, String timeKey) { // not config timeKey, use processing time if (Strings.isNullOrEmpty(timeKey)) { - return (row, builder) -> builder.time(System.currentTimeMillis(), precision); + return (row, builder) -> + builder.time(System.currentTimeMillis(), TimeUnit.MILLISECONDS); } int timeFieldIndex = seaTunnelRowType.indexOf(timeKey); @@ -132,12 +133,14 @@ public class DefaultSerializer implements Serializer { builder.time(Long.parseLong((String) time), precision); break; case TIMESTAMP: + // The value below is epoch milliseconds, so it must be declared + // as milliseconds no matter what precision the epoch option uses. builder.time( ((LocalDateTime) time) .atZone(ZoneOffset.UTC) .toInstant() .toEpochMilli(), - precision); + TimeUnit.MILLISECONDS); break; case BIGINT: builder.time((Long) time, precision); diff --git a/seatunnel-connectors-v2/connector-influxdb/src/test/java/org/apache/seatunnel/connectors/seatunnel/influxdb/serialize/DefaultSerializerTest.java b/seatunnel-connectors-v2/connector-influxdb/src/test/java/org/apache/seatunnel/connectors/seatunnel/influxdb/serialize/DefaultSerializerTest.java index 2da6ff6cfb..964ffbd86c 100644 --- a/seatunnel-connectors-v2/connector-influxdb/src/test/java/org/apache/seatunnel/connectors/seatunnel/influxdb/serialize/DefaultSerializerTest.java +++ b/seatunnel-connectors-v2/connector-influxdb/src/test/java/org/apache/seatunnel/connectors/seatunnel/influxdb/serialize/DefaultSerializerTest.java @@ -27,6 +27,7 @@ import org.influxdb.dto.Point; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; +import java.time.Instant; import java.time.LocalDateTime; import java.util.Collections; import java.util.concurrent.TimeUnit; @@ -35,13 +36,35 @@ public class DefaultSerializerTest { private static final String MEASUREMENT = "cpu"; + /** 2024-01-01T00:00:00 in UTC, as epoch milliseconds. */ + private static final long EPOCH_MILLIS_2024_01_01 = 1704067200000L; + + private static final long EPOCH_MILLIS_2020_01_01 = 1577836800000L; + private DefaultSerializer serializer(SeaTunnelDataType<?> timeType) { + return serializer(timeType, TimeUnit.NANOSECONDS); + } + + private DefaultSerializer serializer(SeaTunnelDataType<?> timeType, TimeUnit precision) { SeaTunnelRowType rowType = new SeaTunnelRowType( new String[] {"time", "value"}, new SeaTunnelDataType[] {timeType, BasicType.DOUBLE_TYPE}); return new DefaultSerializer( - rowType, TimeUnit.NANOSECONDS, Collections.emptyList(), "time", MEASUREMENT); + rowType, precision, Collections.emptyList(), "time", MEASUREMENT); + } + + /** + * Reads the point timestamp from the line protocol. {@link Point#lineProtocol()} always formats + * the timestamp as nanoseconds, so the last token is the timestamp in nanoseconds. + */ + private static long emittedTimestampNanos(Point point) { + String lineProtocol = point.lineProtocol(); + Assertions.assertNotNull(lineProtocol); + String[] tokens = lineProtocol.split("\\s+"); + Assertions.assertTrue( + tokens.length >= 2, "line protocol does not contain a timestamp: " + lineProtocol); + return Long.parseLong(tokens[tokens.length - 1]); } @Test @@ -103,12 +126,33 @@ public class DefaultSerializerTest { SeaTunnelRowType rowType = new SeaTunnelRowType( new String[] {"value"}, new SeaTunnelDataType[] {BasicType.DOUBLE_TYPE}); + // default epoch (nanoseconds) of the influxdb sink DefaultSerializer serializer = new DefaultSerializer( rowType, TimeUnit.NANOSECONDS, Collections.emptyList(), null, MEASUREMENT); + + long before = System.currentTimeMillis(); Point point = serializer.serialize(new SeaTunnelRow(new Object[] {1.0d})); + long after = System.currentTimeMillis(); + Assertions.assertNotNull(point); - Assertions.assertNotNull(point.lineProtocol()); + long timestampMillis = TimeUnit.NANOSECONDS.toMillis(emittedTimestampNanos(point)); + Assertions.assertTrue( + timestampMillis >= before && timestampMillis <= after, + "processing time timestamp should be the current time in milliseconds but was " + + timestampMillis + + " (" + + Instant.ofEpochMilli(timestampMillis) + + "), expected it to be within [" + + before + + ", " + + after + + "]"); + // the millisecond value must not be written as if it were nanoseconds (1970-01-01) + Assertions.assertTrue( + timestampMillis > EPOCH_MILLIS_2020_01_01, + "processing time timestamp must not be a 1970 timestamp but was " + + timestampMillis); } @Test @@ -117,6 +161,26 @@ public class DefaultSerializerTest { LocalDateTime time = LocalDateTime.of(2024, 1, 1, 0, 0, 0); Point point = serializer.serialize(new SeaTunnelRow(new Object[] {time, 1.0d})); Assertions.assertNotNull(point); - Assertions.assertNotNull(point.lineProtocol()); + + // the TIMESTAMP column is converted with UTC to epoch milliseconds; the line protocol + // writes nanoseconds, so the emitted timestamp is the epoch millisecond value * 1_000_000 + Assertions.assertEquals( + TimeUnit.MILLISECONDS.toNanos(EPOCH_MILLIS_2024_01_01), + emittedTimestampNanos(point), + "TIMESTAMP key_time must be written as epoch milliseconds"); + } + + @Test + public void shouldSerializeLocalDateTimeTimeColumnWithMillisecondEpoch() { + // a TIMESTAMP key_time column must be correct for any epoch value, not only for MS + DefaultSerializer serializer = + serializer(LocalTimeType.LOCAL_DATE_TIME_TYPE, TimeUnit.MILLISECONDS); + LocalDateTime time = LocalDateTime.of(2024, 1, 1, 0, 0, 0); + Point point = serializer.serialize(new SeaTunnelRow(new Object[] {time, 1.0d})); + Assertions.assertNotNull(point); + Assertions.assertEquals( + TimeUnit.MILLISECONDS.toNanos(EPOCH_MILLIS_2024_01_01), + emittedTimestampNanos(point), + "TIMESTAMP key_time must be written as epoch milliseconds"); } }
