How to connect Cassandra using Java class
To connect with cassandra from a java program, you need to add some basic dependency to the program. Add the following dependencies to the program. Maven dependencies List:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>3.6.0</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.25</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-simple</artifactId>
<version>1.7.25</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>21.0</version>
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<version>4.1.20.Final</version>
</dependency>
<dependency>
<groupId>com.codahale.metrics</groupId>
<artifactId>metrics-core</artifactId>
<version>3.0.2</version>
</dependency>
Simple java program to connect with cassandra to a keyspace and to retrieve the values of a table
import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;
public class Test {
public static void main(String[] args) {
String serverIp = "127.0.0.1";
String keyspace = "test";
Cluster cluster = Cluster.builder()
.addContactPoints(serverIp)
.build();
Session session = cluster.connect(keyspace);
String cqlStatement = "SELECT * FROM emp";
for (Row row : session.execute(cqlStatement)) {
System.out.println(row.toString());
}
session.close();
}
}
Git Hub Repo for the program as maven project: GitURL
Did you do any research on the matter?
Picking a driver
You need a way to communicate with cassandra, best option is to use a high level API. You have a wide range of choices here but when we look at it from a high level prespective there are really two choices.
- CQL based drivers - Higher level abstraction of what thrift does. Also the newer tool, companies providing support / documentation for cassandra recommend that new cassandra applications are CQL based.
- Thrift based drives - Have access to low level storage, so its easier to get things wrong.
I'll use datastax's CQL driver.
Download and build the driver from datastax's github repo OR use maven and add the following dependencies:
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-core</artifactId>
<version>2.1.3</version>
</dependency>
<dependency>
<groupId>com.datastax.cassandra</groupId>
<artifactId>cassandra-driver-mapping</artifactId>
<version>2.1.2</version>
</dependency>
Picking maven is a good idea, as it will manage all your dependencies for you, but if you dont use maven, at least you will learn about managing jars and reading through stack-traces.
Code
The driver's documentation is coming along nicely. If you get stuck read through it, the documentation contains lots of examples.
I'll use the following two variables throughout the examples.
String serverIP = "127.0.0.1";
String keyspace = "system";
Cluster cluster = Cluster.builder()
.addContactPoints(serverIP)
.build();
Session session = cluster.connect(keyspace);
// you are now connected to the cluster, congrats!
Read
String cqlStatement = "SELECT * FROM local";
for (Row row : session.execute(cqlStatement)) {
System.out.println(row.toString());
}
Create/Update/Delete
// for all three it works the same way (as a note the 'system' keyspace cant
// be modified by users so below im using a keyspace name 'exampkeyspace' and
// a table (or columnfamily) called users
String cqlStatementC = "INSERT INTO exampkeyspace.users (username, password) " +
"VALUES ('Serenity', 'fa3dfQefx')";
String cqlStatementU = "UPDATE exampkeyspace.users " +
"SET password = 'zzaEcvAf32hla'," +
"WHERE username = 'Serenity';";
String cqlStatementD = "DELETE FROM exampkeyspace.users " +
"WHERE username = 'Serenity';";
session.execute(cqlStatementC); // interchangeable, put any of the statements u wish.
Other useful code
Creating a Keyspace
String cqlStatement = "CREATE KEYSPACE exampkeyspace WITH " +
"replication = {'class':'SimpleStrategy','replication_factor':1}";
session.execute(cqlStatement);
Creating a ColumnFamily (aka table)
// based on the above keyspace, we would change the cluster and session as follows:
Cluster cluster = Cluster.builder()
.addContactPoints(serverIP)
.build();
Session session = cluster.connect("exampkeyspace");
String cqlStatement = "CREATE TABLE users (" +
" username varchar PRIMARY KEY," +
" password varchar " +
");";
session.execute(cqlStatement);