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);
Listls = client.get_range_slices(keyspace, parent, predicate, keyrRange, ConsistencyLevel.ONE);
for (KeySlice result : ls) {
Listcolumn = 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();
}
}
Showing posts with label Apache Cassandra. Show all posts
Showing posts with label Apache Cassandra. Show all posts
Thursday, August 19, 2010
Fetch All the Records From Cassandra database exmaple
Java, J2EE, Struts, Hibernate, Web Services, JSF
Apache Cassandra,
Cassandra Examples,
Fetch All the Records From Cassandra database exmaple
Cassandra Fetch Example -- Single Row fetch
package com.examples;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.NotFoundException;
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;
import org.apache.thrift.transport.TTransportException;
public class CassandraFetchHInfo {
public static final String UTF8 = "UTF8";
private static Long hiId = new Long(1) ;
public static void main(String[] args) throws UnsupportedEncodingException,
InvalidRequestException, UnavailableException, TimedOutException,
TException, NotFoundException {
CassandraFetchHInfo cassandraFetchHInfo = new CassandraFetchHInfo();
cassandraFetchHInfo.insertHistoricalInfo(hiId, objectObjHierarchyId, hiParameterOid, hiMonitoredValue, hiTimestampCreated, hiObjectId, hiInfoEventType);
}
public void insertHistoricalInfo(Long hiId,
Long objectObjHierarchyId, String hiParameterOid,
String hiMonitoredValue, Date hiTimestampCreated,
Object hiObjectId, String hiInfoEventType) {
System.out.println("Stating of class................................");
try {
TTransport tr = new TSocket("localhost", 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 = hiId.toString();
ColumnPath colPathhiId = new ColumnPath(columnFamily);
colPathhiId.setColumn("hiId".getBytes(UTF8));
// read single column
System.out.println("single column:");
Column col = client.get(keyspace, keyUserID, colPathhiId, ConsistencyLevel.ONE).getColumn();
System.out.println("column name: " + new String(col.name, UTF8));
System.out.println("column value: " + new String(col.value, UTF8));
System.out.println("column timestamp: " + new Date(col.timestamp));
tr.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (TTransportException e) {
e.printStackTrace();
} catch (UnavailableException e) {
e.printStackTrace();
} catch (TimedOutException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
}
Java, J2EE, Struts, Hibernate, Web Services, JSF
Apache Cassandra,
Cassandra Examples,
Cassandra Single Row Fetch Example
Cassandra insertion example
package com.example;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import org.apache.cassandra.thrift.Cassandra;
import org.apache.cassandra.thrift.Column;
import org.apache.cassandra.thrift.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.NotFoundException;
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;
import org.apache.thrift.transport.TTransportException;
public class CassandraInsertExample {
public static final String UTF8 = "UTF8";
private static Long hiId = new Long(1) ;
public static void main(String[] args) throws UnsupportedEncodingException,
InvalidRequestException, UnavailableException, TimedOutException,
TException, NotFoundException {
CassandraInsertExample cassandraInsertHInfo = new CassandraInsertExample();
cassandraInsertHInfo.insertHistoricalInfo(hiId);
}
public void insertHistoricalInfo(Long hiId) {
System.out.println("Stating of class................................");
try {
TTransport tr = new TSocket("localhost", 9160);
TProtocol proto = new TBinaryProtocol(tr);
Cassandra.Client client = new Cassandra.Client(proto);
tr.open();
String keyspace = "Employee";
String columnFamily = "Employee_Details";
String keyUserID = hiId.toString();
// insert data
long timestamp = System.currentTimeMillis();
ColumnPath colPathhiId = new ColumnPath(columnFamily);
colPathhiId.setColumn("hiId".getBytes(UTF8));
client.insert(keyspace, keyUserID, colPathhiId, hiId.toString().getBytes(UTF8), timestamp, ConsistencyLevel.ONE);
//Fetching of single row
Column col = client.get(keyspace, keyUserID, colPathhiId, ConsistencyLevel.ONE).getColumn();
System.out.println("column name: " + new String(col.name, UTF8));
System.out.println("column value: " + new String(col.value, UTF8));
System.out.println("column timestamp: " + new Date(col.timestamp));
tr.close();
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
} catch (InvalidRequestException e) {
e.printStackTrace();
} catch (TTransportException e) {
e.printStackTrace();
} catch (UnavailableException e) {
e.printStackTrace();
} catch (TimedOutException e) {
e.printStackTrace();
} catch (TException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
}
}
Java, J2EE, Struts, Hibernate, Web Services, JSF
Apache Cassandra,
Cassandra Examples,
Cassandra insertion example
Wednesday, August 11, 2010
Apache Cassandra
Some of useful links
http://cassandra.apache.org/
http://schabby.de/cassandra-installation-configuration/
http://arin.me/blog/wtf-is-a-supercolumn-cassandra-data-model/comment-page-2#comment-728
http://www.divconq.com/2010/how-to-add-and-retrieve-data-from-a-cassandra-database/
http://nosql.mypopescu.com/post/277382598/cassandra-gets-better-documentation
http://www.rackspacecloud.com/blog/2010/05/12/cassandra-by-example/
http://blog.evanweaver.com/articles/2009/07/06/up-and-running-with-cassandra/
http://maxgrinev.com/2010/07/12/do-you-really-need-sql-to-do-it-all-in-cassandra/
http://cassandra.apache.org/
http://schabby.de/cassandra-installation-configuration/
http://arin.me/blog/wtf-is-a-supercolumn-cassandra-data-model/comment-page-2#comment-728
http://www.divconq.com/2010/how-to-add-and-retrieve-data-from-a-cassandra-database/
http://nosql.mypopescu.com/post/277382598/cassandra-gets-better-documentation
http://www.rackspacecloud.com/blog/2010/05/12/cassandra-by-example/
http://blog.evanweaver.com/articles/2009/07/06/up-and-running-with-cassandra/
http://maxgrinev.com/2010/07/12/do-you-really-need-sql-to-do-it-all-in-cassandra/
Java, J2EE, Struts, Hibernate, Web Services, JSF
Apache Cassandra,
Apache Cassandra Usefull links
Subscribe to:
Posts (Atom)