Development Etc.

“Maybe it’s about time I expanded the realm of possibilities around here.” – MacGyver

Range Slices In Cassandra With Hector

I thought I’d write up a simple tutorial on how do a simple range slice in Cassandra with Hector for people coming from the relational world.

I’m going to outline the Cassandra version of SELECT col_name_1 FROM table_name LIMIT 100

Step 1) Setup the connection pool and the client

CassandraClientPool pool = CassandraClientPoolFactory.INSTANCE.get();
CassandraClient client = pool.borrowClient("localhost", 9160);

Step 2) Define which Keyspace you want to use. This is equivalent to which DB or Schema.

Keyspace keyspace = client.getKeyspace("Keyspace1");

Step 3) Setup the slice predicate by defining which columns within the row you want returned. This is equivalent to SELECT col_name_1

// just look for one column for now
List<byte[]> colNames = new ArrayList<byte[]>();

// setup the slice predicate
SlicePredicate sp = new SlicePredicate();

Step 4) Specify which Column Family to use. This is equivalent to a table or FROM table_name in SQL

// setup column parent (CF)
ColumnParent cp = new ColumnParent("Standard3");

Step 5) Execute the request. This is equivalent to executing a SQL query.

// get all the keys with a limit of 100 values returned
Map<String, List<Column>> map = keyspace.getRangeSlice(cp, sp, "", "", 100);

Note that the 3rd and 4th method args are empty strings. When you combine two empty strings for the start and end range, it is like saying SELECT col_name_1 FROM table_name without a WHERE clause.
Also if you have records that were deleted, the List of Column objects will be empty but the key will still be returned until is GC’d by Cassandra.

This example was used against a single Cassandra node using the Random Partitioner. Results will be returned to you in an unordered fashion.


One response to “Range Slices In Cassandra With Hector

  1. mallikarjungunda August 18, 2010 at 2:46 am


    I have a requirement of using Cassandra in my application. In my application there is one table with lot of data and most of my application uses that table. Due to lot of data,performance of the application is decreasing when i use that table is in Oracle.

    So, I have decided to use the Cassandra database for that one table and all other tables in oracle. Lot of business logic is dependent on that table.

    No my question is, Can I use the Cassandra for a table which has lot of business logic.

    I am unable to implement lot of where clauses for Cassandra database.

    Is there any supporting tool to use Cassandra in an efficient way?

    Please let me know…
    i am in urgency..

    Thanks in advance

    By Mallik

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: