cassandra:datastax LocalDate类型与java.util.Date不匹配#scala Akka

时间:2017-10-01 13:42:04

标签: scala date cassandra akka datastax

我一直在努力纠正这种类型不匹配问题。我正在从cassandra获得结果集记录。使用的查询是:

CREATE TABLE tweets (
  key text PRIMARY KEY,
  user_user varchar,
  text text,
  createdat timestamp
  ) WITH comment='Tweets'
    AND COMPACT STORAGE
    AND read_repair_chance = 1.0;

cqlsh:akkacassandra> select * from tweets;

 key | createdat | text | user_user
-----+-----------+------+-----------

(0 rows)
cqlsh:akkacassandra>

这是定义类型的代码块:

Tweet.scala

中的某些内容
case class Tweet(id: TweetId, user: User, text: Text, createdAt: Date)

tweetread.scala

中的部分内容
def buildTweet(r: Row): Tweet = {
    val id = r.getString("key")
    val user = r.getString("user_user")
    val text = r.getString("text")
    val createdAt = r.getDate("createdat")
    Tweet(id, user, text, createdAt)
  }

但是在使用sbt运行时我得到了下面的错误。

[info] Loading project definition from C:\Akka Actors\activator-akka-cassandra-master\project
[info] Set current project to activator-akka-cassandra (in build file:/C:/Akka%20Actors/activator-akka-cassandra-master/)
[info] Compiling 8 Scala sources to C:\Akka Actors\activator-akka-cassandra-master\target\scala-2.10\classes...
[error] C:\Akka Actors\activator-akka-cassandra-master\src\main\scala\core\tweetread.scala:29: type mismatch;
[error]  found   : com.datastax.driver.core.LocalDate
[error]  required: java.util.Date
[error]     Tweet(id, user, text, createdAt)
[error]                           ^

我尝试了一些解决方案,例如将r.getDate("createdat")java.util.Date objec包裹起来。但没有运气。任何建议都值得赞赏。谢谢。

1 个答案:

答案 0 :(得分:0)

使用getTimestamp,返回java.util.Date

def buildTweet(r: Row): Tweet = {
  val id = r.getString("key")
  val user = r.getString("user_user")
  val text = r.getString("text")
  val createdAt = r.getTimestamp("createdat") // <---
  Tweet(id, user, text, createdAt)
}