Fetching all columns example in Cassandra


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(“192.168.1.204”, 9160);
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);

tr.open();

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]);
predicate.setSlice_range(sliceRange);

KeyRange keyrRange = new KeyRange();
keyrRange.setStart_key(“1”);
keyrRange.setEnd_key(“”);
//keyrRange.setCount(100);

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));
}
}

tr.close();
}
}

Advertisements
About

I born in Good cultural place called India. My family is traditional family. We fallow all Indian traditions.

Tagged with: , ,
Posted in Cassandra, Databases
One comment on “Fetching all columns example in Cassandra
  1. nayan says:

    Can anyone tell me how to compile and run this code ?

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 )

Google+ photo

You are commenting using your Google+ 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 )

w

Connecting to %s

Enter your email address to subscribe to this blog and receive notifications of new posts by email.

Join 678 other followers

Calendar
August 2010
M T W T F S S
« Jul   Sep »
 1
2345678
9101112131415
16171819202122
23242526272829
3031  
Mallikarjun Gunda

This is Mallikarjun Gunda, I am working as a software professional. Blogging is my Passion. This blog contains the posts on Java related technologies and programming languages. I am happy to share my knowledge through the blogs.

Blog Stats
  • 262,785 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100