Version Matrix

cassandra-connector

Build Status

Maven Central

Cassandra DB bundle to enable

  • Connection to cassandra
  • Connection to cassandra embedded cassadra for testing
  • Generic materialised repository
  • Managing migrations with Cqlmigrate
Docs?

See Integration tests: IntegrationTests.scala

Uage

Create cassandra-connector.conf in resources directory
cassandra-connector {
  cluster {
    username: user
    password: pass
    keyspace: cassandra_connector
    port: 9402
    contactPoints: localhost
    dc: DC1
  }
  session {
    consistencyLevel: local_quorum
  }
}
Define a repository
@Table(keyspace = "cassandra_connector", name = "users", caseSensitiveKeyspace = false, caseSensitiveTable = false)
class User() {
  @PartitionKey
  @Column(name = "user_id")
  @BeanProperty var userId: UUID = _
  @BeanProperty var name: String = _

  // Define auxiliary constructor due to cassandra driver limitation i.e. using reflection 
  def this(userId: UUID, name: String) = {
    this()
    this.userId = userId
    this.name = name
  }
}
Define a customer Repository Accessor
@Accessor trait TestUserAccessor {
  @Query("SELECT * FROM users") def getAll: Result[TestUser]
  @Query("TRUNCATE users") def truncate: Result[TestUser]
}
Perform CRUD Ops
// Either Create a connected Keyspace from the above config without cql migration
val connectedKeyspace = ConnectedKeyspace("cassandra_connector")

// or with cql migration if needed
val connectedKeyspace = ConnectedKeyspace("cassandra_connector", "aGivenDirectoryWithDotCqlFiles")


//Or alternatively, initialise without config file
import com.github.mideo.cassandra.connector.fluent.Connector

val connectedKeyspace = Connector.keyspace("keyspace" )
      .withUserName("mideo")
      .withPassword("password")
      .withConsistencyLevel(ConsistencyLevel.LOCAL_QUORUM)
      .withContactPoints(List("localhost"))
      .onPort(9402)
      .withDC("DC1")
      .withMigrationsDirectory("aGivenDirectoryWithDotCqlFiles")
      .connect()


// create a connectedTable
val futureTable: Future[ConnectedTable[TestUser, TestUserAccessor]] = connectedKeyspace.materialise[TestUser, TestUserAccessor]


 futureTable map {
      table => table.accessor.truncate
                table.mapper.save(new User(UUID.randomUUID, "mideo"))
                table.accessor.getAll
      } 

// alternatively create a repository
val userMapper: Future[Mapper[TestUser]] = connectedKeyspace.materialise[TestUser]

// Create an instance of repository entity
val mideo = new User(UUID.randomUUID, "mideo")

// Create an instance of repository entity
userMapper.map { _.save(mideo) }

// Get user
userMapper.map { _.get(mideo.userId) }


// Delete user
userMapper.map { _.delete(mideo.userId) }


// alternatively  user custom accessor
val accessor: Future[TestUserAccessor] = connectedKeyspace.materialiseAccessor[TestUserAccessor]

accessor.map { _.getAll }

accessor.map { _.truncate }

Testing with EmbeddedCassandra
// Connect with the ConnectedInMemoryRepository object
val connectedKeyspace: ConnectedKeyspace = ConnectedInMemoryKeyspace("cassandra_connector")

val isRunning = EmbeddedCassandra.isRunning 

val port = EmbeddedCassandra.runningPort 

val hosts = EmbeddedCassandra.getHosts