Cassandra Insert Example

package com.example;

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

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

String keyspace = “Employee”;
String columnFamily = “Employee_Details”;

String keyUserID = hiId.toString();

// insert data
long timestamp = System.currentTimeMillis();

ColumnPath colPathhiId = new ColumnPath(columnFamily);
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(, UTF8));
System.out.println(“column value: ” + new String(col.value, UTF8));
System.out.println(“column timestamp: ” + new Date(col.timestamp));

} catch (UnsupportedEncodingException e) {
} catch (InvalidRequestException e) {
} catch (TTransportException e) {
} catch (UnavailableException e) {
} catch (TimedOutException e) {
} catch (TException e) {
} catch (NotFoundException e) {

Leave a Reply

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

You are commenting using your 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.