In this Article, I will show How to work with Cassandra list datatype.
How to retrieve, insert, delete and update data.
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
When do we use map data type?
1) When order of the elements matters, the order may not be the natural
order dictated by the type of the elements.
List datatype will ensure that adding elements order. The order we add the elements
into list while retrieving the same order we can get from list.
Setup required data :
Start Cassandra server and CQL prompt.
Create a table with name UserOrders.
Syntax is,
CREATE TABLE UserOrders ( user_id text PRIMARY KEY, first_name text, last_name text, dispatchAddress text, amount int, order_ids set<text> ); ALTER TABLE UserOrders ADD orders list<Order>; ALTER TABLE UserOrders ADD order_history map<text, timestamp>;
Steps to be follow :
1) Create a simple maven project.
2) Add the dependencies.
3) Write a simple program to retrieve, insert, delete and update Map 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 org.springframework.data spring-data-cassandra 1.2.1.RELEASE
Write a simple program to work with Cassandra list datatype :
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);
Retrieve Data :
Using select() method of CassandraTemplate class, we can select single
or multiple records from Cassandra Database.
// To select a single User information from Database final String[] columns = new String[] { "user_id", "first_name", "last_name", "dispatchAddress", "amount", "order_ids", "routing_point" }; Select select = QueryBuilder.select(columns).from("UserOrders"); select.where(QueryBuilder.eq("user_id", "User_1101")); final List<UserOrders> results = cassandraOps.select(select, UserOrders.class); // To select multiple User information at a time. // Bulk select operation Select allUserSelect = QueryBuilder.select(columns).from( "UserOrders"); final List<UserOrders> allUsers = cassandraOps.select( allUserSelect, UserOrders.class);
Insert Data :
Insert into UserOrders table,
// To insert into UserOrders table Listrouting_points = Arrays.asList("AUE", "SAN", "UK", "LON"); UserOrders userOrders = new UserOrders("User_1102", "Mike", "Wen", "21/4 11 L 12 St Downtown OAK", 200, orderSet, routing_points); cassandraOps.insert(userOrders);
Delete Data :
Delete entire UserOrders object or only routing points list from UserOrders table
// This is to delete entire UserOrders object cassandraOps.delete(userOrders); // This is to delete Orders from UserOrders tables userOrders.setrouting_point(new ArrayList<String>()); cassandraOps.update(userOrders);
Update Data :
Update Only routing point list of UserOrders table,
userOrders.setrouting_point(Arrays.asList("AUE_Updated", "SAN_Updated", "UK_Updated", "LON_Updated")); cassandraOps.update(userOrders);
Complete Source code is Here,
App.java
package com.devjavasource.cassandra.SpringDataCassandraExample; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; import java.util.List; import java.util.Set; 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.UserOrders; 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); // To select a single User information from Database final String[] columns = new String[] { "user_id", "first_name", "last_name", "dispatchAddress", "amount", "order_ids", "routing_point" }; Select select = QueryBuilder.select(columns).from("UserOrders"); select.where(QueryBuilder.eq("user_id", "User_1101")); final List<UserOrders> results = cassandraOps.select(select, UserOrders.class); System.out.println("***********RETRIEVE OPERATION DEMO*******"); printList(results); // To insert into UserOrders table @SuppressWarnings({ "rawtypes", "unchecked" }) Set<String> orderSet = new HashSet(Arrays.asList("order_id101", "order_id102", "order_id103")); List<String> routing_points = Arrays.asList("AUE", "SAN", "UK", "LON"); UserOrders userOrders = new UserOrders("User_1102", "Mike", "Wen", "21/4 11 L 12 St Downtown OAK", 200, orderSet, routing_points); cassandraOps.insert(userOrders); System.out.println("***********INSERT OPERATION DEMO*******"); printit(columns, cassandraOps); userOrders.setrouting_point(Arrays.asList("AUE_Updated", "SAN_Updated", "UK_Updated", "LON_Updated")); cassandraOps.update(userOrders); System.out.println("***********UPDATE OPERATION DEMO*******"); printit(columns, cassandraOps); userOrders.setrouting_point(new ArrayList<String>()); cassandraOps.update(userOrders); System.out .println("***********DELETE OPERATION DEMO - To Null Set*******"); printit(columns, cassandraOps); cassandraOps.delete(userOrders); System.out.println("***********DELETE OPERATION DEMO*******"); printit(columns, cassandraOps); } catch (Exception e) { e.printStackTrace(); } finally { cluster.close(); } } private static void printit(final String[] columns, final CassandraOperations cassandraOps) { // To select multiple User information at a time. // Bulk select operation Select allUserSelect = QueryBuilder.select(columns).from("UserOrders"); final List<UserOrders> allUsers = cassandraOps.select(allUserSelect, UserOrders.class); printList(allUsers); } private static void printList(final List<UserOrders> inResults) { for (UserOrders userOrders : inResults) { System.out.println("User Id is: " + userOrders.getUser_id()); System.out.println("First Name is: " + userOrders.getFirst_name()); System.out.println("Last Name is: " + userOrders.getLast_name()); System.out.println("User Dispatch Address is: " + userOrders.getDispatchAddress()); System.out.println("Total Amount is: " + userOrders.getAmount()); System.out.println("Ordered Ids is: " + userOrders.getOrder_ids()); System.out.println("Routing Points is: " + userOrders.getrouting_point() + "\n"); } } }
UserOrders.java
package com.devjavasource.cassandra.dto; import java.util.List; import java.util.Set; import org.springframework.data.cassandra.mapping.PrimaryKey; import org.springframework.data.cassandra.mapping.Table; @Table public class UserOrders { @PrimaryKey private String user_id; private String first_name; private String last_name; private String dispatchAddress; private int amount; private Set<String> order_ids; private List<String> routing_point; public UserOrders(String user_id, String first_name, String last_name, String dispatchAddress, int amount, Set<String> order_ids, List<String> routing_point) { this.user_id = user_id; this.first_name = first_name; this.last_name = last_name; this.dispatchAddress = dispatchAddress; this.amount = amount; this.order_ids = order_ids; this.routing_point = routing_point; } public String getUser_id() { return user_id; } public String getFirst_name() { return first_name; } public void setFirst_name(String first_name) { this.first_name = first_name; } public String getLast_name() { return last_name; } public void setLast_name(String last_name) { this.last_name = last_name; } public String getDispatchAddress() { return dispatchAddress; } public void setDispatchAddress(String dispatchAddress) { this.dispatchAddress = dispatchAddress; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public Set<String> getOrder_ids() { return order_ids; } public void setOrder_ids(Set<String> order_ids) { this.order_ids = order_ids; } public List<String> getrouting_point() { return routing_point; } public void setrouting_point(List<String> routing_point) { this.routing_point = routing_point; } public void setUser_id(String user_id) { this.user_id = user_id; } }
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 :
***********RETRIEVE OPERATION DEMO******* User Id is: User_1101 First Name is: Bob Last Name is: Wigins User Dispatch Address is: 22/4 11 L 12 St Downtown OAK Total Amount is: 100 Ordered Ids is: [order_id101, order_id102, order_id103] Routing Points is: [SIN, AUS, SAN] ***********INSERT OPERATION DEMO******* User Id is: User_1101 First Name is: Bob Last Name is: Wigins User Dispatch Address is: 22/4 11 L 12 St Downtown OAK Total Amount is: 100 Ordered Ids is: [order_id101, order_id102, order_id103] Routing Points is: [SIN, AUS, SAN] User Id is: User_1102 First Name is: Mike Last Name is: Wen User Dispatch Address is: 21/4 11 L 12 St Downtown OAK Total Amount is: 200 Ordered Ids is: [order_id101, order_id102, order_id103] Routing Points is: [AUE, SAN, UK, LON] ***********UPDATE OPERATION DEMO******* User Id is: User_1101 First Name is: Bob Last Name is: Wigins User Dispatch Address is: 22/4 11 L 12 St Downtown OAK Total Amount is: 100 Ordered Ids is: [order_id101, order_id102, order_id103] Routing Points is: [SIN, AUS, SAN] User Id is: User_1102 First Name is: Mike Last Name is: Wen User Dispatch Address is: 21/4 11 L 12 St Downtown OAK Total Amount is: 200 Ordered Ids is: [order_id101, order_id102, order_id103] Routing Points is: [AUE_Updated, SAN_Updated, UK_Updated, LON_Updated] ***********DELETE OPERATION DEMO - To Null Set******* User Id is: User_1101 First Name is: Bob Last Name is: Wigins User Dispatch Address is: 22/4 11 L 12 St Downtown OAK Total Amount is: 100 Ordered Ids is: [order_id101, order_id102, order_id103] Routing Points is: [SIN, AUS, SAN] User Id is: User_1102 First Name is: Mike Last Name is: Wen User Dispatch Address is: 21/4 11 L 12 St Downtown OAK Total Amount is: 200 Ordered Ids is: [order_id101, order_id102, order_id103] Routing Points is: null ***********DELETE OPERATION DEMO******* User Id is: User_1101 First Name is: Bob Last Name is: Wigins User Dispatch Address is: 22/4 11 L 12 St Downtown OAK Total Amount is: 100 Ordered Ids is: [order_id101, order_id102, order_id103] Routing Points is: [SIN, AUS, SAN]
You can download complete project, Here
*** Venkat – Happy leaning ****