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: dc-eu-west-1
  }
  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
// Create a connected repository
val connectedKeyspace = ConnectedKeyspace.connect("cassandra_connector")


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

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

// Run migrations if required
Await.result(connectedKeyspace.runMigrations("aGivenDirectoryWithDotCqlFiles"), 1 minute)

// Materialise 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) }


// 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.connect("cassandra_connector")

val isRunning = EmbeddedCassandra.isRunning 

val port = EmbeddedCassandra.runningPort 

val hosts = EmbeddedCassandra.getHosts