Skip to content

Commit

Permalink
Use test containers for Cassandra examples
Browse files Browse the repository at this point in the history
  • Loading branch information
vietj committed Nov 6, 2024
1 parent 1821f22 commit c56875e
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 18 deletions.
15 changes: 0 additions & 15 deletions cassandra-examples/README.adoc
Original file line number Diff line number Diff line change
@@ -1,20 +1,5 @@
= Vert.x Cassandra Client examples

== Start Cassandra instance

In order to run this examples you need a Cassandra instance running locally.

On the ways to achieve this is to use link:https://github.com/riptano/ccm[ccm].
Follow link:https://github.com/riptano/ccm#installation[the installation instructions] for installing `ccm`.

After `ccm` installation you can run a single node Cassandra cluster:

[source,bash]
----
ccm create test_node -v 3.11.6 -n 1 -s
ccm start test_node
----

== Examples

=== Simple
Expand Down
5 changes: 5 additions & 0 deletions cassandra-examples/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,10 @@
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.testcontainers</groupId>
<artifactId>cassandra</artifactId>
<version>${testcontainers.version}</version>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -1,24 +1,36 @@
package io.vertx.example.cassandra.prepared;

import io.vertx.cassandra.CassandraClient;
import io.vertx.cassandra.CassandraClientOptions;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.launcher.application.VertxApplication;
import org.testcontainers.cassandra.CassandraContainer;

public class PreparedExample extends VerticleBase {

private final static CassandraContainer CASSANDRA_CONTAINER = new CassandraContainer("cassandra:3.11.2");

/**
* Convenience method so you can run it in your IDE
*/
public static void main(String[] args) {
CASSANDRA_CONTAINER.start();
VertxApplication.main(new String[]{PreparedExample.class.getName()});
}

private CassandraClient client;

@Override
public Future<?> start() {
client = CassandraClient.createShared(vertx);
CassandraClientOptions options = new CassandraClientOptions()
.addContactPoint(CASSANDRA_CONTAINER.getContactPoint())
.setUsername(CASSANDRA_CONTAINER.getUsername())
.setPassword(CASSANDRA_CONTAINER.getPassword());
options
.dataStaxClusterBuilder()
.withLocalDatacenter("datacenter1");
client = CassandraClient.create(vertx, options);

return client
.prepare("SELECT * from system_schema.tables WHERE keyspace_name = ? ")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,35 @@

import com.datastax.oss.driver.api.core.cql.Row;
import io.vertx.cassandra.CassandraClient;
import io.vertx.cassandra.CassandraClientOptions;
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.launcher.application.VertxApplication;
import org.testcontainers.cassandra.CassandraContainer;

public class SimpleExample extends VerticleBase {

private final static CassandraContainer CASSANDRA_CONTAINER = new CassandraContainer("cassandra:3.11.2");

/**
* Convenience method so you can run it in your IDE
*/
public static void main(String[] args) {
CASSANDRA_CONTAINER.start();
VertxApplication.main(new String[]{SimpleExample.class.getName()});
}

@Override
public Future<?> start() {
CassandraClient client = CassandraClient.createShared(vertx);
CassandraClientOptions options = new CassandraClientOptions()
.addContactPoint(CASSANDRA_CONTAINER.getContactPoint())
.setUsername(CASSANDRA_CONTAINER.getUsername())
.setPassword(CASSANDRA_CONTAINER.getPassword());
options
.dataStaxClusterBuilder()
.withLocalDatacenter("datacenter1");
CassandraClient client = CassandraClient.create(vertx, options);

return client.execute("select release_version from system.local")
.onSuccess(res -> {
Row row = res.one();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,33 @@
import io.vertx.core.Future;
import io.vertx.core.VerticleBase;
import io.vertx.launcher.application.VertxApplication;
import org.testcontainers.cassandra.CassandraContainer;

public class StreamingExample extends VerticleBase {

private final static CassandraContainer CASSANDRA_CONTAINER = new CassandraContainer("cassandra:3.11.2");

/**
* Convenience method so you can run it in your IDE
*/
public static void main(String[] args) {
CASSANDRA_CONTAINER.start();
VertxApplication.main(new String[]{StreamingExample.class.getName()});
}

private CassandraClient client;

@Override
public Future<?> start() {
client = CassandraClient.createShared(vertx, new CassandraClientOptions());
CassandraClientOptions options = new CassandraClientOptions()
.addContactPoint(CASSANDRA_CONTAINER.getContactPoint())
.setUsername(CASSANDRA_CONTAINER.getUsername())
.setPassword(CASSANDRA_CONTAINER.getPassword());
options
.dataStaxClusterBuilder()
.withLocalDatacenter("datacenter1");
client = CassandraClient.create(vertx, options);

return Future.future(p -> {
client
.queryStream("SELECT * from system_schema.tables WHERE keyspace_name = 'system_schema' ")
Expand Down

0 comments on commit c56875e

Please sign in to comment.