Connecting cassandra cluster through scala

前端 未结 4 1637
深忆病人
深忆病人 2021-02-11 10:40

I am new in both scala and cassandra . I know the connectivity of cassandra with java using hector . But I don\'t know how to connect cassandra through scala. I want a simple ex

4条回答
  •  温柔的废话
    2021-02-11 11:28

    I've been working on Scala wrapper of Java Driver to minimize boilerplate code. You could find it here: https://github.com/InnovaCo/binders-cassandra

    Here is an example of class that works with some Cassandra DB:

    class Db(session: com.datastax.driver.core.Session) {
    
      implicit val cache = new SessionQueryCache[PlainConverter](session)
    
      // class for binding input/output parameters
      case class User(userId: Int, name: String)
    
      def insertUser(user: User): Future[Unit] = cql"insert into users(userid, name) values (?, ?)".bind(user).execute
    
      // returns Future[Iterator[User]]
      def selectAllUsers: Future[Iterator[User]] = cql"select * from users".all[User]
    
      // if no user is found will throw NoRowsSelectedException
      def selectUser(userId: Int) = cql"select * from users where userId = $userId".one[User]
    
      // if no user is found will return None, otherwise Some(User)
      def selectUserIfFound(userId: Int) = cql"select * from users where userId = $userId".oneOption[User]
    }
    

    To use library in your playframework project, add this line to the build.sbt file:

    libraryDependencies += "eu.inn" %% "binders-cassandra" % "0.2.0"
    

提交回复
热议问题