Fetching single Column from Cassandra database

package com.examples;

import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;
import org.apache.cassandra.thrift.ColumnParent;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.TimedOutException;
import org.apache.cassandra.thrift.UnavailableException;
import org.apache.thrift.TException;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class CassandraFeatchHInfo {
public static final String UTF8 = “UTF8”;

public static void main(String[] args) throws UnsupportedEncodingException,
InvalidRequestException, UnavailableException, TimedOutException,
TException, NotFoundException {

TTransport tr = new TSocket(“”, 9160);
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);


String keyspace = “Historical_Info”;
String columnFamily = “Historical_Info_Column”;
//String keyUserID = “3”;

// read entire row
SlicePredicate predicate = new SlicePredicate();
SliceRange sliceRange = new SliceRange();
sliceRange.setStart(new byte[0]);
sliceRange.setFinish(new byte[0]);

KeyRange keyrRange = new KeyRange();

ColumnParent parent = new ColumnParent(columnFamily);

List<KeySlice> ls = client.get_range_slices(keyspace, parent, predicate, keyrRange, ConsistencyLevel.ONE);

for (KeySlice result : ls) {
List<ColumnOrSuperColumn> column = result.columns;
for (ColumnOrSuperColumn result2 : column) {
Column column2 = result2.column;
System.out.println(new String(column2.name, UTF8) + ” ->  ” + new String(column2.value, UTF8));


3 thoughts on “Fetching single Column from Cassandra database

  1. Bhalchandra Bhutkar January 10, 2013 / 12:12 pm

    I am getting following errors while running the above code. I am using cassandra 1.1.5 :

    1)The method setStart_key(byte[]) in the type KeyRange is not applicable for the arguments (String)

    2)The method setEnd_key(byte[]) in the type KeyRange is not applicable for the arguments (String)

    3)The method get_range_slices(ColumnParent, SlicePredicate, KeyRange, ConsistencyLevel) in the type Cassandra.Client is not applicable for the arguments (String, ColumnParent, SlicePredicate, KeyRange, ConsistencyLevel)

    4)The constructor String(ByteBuffer, String) is undefined

  2. mallikarjungunda January 10, 2013 / 4:28 pm

    This code has been written using the 0.6 version of cassandra.

    I am not sure whether it works with Cassandra 1.1.5

    • Bhalchandra Bhutkar January 14, 2013 / 8:45 am

      Do you have any example of application using pelops ?

Leave a Reply

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

WordPress.com Logo

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

Facebook photo

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

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.