顯示具有 nosql 標籤的文章。 顯示所有文章
顯示具有 nosql 標籤的文章。 顯示所有文章

2011年12月25日 星期日

Road of Migration: From Traditional DB to Cassandra(2)

In accordance to previous post:

The following code works perfect with Jdk 7 in accompany with Cassandra 1.0.6:

May refer to this url: http://babydeed.iteye.com/category/187192


import java.nio.ByteBuffer;
import org.apache.cassandra.thrift.*;
import org.apache.cassandra.thrift.TBinaryProtocol;
import org.apache.thrift.protocol.*;
import org.apache.thrift.transport.*;

public class Test {
  public static void main(String[] args) throws Exception {
    insertOrUpdateData();
    getData();

  }

  public static void insertOrUpdateData() throws Exception{
    TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",9160));
    TProtocol protocol = new TBinaryProtocol(transport);
    Cassandra.Client client = new Cassandra.Client(protocol);
    transport.open();

    client.set_keyspace("ksTest1");
    ColumnParent parent = new ColumnParent("User1");

    ByteBuffer rowid = ByteBuffer.wrap("100".getBytes());

    Column column = new Column();
    column.setName("description2".getBytes());

    column.setValue("some value here222...".getBytes());
    column.setTimestamp(System.currentTimeMillis());
    client.insert(rowid, parent, column, ConsistencyLevel.ONE);

    transport.flush();
    transport.close();


  }

  public static void getData() throws Exception{
    TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",9160));
    TProtocol protocol = new TBinaryProtocol(transport);
    Cassandra.Client client = new Cassandra.Client(protocol);
    transport.open();

    client.set_keyspace("ksTest1");
    ColumnParent parent = new ColumnParent("User1");

    ByteBuffer rowid = ByteBuffer.wrap("100".getBytes());

    Column column = new Column();
    column.setName("description2".getBytes());

    ColumnPath path = new ColumnPath();
        path.column_family = "User1";
        path.column = ByteBuffer.wrap("description2".getBytes());

    ColumnOrSuperColumn c1 = client.get(rowid, path, ConsistencyLevel.ONE) ;
    String res = new String(c1.getColumn().getValue(),"UTF-8");

    System.out.println(res);
    transport.flush();
    transport.close();
  }


  public static void delete() throws Exception{
         // open connection to Cassandra server
    TTransport transport = new TFramedTransport(new TSocket("127.0.0.1",9160));
        TProtocol protocol = new TBinaryProtocol(transport);
        Cassandra.Client client = new Cassandra.Client(protocol);
        transport.open();

        // specify keyspace
        client.set_keyspace("ksTest1");

        // specify row id
        ByteBuffer rowid = ByteBuffer.wrap("100".getBytes());

        // specify column path
        ColumnPath path = new ColumnPath();
        path.column_family = "User1";
        path.column = ByteBuffer.wrap("description2".getBytes());

        // specify timestamp value of data to be removed
        long timestamp = 1301874610643000L;

        // set consistency level to one
        ConsistencyLevel consistency = ConsistencyLevel.ONE;

        // remove the description column from the specified row
        // in the User column family
        client.remove(rowid, path, timestamp, consistency);

        // release resources
        transport.flush();
        transport.close();
  }
}

Road of Migration: From Traditional DB to Cassandra(1)

As using SQL-Supported DB always encounters the tragedy of performance issue. nosql DB becomes my next of my research milestone. While figuring why all the example code posted on internet does not work. I finally figure out that the supported JAVA api is quite different from 0.6.6 to 1.0.6.

The difference is mentioned here:

If you are new to Cassandra and just want to get started, take a look at the available clients ClientOptions (v0.6) instead.

This page shows examples of using the low-level Thrift interface, primarily intended for client library developers.

To generate the bindings for a particular language, first find out if Thrift supports that language. If it does, you can run "thrift --gen XYZ interface/cassandra.thrift" for whatever XYZ you fancy.

These examples are for Cassandra 0.5 and 0.6.

As of 0.7 and later, the default thrift transport is TFramedTransport so if you get an error such as "No more data to read", switch transports.


For insertion example:

Ver 0.6.6


public void insert(java.lang.String keyspace,
                   java.lang.String key,
                   ColumnPath column_path,
                   byte[] value,
                   long timestamp,
                   ConsistencyLevel consistency_level)
            throws InvalidRequestException,
                   UnavailableException,
                   TimedOutException,
                   org.apache.thrift.TException


Ver 1.0.6

public void insert(java.nio.ByteBuffer key,
                   ColumnParent column_parent,
                   Column column,
                   ConsistencyLevel consistency_level)
            throws InvalidRequestException,
                   UnavailableException,
                   TimedOutException,
                   org.apache.thrift.TException