간단한 thrift를 이용한 cassandra 예제

간단한 thrift를 이용한 카산드라 예제 흐음…

 

create keyspace BLOG with strategy_options={replication_factor:1} and placement_strategy = ‘org.apache.cassandra.locator.SimpleStrategy’;
create column family category with comparator=UTF8Type
and default_validation_class=UTF8Type and key_validation_class=UTF8Type
and column_metadata=[
{column_name: category_id, validation_class: UTF8Type},
{column_name: category_name, validation_class: UTF8Type}];

create column family posts with comparator=UTF8Type
and default_validation_class=LongType and key_validation_class=LongType
and column_metadata=[
{column_name: id, validation_class: UTF8Type},
{column_name: title, validation_class: UTF8Type},
{column_name: post, validation_class: UTF8Type},
{column_name: category_id, validation_class: UTF8Type, index_name: category_index, index_type: 0},
{column_name: post_date, validation_class: LongType}];
create column family comments with comparator=UTF8Type
and default_validation_class=LongType and key_validation_class=LongType
and column_metadata=[
{column_name: post_id, validation_class: UTF8Type, index_name: post_index, index_type: 0},
{column_name: comment, validation_class: UTF8Type},
{column_name: comment_date, validation_class: LongType}];

 

<pre>import java.io.UnsupportedEncodingException;
import java.nio.ByteBuffer;
import java.util.List;
import java.util.Collections;

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.ColumnPath;
import org.apache.cassandra.thrift.ConsistencyLevel;
import org.apache.cassandra.thrift.InvalidRequestException;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.cassandra.thrift.KeyRange;
import org.apache.cassandra.thrift.KeySlice;
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.TFramedTransport;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;

public class CClient
{
 public static void main(String[] args)
 throws TException, InvalidRequestException, UnavailableException, UnsupportedEncodingException, NotFoundException, TimedOu
tException
 {
 TTransport tr = new TFramedTransport(new TSocket("10.64.81.181", 9160));
 TProtocol proto = new TBinaryProtocol(tr);
 Cassandra.Client client = new Cassandra.Client(proto);
 tr.open();

String key_user_id = "1";

// insert data
 long timestamp = System.currentTimeMillis();
 client.set_keyspace("BLOG");
 ColumnParent parent = new ColumnParent("category");

Column idColumn = new Column(toByteBuffer("category_id"));
 idColumn.setValue(toByteBuffer("3"));
 idColumn.setTimestamp(timestamp);
 client.insert(toByteBuffer("3"), parent, idColumn, ConsistencyLevel.ONE);

Column nameColumn = new Column(toByteBuffer("category_name"));
 nameColumn.setValue(toByteBuffer("NoSQL"));
 nameColumn.setTimestamp(timestamp);
 client.insert(toByteBuffer("3"), parent, nameColumn, ConsistencyLevel.ONE);

&nbsp;

ColumnPath path = new ColumnPath("category");

// read single column
 path.setColumn(toByteBuffer("category_id"));
 System.out.println(client.get(toByteBuffer(key_user_id), path, ConsistencyLevel.ONE));

// read entire row
 SlicePredicate predicate = new SlicePredicate();
 SliceRange sliceRange = new SliceRange(toByteBuffer(""), toByteBuffer(""), false, 10);
 predicate.setSlice_range(sliceRange);
// predicate.addToColumn_names( ByteBuffer.wrap("category_id".getBytes("UTF8")));
// predicate.addToColumn_names( ByteBuffer.wrap("category_name".getBytes("UTF8")));

KeyRange keyRange = new KeyRange();
 keyRange.start_key = ByteBuffer.allocate(0);
 keyRange.end_key = ByteBuffer.allocate(0);
 keyRange.count = 2;

List<KeySlice> results = client.get_range_slices( parent, predicate, keyRange, ConsistencyLevel.ONE);

for( KeySlice keySlice : results ){
 List<ColumnOrSuperColumn> columns = keySlice.getColumns();
 for (ColumnOrSuperColumn result : columns)
 {
 Column column = result.column;
 System.out.println(toString(column.name) + " -> " + toString(column.value));
 }

}

tr.close();
 }

public static ByteBuffer toByteBuffer(String value)
 throws UnsupportedEncodingException
 {
 return ByteBuffer.wrap(value.getBytes("UTF-8"));
 }

public static String toString(ByteBuffer buffer)
 throws UnsupportedEncodingException
 {
 byte[] bytes = new byte[buffer.remaining()];
 buffer.get(bytes);
 return new String(bytes, "UTF-8");
 }
}

&nbsp;