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(“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
3 comments on “Fetching single Column from Cassandra database
  1. Bhalchandra Bhutkar says:

    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. This code has been written using the 0.6 version of cassandra.

    I am not sure whether it works with Cassandra 1.1.5

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 )

Connecting to %s

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

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
  • 284,397 hits
IndiBlogger
malliktalksjava.wordpress.c...
70/100