Skip to content

Cassandra Module

Usage example

This example connects to the Cassandra cluster:

  1. Define a container:

    CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE)
    
  2. Build a CqlSession:

    final CqlSession cqlSession = CqlSession
        .builder()
        .addContactPoint(cassandraContainer.getContactPoint())
        .withLocalDatacenter(cassandraContainer.getLocalDatacenter())
        .build();
    
  3. Define a container with custom cassandra.yaml located in a directory cassandra-auth-required-configuration:

    CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE)
        .withConfigurationOverride("cassandra-auth-required-configuration")
        .withInitScript("initial.cql")
    

Using secure connection (TLS)

If you override the default cassandra.yaml with a version setting the property client_encryption_options.optional to false, you have to provide a valid client certificate and key (PEM format) when you initialize your container:

CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE)
    .withConfigurationOverride("cassandra-ssl-configuration")
    .withSsl("client-ssl/cassandra.cer.pem", "client-ssl/cassandra.key.pem")

Hint

To generate the client certificate and key, please refer to this documentation.

Adding this module to your project dependencies

Add the following dependency to your pom.xml/build.gradle file:

testImplementation "org.testcontainers:testcontainers-cassandra:2.0.1"
<dependency>
    <groupId>org.testcontainers</groupId>
    <artifactId>testcontainers-cassandra</artifactId>
    <version>2.0.1</version>
    <scope>test</scope>
</dependency>