1
votes

I have written this code

case class User(id: Int, gender: String, age: Int, occupation: String, zipCode: String)

object Defaults {
  val hosts = Seq("172.17.0.9")
  val Connector = ContactPoints(hosts).keySpace("Movies")
}

class MyDatabase(val keyspace: KeySpaceDef) extends com.websudos.phantom.db.DatabaseImpl(keyspace) {
  object users extends Users with keyspace.Connector
}

object MyDatabase extends MyDatabase(Defaults.Connector)
class Users extends CassandraTable[Users, User] {
  object id extends IntColumn(this) with PartitionKey[Int]
  object age extends IntColumn(this) with Index[Int]
  object gender extends StringColumn(this) with Index[String]
  object occupation extends StringColumn(this) with Index[String]
  object zipCode extends StringColumn(this) with Index[String]

  def fromRow(row: Row) : User = {
    User(
      row(id),
      row(gender),
      row(age),
      row(occupation),
      row(zipCode)
    )
  }
}

object Users extends Users with RootConnector {
  def store(user: User) : Future[ResultSet] = {
    insert
      .value(_.id, user.id)
      .value(_.gender, user.gender)
      .value(_.age, user.age)
      .value(_.occupation, user.occupation)
      .value(_.zipCode, user.zipCode)
      .consistencyLevel_=(ConsistencyLevel.ALL)
      .future()
  }

  def getById(id: Int) : Future[Option[User]] = {
    select.where(_.id eqs id).one()
  }
}

But when I compile this I get an errors.

Object creation impossible, since member session: Session in 
com.websudos.phantom.connectors.RootConnector is not defined; member space: 
KeySpace in com.websudos.phantom.connectors.RootConnector is not defined.

I also see other errors

[error] /Users/U/MyProjects/src/main/scala-2.11/com/abhi/MovieLensDataPreperation.scala:180: com.websudos.phantom.dsl.Row does not take parameters
[error]       row(id),
[error]          ^
[error] /Users/U/MyProjects/src/main/scala-2.11/com/abhi/MovieLensDataPreperation.scala:181: com.websudos.phantom.dsl.Row does not take parameters
[error]       row(gender),
[error]          ^
[error] /Users/U/MyProjects/src/main/scala-2.11/com/abhi/MovieLensDataPreperation.scala:182: com.websudos.phantom.dsl.Row does not take parameters
[error]       row(age),
[error]          ^
[error] /Users/U/MyProjects/src/main/scala-2.11/com/abhi/MovieLensDataPreperation.scala:183: com.websudos.phantom.dsl.Row does not take parameters
[error]       row(occupation),
[error]          ^
[error] /Users/U/MyProjects/src/main/scala-2.11/com/abhi/MovieLensDataPreperation.scala:184: com.websudos.phantom.dsl.Row does not take parameters
[error]       row(zipCode)
[error]          ^
[error] 5 errors found
[error] (compile:compileIncremental) Compilation failed
1
I think you should read my tutorial again, you are not following the structure of the DSL and you are not providing a session as you should. You are also not using the Database implementation which is really helpful. Minor things, but going through this will help you get up to speed. Your Users object should be a class called ConcreteUsers and that's what your users field under MyDatabase should extend, otherwise you will have none of the methods. websudos.com/blog/post/… - flavian
Thanks Flavian. I changed my implementation based on the code here github.com/thiagoandrade6/cassandra-phantom/blob/master/src/… Now its much better. However I have another holdup. stackoverflow.com/questions/35692508/… - Knows Not Much
I've answered that one too. - flavian
Also, read my introduction to Cassandra indexes: outworkers.com/blog/post/…. Using so many Indexes is really really bad for performance. - flavian

1 Answers

1
votes

Since you've defined id, gender, age, etc as columns, you should extract values from row like this:

def fromRow(row: Row) : User = {
  User(
    id(row),
    gender(row),
    age(row),
    occupation(row),
    zipCode(row)
  )
}