1

私はこのコードを書きました

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()
  }
}

しかし、これをコンパイルするとエラーが発生します。

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.

他のエラーも表示されます

[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
4

1 に答える 1

1

ID、性別、年齢などを列として定義したので、次のように行から値を抽出する必要があります。

def fromRow(row: Row) : User = {
  User(
    id(row),
    gender(row),
    age(row),
    occupation(row),
    zipCode(row)
  )
}
于 2016-02-29T04:59:14.147 に答える