0

I am using phantom 1.29.4 and scala 2.11.8, trying to do hands on cassandra with scala. I have my datamodel like below...

    case class User(id: Long, name: String, createdDate: Timestamp, ...)
    class UserTableMapping extends CassandraTable[UserTableDao, User] {
        ...
        object createdDate extends DateTimeColumn(this)
        ...
    }

    abstract class UserTableDao extends UserTableMapping with RootConnector {
        def createUser(user: User) = insert.value...(_.createdDate, user.createdDate)
        ...
    }

Now, I am getting a type mismatch error ("expected com.websudos.phantom.dsl.DateTime actual java.sql.Timestamp" which is obvious)... Now my question is how do I convert Timestamp to DateTime (because, I have my service layer in different sub project and I don want to add all phantom dsl jars there) or provide current time to Datetime?

I have also tried providing a implicit conversion like below...

    implicit def sqlTimestampToPhantomDateTime(dt: Timestamp): DateTime = new DateTime(dt)

but still no luck...

Please help me guys as I am new to cassandra... Thanks...

4

2 回答 2

4

另一个答案是正确的,说 phantom 默认只使用 Joda Time,但它引入了一个相当危险的建议,即使用new DateTime()空参数构造函数,它将使用它正在执行的机器的本地时区。

默认情况下,phantomDateTimeZone.UTC在从 Cassandra 解析回来时强制执行 a,因为 Cassandra 只处理该timestamp类型的 UTC 时间。

因此,您必须使用new Datetime(time, DateTimeZone.UTC)以确保从 Cassandra 返回的内容与您输入的内容相同。

于 2016-10-12T19:29:50.613 回答
1

那只是一个joda DateTime

type DateTime = org.joda.time.DateTime

joda DateTime 有一个毫秒构造函数,所以你快到了。您需要做的就是从您的Timestamp实例中获取以毫秒为单位的时间戳,并使用它来构造一个DateTime实例:

new DateTime(timestampInstance.getTime, DateTimeZone.UTC)

但是,您也可以创建一个新的 DateTime 实例来获取当前时间:

new DateTime(DateTimeZone.UTC)

编辑:对于将来阅读本文的任何人,@flavian 对幻象如何处理时区提出了一个有效的观点,我编辑了这个以反映它。

于 2016-10-12T16:56:45.000 回答