Handling of java.time.Instant - What do you guys do?

883 views
Skip to first unread message

Joo Lee

unread,
Jan 6, 2017, 6:38:28 AM1/6/17
to Lagom Framework Users
Hey guys,

How do you guys handle java.time.Instant in Lagom?

What I do right now is when storing, I create a column with BIGINT as a type, and then I insert instant.toEpochMilli() for insertion and I do Instant.ofEpochMilli(timestamp)load it back. 

I think it works but it is bit tedious. How do you guys handle it?

Cheers,

Joo
 

Tim Moore

unread,
Jan 6, 2017, 10:16:45 PM1/6/17
to Joo Lee, Lagom Framework Users
Hi Joo,

Are you using Cassandra? Cassandra has a native timestamp data type: http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cql_data_types_c.html

You can use that to declare the column type.

The built-in mappings from Java types to CQL types are documented here: http://docs.datastax.com/en/developer/java-driver/3.0/manual/#cql-to-java-type-mapping

As you can see there, the default mapping for timestamp is to java.util.Date, however the Cassandra Java driver lets you register custom codecs for translating other data types. They also provide an optional library called "cassandra-driver-extras" that includes custom codecs for some common classes, including java.time.Instant: http://docs.datastax.com/en/developer/java-driver/3.0/manual/custom_codecs/extras/#jdk-8

To use this in your Lagom service, you need to:

  1. Add a dependency on the cassandra-driver-extras library to your project (in build.sbt or pom.xml). Make sure to use the right version to match the cassandra-driver-core version used by your app.
  2. Register the InstantCodec somewhere in your app initialization, probably in the prepare handler for your read-side processor.
The Online Auction app has an example of this (using EnumCodec rather than InstantCodec):

Cheers,
Tim




--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/fe1352d7-6f9b-4e97-a488-c9a8d088521d%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Tim Moore
Senior Engineer, Lagom, Lightbend, Inc.


Joo Lee

unread,
Jan 7, 2017, 9:16:12 PM1/7/17
to Lagom Framework Users, joo.an...@gmail.com
Much appreciated Tim.


On Saturday, January 7, 2017 at 11:16:45 AM UTC+8, Tim Moore wrote:
Hi Joo,

Are you using Cassandra? Cassandra has a native timestamp data type: http://docs.datastax.com/en/cql/3.3/cql/cql_reference/cql_data_types_c.html

You can use that to declare the column type.

The built-in mappings from Java types to CQL types are documented here: http://docs.datastax.com/en/developer/java-driver/3.0/manual/#cql-to-java-type-mapping

As you can see there, the default mapping for timestamp is to java.util.Date, however the Cassandra Java driver lets you register custom codecs for translating other data types. They also provide an optional library called "cassandra-driver-extras" that includes custom codecs for some common classes, including java.time.Instant: http://docs.datastax.com/en/developer/java-driver/3.0/manual/custom_codecs/extras/#jdk-8

To use this in your Lagom service, you need to:

  1. Add a dependency on the cassandra-driver-extras library to your project (in build.sbt or pom.xml). Make sure to use the right version to match the cassandra-driver-core version used by your app.
  2. Register the InstantCodec somewhere in your app initialization, probably in the prepare handler for your read-side processor.
The Online Auction app has an example of this (using EnumCodec rather than InstantCodec):

Cheers,
Tim



On Fri, Jan 6, 2017 at 10:38 PM, Joo Lee <joo.an...@gmail.com> wrote:
Hey guys,

How do you guys handle java.time.Instant in Lagom?

What I do right now is when storing, I create a column with BIGINT as a type, and then I insert instant.toEpochMilli() for insertion and I do Instant.ofEpochMilli(timestamp)load it back. 

I think it works but it is bit tedious. How do you guys handle it?

Cheers,

Joo
 

--
You received this message because you are subscribed to the Google Groups "Lagom Framework Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framewo...@googlegroups.com.
To post to this group, send email to lagom-f...@googlegroups.com.

Joo Lee

unread,
Jan 8, 2017, 9:41:43 AM1/8/17
to Lagom Framework Users, joo.an...@gmail.com
Hello Tim,

I don't find the underlying().thenAccept and thenApply() in scala version of session() object. Is this not yet supported in Scala version?

Thanks,

Joo

Tim Moore

unread,
Jan 8, 2017, 7:17:19 PM1/8/17
to Joo Lee, Lagom Framework Users
Hi, Joo,


The only difference is that it returns a Scala Future, rather than a Java CompletionStage, so you'll need to use the equivalent Scala Future method calls (i.e., map) or a for comprehension.

I haven't tried compiling this, but it would look something like this:

for (s <- session.underlying) yield {
  registerCodec(s, new EnumNameCodec<>(ItemStatus.class))
  Done
}

Where registerCodec is essentially the same as in the Java version, other than syntax differences.

Cheers,
Tim

To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/ce7e92cf-2b7a-418f-9eb0-28946043bd4c%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Joo Lee

unread,
Feb 1, 2017, 7:35:52 AM2/1/17
to Lagom Framework Users, joo.an...@gmail.com, ja...@lightbend.com

Hi Tim / James,

I've been trying quite hard to insert and read the Scala case classes into Cassnadra but without any luck so far.

What I would like to do is 1) insert case class instance  into the column 2) read it back and if possible 3)  use the case class instance to do insert / read for Cassandra's Set / List / Map collection types 

Since I already have JSON Format defined for all the case classes in my project using Play Json, I am thinking there should be a way to achieve them quite easily.

Could you please throw me some light here? 

I found out that although Java Online Auction project uses RegisterCodec to register Enum codec, Scala version just insert Enum.toString and read the string back.
I would do the same if it is only the Enums that I am dealing with, but ideally I would want to store Seq[Order] into Cassandra and read it back and forth!


Thanks,

Joo

Tim Moore

unread,
Feb 7, 2017, 8:14:37 PM2/7/17
to Joo Lee, Lagom Framework Users, Joo Lee, James Roper
Hi Joo,

Could you give us more details about what you've tried and how it hasn't worked?

You'll probably need to write a custom codec for the Cassandra Java Driver http://docs.datastax.com/en/developer/java-driver/3.0/manual/custom_codecs/

That page has an example (in Java) of a JSON codec that uses Jackson. You should be able to implement something similar using Play JSON instead.

Cheers,
Tim

To unsubscribe from this group and stop receiving emails from it, send an email to lagom-framework+unsubscribe@googlegroups.com.
To post to this group, send email to lagom-framework@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/lagom-framework/7eb93de0-0632-4632-a20b-d45f2d91a636%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.
Reply all
Reply to author
Forward
0 new messages