Click here to edit subtitle


Post Reply
Forum Home > Bigdata Learnings( Hadoop, HBase,Hive and other bigdata technologies) > Cassandra:Read Data from table using Thrift API

Sourav Gulati
Site Owner
Posts: 83

Here is an example Cassandra Table(column Family) "hbtable" in "mykeyspace" :


cqlsh:mykeyspace>select * from hbtable;

rowkey | cf | quantifier | value


row-2 | colfam1 | quan2 | val2

row-1 | colfam1 | quan1 | val1

 Lets read a line from this using Cassandra Thrift API:

public class SimpleReadThrift {

private static final String HOST = "";

private static final int PORT = 9160;

private static final ConsistencyLevel CL = ConsistencyLevel.ONE;

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

TTransport tr = new TSocket(HOST,PORT);

TFramedTransport tf = new TFramedTransport(tr);

TProtocol proto = new TBinaryProtocol(tf);

Cassandra.Client client = new Cassandra.Client(proto);;


String cfName="hbtable";

ColumnParent cp = new ColumnParent(cfName);

SlicePredicate predicate = new SlicePredicate();

predicate.setSlice_range(new SliceRange(ByteBuffer.wrap(new byte[0]),ByteBuffer.wrap(new byte[0]), false, 3));

//Specifying how many columns to read

List results = client.get_slice(ByteBuffer.wrap("row-2".getBytes()), cp, predicate, CL); // Reading row-2

for (ColumnOrSuperColumn result : results)


Column column = result.column;

System.out.print(new String(column.getName())+" "+new String(column.getValue()) );







Click here for Other topics of BigData Technologies



March 5, 2013 at 12:33 PM Flag Quote & Reply

You must login to post.