Skip to content

CQL embedded cassandra server

DuyHai DOAN edited this page Jul 12, 2014 · 16 revisions

Achilles exposes a simple builder to start an embedded Cassandra server and optionally bootstrap the framework.

Such feature is already available for JUnit testing using the AchillesResourceBuilder discussed previously. However there is still a need to start an embedded Cassandra outside of testing context and more importantly, to connect to several keyspaces.

The builder API:

CassandraEmbeddedServerBuilder
  .withEntityPackages("test") || noEntityPackages()
  .withClusterName("Test Cluster")
  .withDataFolder("/home/user/cassandra/data")
  .withCommitLogFolder("/home/user/cassandra/commitlog")
  .withSavedCachesFolder("/home/user/cassandra/saved_caches")
  .cleanDataFilesAtStartup(true)
  .withConfigYamlFile("/home/user/cassandra/cassandra.yaml")
  .withClusterName("Test Cluster")
  .withKeyspaceName("achilles_test") 
  .withCQLPort(9042)
  .withThriftPort(9160)
  .withStoragePort(7990)
  .withStorageSSLPort(7999)
  .withDurableWrite(true)
  .buildNativeSessionOnly() || buildPersistenceManagerFactory() || buildPersistenceManager();

Most of the parameters are optional, below are the default values:

  • clusterName: Achilles Embedded Cassandra Cluster
  • keyspaceName: achilles_embedded
  • dataFolder: target/cassandra_embedded/data
  • commitLogFolder: target/cassandra_embedded/commitlog
  • savedCachesFolder: target/cassandra_embedded/saved_caches
  • cleanDataFilesAtStartup: true
  • configYAMLFile: target/cassandra_embedded/cassandra.yaml
  • cqlPort: randomized at runtime
  • thriftPort: randomized at runtime
  • storagePort: randomized at runtime
  • storageSSLPort: randomized at runtime
  • durableWrite: true

The builder can return:

  1. either an Achilles PersistenceManagerFactory
  2. or an Achilles PersistenceManager
  3. or just a native Session object from the driver

There will be at most one embedded Cassandra server started per JVM (more precisely, per classloader) event if many CassandraEmbeddedServer are built. But because the keyspace name is provided each time, the builder will bootstrap as many PersistenceManager instances or Session objects as distinct keyspaces.

Example is better than words:

PersistenceManagerFactory factory1 = CassandraEmbeddedServerBuilder
  .withEntityPackages("my.entity.package1")
  .withKeyspaceName("keyspace1")
  .buildPersistenceManagerFactory();

PersistenceManagerFactory factory2 = CassandraEmbeddedServerBuilder
  .withEntityPackages("my.entity.package2")
  .withKeyspaceName("keyspace2")
  .buildPersistenceManagerFactory();

PersistenceManagerFactory factory3 = CassandraEmbeddedServerBuilder
  .withEntityPackages("my.entity.package1")
  .withKeyspaceName("keyspace1")
  .buildPersistenceManagerFactory();

assertThat(factory1).isNotEqualTo(factory2);
assertThat(factory1).isEqualTo(factory3);

Home

Clone this wiki locally