cassandra get set同步問題

oxoxooxx發表於2011-07-15

import quicktime.std.clocks.Clock;

import com.sun.corba.se.spi.orbutil.fsm.Guard.Result;

import me.prettyprint.cassandra.serializers.StringSerializer;
import me.prettyprint.cassandra.service.CassandraHostConfigurator;
import me.prettyprint.hector.api.Cluster;
import me.prettyprint.hector.api.Keyspace;
import me.prettyprint.hector.api.beans.HColumn;
import me.prettyprint.hector.api.factory.HFactory;
import me.prettyprint.hector.api.mutation.Mutator;
import me.prettyprint.hector.api.query.ColumnQuery;
import me.prettyprint.hector.api.query.QueryResult;

public class ExampleClient {

public static void main(String[] args) throws IllegalStateException,Exception {
CassandraHostConfigurator cassandraHostConfigurator=new CassandraHostConfigurator("192.168.100.108:9160,192.168.100.110:9160");
cassandraHostConfigurator.setMaxActive(20);
cassandraHostConfigurator.setMaxIdle(5);
cassandraHostConfigurator.setCassandraThriftSocketTimeout(3000);
cassandraHostConfigurator.setMaxWaitTimeWhenExhausted(4000);

StringSerializer stringSerializer=new StringSerializer();
Cluster cluster=HFactory.createCluster("cluster_name", cassandraHostConfigurator);

Keyspace ks=HFactory.createKeyspace("OBS", cluster);
String columnFamily = "bucket";
Mutator mutator =HFactory.createMutator(ks, stringSerializer);

mutator.insert("bucket_key", columnFamily, HFactory.createStringColumn("object_name", "object_value"));

ColumnQuery columnQuery =HFactory.createColumnQuery(ks, stringSerializer, stringSerializer, stringSerializer);
columnQuery.setColumnFamily(columnFamily).setKey("bucket_key").setName("object_name");
QueryResult> queryResult=columnQuery.execute();


//若物件存在,列印物件key-value
System.out.println("object_name: "+queryResult.get().getName());
System.out.println("object_value: "+queryResult.get().getValue());
System.out.println("object_clock: "+queryResult.get().getClock());


//測試一個不存在的物件
System.out.println("測試一個不存在的物件");
columnQuery.setColumnFamily(columnFamily).setKey("bucket_key").setName("object_name1");
queryResult=columnQuery.execute();
try{
System.out.println("queryResult: "+queryResult.get().getClock());
}catch(NullPointerException e){
System.out.println("the object you query does not exist!");
e.printStackTrace();
}

//測試獲取一個刪除的物件
System.out.println("測試一個刪除的物件");
columnQuery.setColumnFamily(columnFamily).setKey("bucket_name").setName("object_name");
queryResult=columnQuery.execute();
try{
System.out.println("queryResult: "+queryResult.get().getClock());
}catch(NullPointerException e){
System.out.println("the object you query does not exist!");
e.printStackTrace();
}

//測試寫入一個物件後,在寫入一個比當前物件時間戳要小的物件
//exist object_name_tw object_value_tw 1310661932412000L
long clock =1210661994080000L;
//mutator.insert("bucket_key", columnFamily, HFactory.createStringColumn("object_name_tw", "object_value_tw"));
HColumn column=HFactory.createColumn("object_name_tw", "object_value_tw", clock, stringSerializer, stringSerializer);
mutator.insert("bucket_key", columnFamily, column);
cluster.getConnectionManager().shutdown();
//result:clock 小於當前物件 則當前物件仍保留;clock大於當前物件,新的物件被寫入

}
}

[@more@]

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/23937368/viewspace-1052469/,如需轉載,請註明出處,否則將追究法律責任。

相關文章