datastax java driver example

Emerald 112 Published: 02/11/2025

datastax java driver example

Here's an example of using the DataStax Java Driver to interact with a Cassandra cluster:

Step 1: Add dependencies

In your Maven or Gradle project, add the following dependencies:


com.datastax.driver

driver-core

2.13.0

dependencies {

implementation 'com.datastax.driver:driver-core:2.13.0'

}

Step 2: Create a Cassandra cluster instance

Create an instance of Cluster and specify the contact points (nodes) in your Cassandra cluster:

import com.datastax.driver.core.Cluster;

import com.datastax.driver.core.ClusterBuilder;

Cluster cluster = Cluster.builder()

.addContactPoint("localhost")

.build();

Step 3: Create a session

Create an instance of Session using the cluster instance:

import com.datastax.driver.core.Session;

Session session = cluster.connect();

Step 4: Execute CQL queries

Use the session instance to execute Cassandra Query Language (CQL) queries. For example, to create a keyspace and table:

String cql = "CREATE KEYSPACE IF NOT EXISTS my_keyspace WITH replication = {'class': 'SimpleStrategy', 'replication_factor': 3};";

session.execute(cql);

cql = "CREATE TABLE IF NOT EXISTS my_keyspace.my_table (id int PRIMARY KEY, name text);";

session.execute(cql);

Step 5: Insert data

Use the session instance to insert data into your table:

String cql = "INSERT INTO my_keyspace.my_table (id, name) VALUES (1, 'John');";

session.execute(cql);

cql = "INSERT INTO my_keyspace.my_table (id, name) VALUES (2, 'Jane');";

session.execute(cql);

Step 6: Retrieve data

Use the session instance to retrieve data from your table:

String cql = "SELECT * FROM my_keyspace.my_table;";

ResultSet result = session.execute(cql);

while(result.hasNext()) {

Row row = result.next();

int id = row.getInt("id");

String name = row.getString("name");

System.out.println("ID: " + id + ", Name: " + name);

}

Step 7: Close the session

Close the session instance to release system resources:

session.close();

cluster.close();

This example demonstrates the basic steps of using the DataStax Java Driver to interact with a Cassandra cluster. You can customize and extend this example to fit your specific use case.

Additional Tips:

Make sure to handle exceptions properly when interacting with Cassandra. Use prepared statements for better performance and security. Use the executeAsync() method to execute CQL queries asynchronously, which can be useful in distributed systems. The DataStax Java Driver supports both synchronous and asynchronous programming models.

datastax java driver example

Here's an example of using the DataStax Java Driver for Apache Cassandra:

Step 1: Add dependencies

To use the DataStax Java Driver, you'll need to add the following dependencies to your pom.xml file (if you're using Maven) or your build.gradle file (if you're using Gradle):

For Maven:


com.datastax.driver

driver-core

3.6.0

For Gradle:

dependencies {

implementation 'com.datastax.driver:driver-core:3.6.0'

}

Step 2: Create a Cassandra configuration object

Create a CassandraConfiguration object that specifies the connection settings for your Cassandra cluster:

import com.datastax.driver.core.Cluster;

import com.datastax.driver.core.ClusterBuilder;

Cluster cluster = Cluster.builder()

.addContactPoint("localhost")

.withPort(9042)

.build();

Step 3: Create a session object

Create a Session object that will be used to execute queries:

import com.datastax.driver.core.Session;

Session session = cluster.connect();

Step 4: Execute a query

Use the Session object to execute a query:

// Create a table named "users" with columns "id", "name", and "email"

String createTableQuery = "CREATE TABLE users (id int PRIMARY KEY, name text, email text)";

session.execute(createTableQuery);

// Insert data into the "users" table

String insertQuery = "INSERT INTO users (id, name, email) VALUES (1, 'John Doe', '[email protected]')";

session.execute(insertQuery);

Step 5: Query the data

Use the Session object to query the data:

// Retrieve all rows from the "users" table

ResultSet result = session.execute("SELECT * FROM users");

// Print out the results

for (Row row : result) {

System.out.println("ID: " + row.getInt(0));

System.out.println("Name: " + row.getString(1));

System.out.println("Email: " + row.getString(2));

}

This example demonstrates the basic steps for using the DataStax Java Driver to connect to a Cassandra cluster, create a table, insert data, and query the data. You can modify this code to suit your specific use case.

Note that this is just a simple example to illustrate the basic usage of the driver. In a real-world application, you would likely want to handle errors more robustly, implement connection pooling, and add other features to make your application production-ready.