如何将 java.time.Duration 映射到类型为 time 的 Cassandra DB 列
How do I map a java.time.Duration to a Cassandra DB column with type time
目标:
将 java.time.Duration 从 JDK8+ 映射到类型为时间的 Cassandra DB 列。
情况:
我找不到能够做到这一点的任何标准映射或任何编解码器(即使在具有额外编解码器的 Maven 依赖项中)。
这是一个 java 8 maven 项目,带有 Cassandra DB 的 datastax 驱动程序。
问题:
有人知道这个或任何编解码器的可行解决方案吗?如果不是,这是通过 java.sql.time 进行变通的最佳方法还是可能?
如果我忘记了一些主要细节,请在下面评论。
tl;博士
使用 Cassandra 类型 duration
而不是 time
。
使用 java.time.Duration
class 传递或解析标准 ISO 8601 字符串。
Duration.parse( "PT1H30M" ) // Parse ISO 8601 string into a `Duration` object.
.toString() // Generate a ISO 8601 string from the `Duration` object.
Duration
不是时间
java.time.Duration
class 表示未附加到时间轴的时间跨度。它 而不是 表示时间。
Cassandra time
类型定义为:
Value is encoded as a 64-bit signed integer representing the number of nanoseconds since midnight. Values can be represented as strings, such as 13:30:54.234.
因此您不应使用 Cassandra time
类型存储 Duration
值。
您还有其他选择。
一个选项是存储标准 ISO 8601 textual representation of a duration:PnYnMnDTnHnMnS
。示例:一个半小时为 PT1H30M
。此选项更真实,但无法在 Cassandra 中比较或排序值,因为它们只是文本,没有按时间顺序按字母顺序排列。另外,请参阅本答案下面的“Cassandra 持续时间”部分。
另一个选项是提取内部使用的两个数字 Duration
:
- 一个 64 位整数,整秒数。
- 一个 32 位整数,小数秒中纳秒的计数。
如果你坚持,你可以使用这些数字生成一个值来存储为 Cassandra time
,但你会违反语义。
long fakeNanosSinceMidnight = TimeUnit.SECONDS.toNanos( duration.getSeconds ) + duration.getNano() ; // NOT recommended, but if you insist on abusing Cassandra semantics, here you go.
卡桑德拉Duration
Cassandra documentation says 内置了一个 duration
类型。我不是 Cassandra 的用户,所以我不能保证。
文档声明它接受标准 ISO 8601 持续时间字符串作为输入。 Java 中的 Duration::toString
方法生成这样的字符串,而 Duration::parse
解析相同的字符串。
String inputDurationForCassandra = duration.toString() ;
并解析。
Duration duration = Duration.parse( myCassandraDurationString ) ;
请注意 Java 使用 Duration
class for days (generic 24-hour chunks, not calendar days), hours, minutes, and seconds. The Period
class represents years, months, days. It usually does not make sense to conjoin the two concepts. But if you insist, you can use the PeriodDuration
class found in the ThreeTen-Extra 项目库。
目标:
将 java.time.Duration 从 JDK8+ 映射到类型为时间的 Cassandra DB 列。
情况:
我找不到能够做到这一点的任何标准映射或任何编解码器(即使在具有额外编解码器的 Maven 依赖项中)。 这是一个 java 8 maven 项目,带有 Cassandra DB 的 datastax 驱动程序。
问题:
有人知道这个或任何编解码器的可行解决方案吗?如果不是,这是通过 java.sql.time 进行变通的最佳方法还是可能?
如果我忘记了一些主要细节,请在下面评论。
tl;博士
使用 Cassandra 类型 duration
而不是 time
。
使用 java.time.Duration
class 传递或解析标准 ISO 8601 字符串。
Duration.parse( "PT1H30M" ) // Parse ISO 8601 string into a `Duration` object.
.toString() // Generate a ISO 8601 string from the `Duration` object.
Duration
不是时间
java.time.Duration
class 表示未附加到时间轴的时间跨度。它 而不是 表示时间。
Cassandra time
类型定义为:
Value is encoded as a 64-bit signed integer representing the number of nanoseconds since midnight. Values can be represented as strings, such as 13:30:54.234.
因此您不应使用 Cassandra time
类型存储 Duration
值。
您还有其他选择。
一个选项是存储标准 ISO 8601 textual representation of a duration:PnYnMnDTnHnMnS
。示例:一个半小时为 PT1H30M
。此选项更真实,但无法在 Cassandra 中比较或排序值,因为它们只是文本,没有按时间顺序按字母顺序排列。另外,请参阅本答案下面的“Cassandra 持续时间”部分。
另一个选项是提取内部使用的两个数字 Duration
:
- 一个 64 位整数,整秒数。
- 一个 32 位整数,小数秒中纳秒的计数。
如果你坚持,你可以使用这些数字生成一个值来存储为 Cassandra time
,但你会违反语义。
long fakeNanosSinceMidnight = TimeUnit.SECONDS.toNanos( duration.getSeconds ) + duration.getNano() ; // NOT recommended, but if you insist on abusing Cassandra semantics, here you go.
卡桑德拉Duration
Cassandra documentation says 内置了一个 duration
类型。我不是 Cassandra 的用户,所以我不能保证。
文档声明它接受标准 ISO 8601 持续时间字符串作为输入。 Java 中的 Duration::toString
方法生成这样的字符串,而 Duration::parse
解析相同的字符串。
String inputDurationForCassandra = duration.toString() ;
并解析。
Duration duration = Duration.parse( myCassandraDurationString ) ;
请注意 Java 使用 Duration
class for days (generic 24-hour chunks, not calendar days), hours, minutes, and seconds. The Period
class represents years, months, days. It usually does not make sense to conjoin the two concepts. But if you insist, you can use the PeriodDuration
class found in the ThreeTen-Extra 项目库。