Spring Data Cassandra hello world example

In this Article, I will show How to start quick development with
Spring Data Cassandra. How to create a maven project and add required dependencies.
How to retrieve data using Spring Cassandra Template.

Tools Uses :

1) spring-data-cassandra-1.2.1.RELEASE
2) Apache-cassandra-2.1.6
3) eclipse version Luna 4.4.1.
4) Maven 3.3.3
5) JDK 1.6

Steps to be follow :

1) Create a simple maven project.

2) Add the dependencies and

3) Write a simple program to retrieve data using Spring Cassandra Template.

4) Start the Cassandra server.

5) Run the program and verify the data in cassandra.

Add the given dependency to spring-data-cassandra API,


	org.springframework.data
	spring-data-cassandra
	1.2.1.RELEASE

Complete pom.xml file code is Here,
pom.xml


  4.0.0

  com.devjavasource.cassandra
  SpringDataCassandraExample
  0.0.1-SNAPSHOT
  jar

  SpringDataCassandraExample
  http://maven.apache.org

  
    UTF-8
  

  
    
      junit
      junit
      3.8.1
      test
    
    
	  com.datastax.cassandra
	  cassandra-driver-core
	  2.1.6
	
	
    
        org.springframework.data
        spring-data-cassandra
        1.2.1.RELEASE
    
  

Write a simple program to retrieve data using Spring Cassandra Template :

This program will do the following,

1) Create a Cluster object.

Cluster cluster = null;
cluster = Cluster.builder().addContactPoint("127.0.0.1").build();

A cluster object maintains a permanent connection to one of
the cluster nodes. builder() is a static method of Cluster class.

2) Create a session object

private static Session session;
session = cluster.connect("devjavasource");

Here “devjavasource” is the existed keyspace, I am using the same.
we can create a session object by passing key space name as parameter
to connect() method of cluster class.

3) Create CassandraOperations :

CassandraOperations is interface that helps to do Operations like select, insert, delete…etc
for interacting with Cassandra.

CassandraOperations cassandraOps = new CassandraTemplate(session);

We can create query using QueryBuilder class.

// List of columns, that has to be selected from table users
final String[] columns = new String[] { "id", "address", "name" };
// Select query creation with QueryBuilder class
Select select = QueryBuilder.select(columns).from("users");
select.where(QueryBuilder.eq("id", 11101));
//Execute a query and get the results
final List<User> results = cassandraOps.select(select, User.class);

Complete Source code is Here,
App.java

package com.devjavasource.cassandra.SpringDataCassandraExample;

import java.util.List;

import org.springframework.data.cassandra.core.CassandraOperations;
import org.springframework.data.cassandra.core.CassandraTemplate;

import com.datastax.driver.core.Cluster;
import com.datastax.driver.core.Session;
import com.datastax.driver.core.querybuilder.QueryBuilder;
import com.datastax.driver.core.querybuilder.Select;
import com.devjavasource.cassandra.dto.User;

public class App {
	private static Cluster cluster;
	private static Session session;

	public static void main(String[] args) {

		try {

			cluster = Cluster.builder().addContactPoint("127.0.0.1").build();

			session = cluster.connect("devjavasource");

			CassandraOperations cassandraOps = new CassandraTemplate(session);

			final String[] columns = new String[] { "id", "address", "name" };

			Select select = QueryBuilder.select(columns).from("users");
			select.where(QueryBuilder.eq("id", 11101));

			final List<User> results = cassandraOps.select(select, User.class);

			System.out.println("Spring Data Cassandra Example");
			System.out.println("==============================");

			for (User user : results) {
				System.out.println("User Id is: " + user.getId());
				System.out.println("User Address is: " + user.getAddress());
				System.out.println("User Name is: " + user.getName());
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			cluster.close();
		}
	}
}

User.java

package com.devjavasource.cassandra.dto;

import org.springframework.data.cassandra.mapping.PrimaryKey;
import org.springframework.data.cassandra.mapping.Table;

@Table
public class User {
	@PrimaryKey
	private int id;

	private String address;
	private String name;

	public User(int id, String address, String name) {
		this.id = id;
		this.address = address;
		this.name = name;
	}

	public int getId() {
		return id;
	}

	public String getAddress() {
		return address;
	}

	public String getName() {
		return name;
	}

	@Override
	public String toString() {
		return "User [id=" + id + ", address=" + address + ", name=" + name
				+ "]";
	}
}

4) Start the Cassandra server :

Cassandra server should be up and running.
If the server is not running, run the server using following command.

Command to start Casandra server is,
C:\apache-cassandra-2.1.6\bin>cassandra.bat -f

5) Run Maven project :

Select and Run As -> Java Application.

Out Put :

Spring Data Cassandra Example
==============================
User Id is: 11101
User Address is: Oakland
User Name is: john

Verify the data in cassandra :

Start cqlsh shell and use key space “devjavasource” and query
for the table Users.

1

You can download complete project, Here

SpringDataCassandraExample

*** Venkat – Happy leaning ****