public class DateTimeCodec extends TypeCodec.AbstractTupleCodec<DateTime>
TypeCodec that maps DateTime 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 ID can be persisted in the varchar field such that when the value
is deserialized the timezone is preserved.
IMPORTANT: this codec's format method formats
timestamps as CQL literal strings using an ISO-8601 format that includes milliseconds.
This format is incompatible with Cassandra versions < 2.0.9.
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.PrimitiveShortCodecdefinition| Constructor and Description |
|---|
DateTimeCodec(TupleType tupleType)
Creates a new
DateTimeCodec for the given tuple, using a default parser and a default
printer to handle the timestamp component of the tuple. |
DateTimeCodec(TupleType tupleType,
DateTimeFormatter parser,
DateTimeFormatter printer)
Creates a new
DateTimeCodec for the given tuple, using the provided parser and printer to format and print the
timestamp component of the tuple. |
| Modifier and Type | Method and Description |
|---|---|
protected DateTime |
deserializeAndSetField(ByteBuffer input,
DateTime target,
int index,
ProtocolVersion protocolVersion) |
protected String |
formatField(DateTime value,
int index) |
protected DateTime |
newInstance() |
protected DateTime |
parseAndSetField(String input,
DateTime target,
int index) |
protected ByteBuffer |
serializeField(DateTime source,
int index,
ProtocolVersion protocolVersion) |
accepts, deserialize, format, parse, serializeaccepts, 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, varintpublic DateTimeCodec(TupleType tupleType)
DateTimeCodec for the given tuple, using a default parser and a default
printer to handle the timestamp component of the tuple.
The default formatter and printer produce and parse CQL timestamp literals of the following form:
Z
(UTC), e.g. 2010-06-30T01:20:47.999Z; note that timestamp components are always
printed in UTC time, hence the zone ID Z.
Note that it is not possible to customize the parsing and printing of the zone component of
the tuple. This codec prints either a zone offset such as -07:00, or a zone ID such as
UTC or Europe/Paris, depending on what is the best 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 DateTimeCodec(TupleType tupleType, DateTimeFormatter parser, DateTimeFormatter printer)
DateTimeCodec for the given tuple, using the provided parser and printer to format and print the
timestamp component of the tuple.
Use this constructor if you intend to customize the way the codec parses and formats timestamps. 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.
Note that it is not possible to customize the parsing and printing of the zone component of
the tuple. This codec prints either a zone offset such as -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>.parser - The parser to use to parse the timestamp component of
the tuple. It should be lenient enough to accept most of the ISO-8601 formats accepted by
Cassandra as valid CQL literals.printer - The printer to use to format the timestamp component
of the tuple. This printer should be configured to always format timestamps in UTC (see
DateTimeFormatter.withZoneUTC().IllegalArgumentException - if the provided tuple type is not a tuple<timestamp,varchar>.protected DateTime newInstance()
newInstance in class TypeCodec.AbstractTupleCodec<DateTime>protected ByteBuffer serializeField(DateTime source, int index, ProtocolVersion protocolVersion)
serializeField in class TypeCodec.AbstractTupleCodec<DateTime>protected DateTime deserializeAndSetField(ByteBuffer input, DateTime target, int index, ProtocolVersion protocolVersion)
deserializeAndSetField in class TypeCodec.AbstractTupleCodec<DateTime>protected String formatField(DateTime value, int index)
formatField in class TypeCodec.AbstractTupleCodec<DateTime>protected DateTime parseAndSetField(String input, DateTime target, int index)
parseAndSetField in class TypeCodec.AbstractTupleCodec<DateTime>Copyright © 2012–2018. All rights reserved.