public class ZonedDateTimeCodec extends TypeCodec.AbstractTupleCodec<ZonedDateTime>
TypeCodec
that maps
ZonedDateTime
to CQL tuple<timestamp,varchar>
,
providing a pattern for maintaining timezone information in
Cassandra.
Since Cassandra's timestamp
type preserves only
milliseconds since epoch, any timezone information
would normally be lost. By using a
tuple<timestamp,varchar>
a timezone can be
persisted in the varchar
field such that when the
value is deserialized the timezone is preserved.
IMPORTANT
1) The default timestamp formatter used by this codec produces CQL literals that may include milliseconds. This literal format is incompatible with Cassandra < 2.0.9.
2) Even if the ISO-8601 standard accepts timestamps with nanosecond precision,
Cassandra timestamps have millisecond precision; therefore, any sub-millisecond
value set on a ZonedDateTime
will be lost when persisted to Cassandra.
TypeCodec.AbstractCollectionCodec<E,C extends Collection<E>>, TypeCodec.AbstractMapCodec<K,V>, TypeCodec.AbstractTupleCodec<T>, TypeCodec.AbstractUDTCodec<T>, TypeCodec.PrimitiveBooleanCodec, TypeCodec.PrimitiveByteCodec, TypeCodec.PrimitiveDoubleCodec, TypeCodec.PrimitiveFloatCodec, TypeCodec.PrimitiveIntCodec, TypeCodec.PrimitiveLongCodec, TypeCodec.PrimitiveShortCodec
definition
Constructor and Description |
---|
ZonedDateTimeCodec(TupleType tupleType)
Creates a new
ZonedDateTimeCodec for the given tuple
and with default formatters for
both the timestamp and the zone components. |
ZonedDateTimeCodec(TupleType tupleType,
DateTimeFormatter dateTimeFormatter,
DateTimeFormatter zoneFormatter)
Creates a new
ZonedDateTimeCodec for the given tuple
and with the provided formatters for
the timestamp and the zone components of the tuple. |
Modifier and Type | Method and Description |
---|---|
protected ZonedDateTime |
deserializeAndSetField(ByteBuffer input,
ZonedDateTime target,
int index,
ProtocolVersion protocolVersion)
Deserialize an individual field and set it on an object, as part of deserializing the whole
object from a CQL tuple (see
TypeCodec.AbstractTupleCodec.deserialize(ByteBuffer, ProtocolVersion) ). |
protected String |
formatField(ZonedDateTime value,
int index)
Format an individual field in an object as a CQL literal, as part of formatting the whole object
(see
TypeCodec.AbstractTupleCodec.format(Object) ). |
protected ZonedDateTime |
newInstance()
Return a new instance of
T . |
protected ZonedDateTime |
parseAndSetField(String input,
ZonedDateTime target,
int index)
Parse an individual field and set it on an object, as part of parsing the whole object
(see
TypeCodec.AbstractTupleCodec.parse(String) ). |
protected ByteBuffer |
serializeField(ZonedDateTime source,
int index,
ProtocolVersion protocolVersion)
Serialize an individual field in an object, as part of serializing the whole object to a CQL
tuple (see
TypeCodec.AbstractTupleCodec.serialize(Object, ProtocolVersion) ). |
accepts, deserialize, format, parse, serialize
accepts, accepts, accepts, ascii, bigint, blob, cboolean, cdouble, cfloat, cint, counter, custom, date, decimal, duration, getCqlType, getJavaType, inet, list, map, set, smallInt, time, timestamp, timeUUID, tinyInt, toString, tuple, userType, uuid, varchar, varint
public ZonedDateTimeCodec(TupleType tupleType)
ZonedDateTimeCodec
for the given tuple
and with default formatters
for
both the timestamp and the zone components.
The default formatters produce and parse CQL timestamp literals of the following form:
Z
(UTC),
e.g. 2010-06-30T02:01Z
or 2010-06-30T01:20:47.999Z
;
note that timestamp components are always expressed in UTC time, hence the zone ID Z
.-07:00
, or a zone ID such as UTC
or Europe/Paris
,
depending on what information is available.tupleType
- The tuple type this codec should handle.
It must be a tuple<timestamp,varchar>
.IllegalArgumentException
- if the provided tuple type is not a tuple<timestamp,varchar>
.public ZonedDateTimeCodec(TupleType tupleType, DateTimeFormatter dateTimeFormatter, DateTimeFormatter zoneFormatter)
ZonedDateTimeCodec
for the given tuple
and with the provided formatters
for
the timestamp and the zone components of the tuple.
Use this constructor if you intend to customize the way the codec parses and formats timestamps and zones. Beware that Cassandra only accepts timestamp literals in some of the most common ISO-8601 formats; attempting to use non-standard formats could result in invalid CQL literals.
tupleType
- The tuple type this codec should handle.
It must be a tuple<timestamp,varchar>
.dateTimeFormatter
- The DateTimeFormatter
to use
to parse and format the timestamp component of the tuple.
As a parser, it should be lenient enough to accept most of the ISO-8601 formats
accepted by Cassandra as valid CQL literals.
As a formatter, it should be configured to always format timestamps in UTC
(see DateTimeFormatter.withZone(java.time.ZoneId)
.zoneFormatter
- The DateTimeFormatter
to use
to parse and format the zone component of the tuple.IllegalArgumentException
- if the provided tuple type is not a tuple<timestamp,varchar>
.protected ZonedDateTime newInstance()
TypeCodec.AbstractTupleCodec
T
.newInstance
in class TypeCodec.AbstractTupleCodec<ZonedDateTime>
T
.protected ByteBuffer serializeField(ZonedDateTime source, int index, ProtocolVersion protocolVersion)
TypeCodec.AbstractTupleCodec
TypeCodec.AbstractTupleCodec.serialize(Object, ProtocolVersion)
).serializeField
in class TypeCodec.AbstractTupleCodec<ZonedDateTime>
source
- The object to read the field from.index
- The index of the field.protocolVersion
- The protocol version to use.null
if that field should be ignored.protected ZonedDateTime deserializeAndSetField(ByteBuffer input, ZonedDateTime target, int index, ProtocolVersion protocolVersion)
TypeCodec.AbstractTupleCodec
TypeCodec.AbstractTupleCodec.deserialize(ByteBuffer, ProtocolVersion)
).deserializeAndSetField
in class TypeCodec.AbstractTupleCodec<ZonedDateTime>
input
- The serialized form of the field.target
- The object to set the field on.index
- The index of the field.protocolVersion
- The protocol version to use.target
, but if you're dealing
with immutable types you'll need to return a different instance.protected String formatField(ZonedDateTime value, int index)
TypeCodec.AbstractTupleCodec
TypeCodec.AbstractTupleCodec.format(Object)
).formatField
in class TypeCodec.AbstractTupleCodec<ZonedDateTime>
value
- The object to read the field from.index
- The index of the field.protected ZonedDateTime parseAndSetField(String input, ZonedDateTime target, int index)
TypeCodec.AbstractTupleCodec
TypeCodec.AbstractTupleCodec.parse(String)
).parseAndSetField
in class TypeCodec.AbstractTupleCodec<ZonedDateTime>
input
- The String to parse the field from.target
- The value to write to.index
- The index of the field.target
, but if you're dealing
with immutable types you'll need to return a different instance.Copyright © 2012–2017. All rights reserved.