Cassandra Module
Usage example
This example connects to the Cassandra cluster:
-
Define a container:
CassandraContainer cassandraContainer = new CassandraContainer(CASSANDRA_IMAGE) -
Build a
CqlSession:final CqlSession cqlSession = CqlSession .builder() .addContactPoint(cassandraContainer.getContactPoint()) .withLocalDatacenter(cassandraContainer.getLocalDatacenter()) .build(); -
Define a container with custom
cassandra.yamllocated in a directorycassandra-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>