Nosql Cassandra 0.6 key值的区间查询 小记: 传入条件 如key区间a至c 一种有a-d的数据 List<KeySlice> sliceList = client.get_range_slice(keyspace,parent, predicate,"a","d",1000,ConsistencyLevel.ONE);
- package com.sh2999.cassandra.testapp;
-
- import java.util.List;
- import org.apache.cassandra.thrift.Cassandra;
- 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.KeySlice;
- import org.apache.cassandra.thrift.NotFoundException;
- import org.apache.cassandra.thrift.SlicePredicate;
- import org.apache.cassandra.thrift.SliceRange;
- 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 Cassandra647TestApp{
-
-
-
-
- public static void main(String[]args) throws Exception{
-
- Stringkeyspace="Keyspace1" ;
- Stringcf="sh2999.com" ;
- Stringkey="row1" ;
- byte []columnName= "colname" .getBytes( "UTF-8" );
- byte []data= "testdata" .getBytes( "UTF-8" );
-
- TTransporttransport=new TSocket( "localhost" , 9160 );
- TProtocolprotocol=new TBinaryProtocol(transport);
-
- Cassandra.Clientclient=new Cassandra.Client(protocol);
- transport.open();
-
- ColumnPathpath=new ColumnPath(cf);
- path.setColumn(columnName);
-
- client.insert(keyspace,key,path,data,System.currentTimeMillis(),
- ConsistencyLevel.ONE);
- key="testrow2" ;
- byte []data2= "testdata" .getBytes( "UTF-8" );
- client.insert(keyspace,data2,
- ConsistencyLevel.ONE);
-
- key="a" ;
- byte []data3= "testdata" .getBytes( "UTF-8" );
- client.insert(keyspace,data3,
- ConsistencyLevel.ONE);
-
- key="b" ;
- byte []data4= "testdata" .getBytes( "UTF-8" );
- client.insert(keyspace,data4,
- ConsistencyLevel.ONE);
- key="c" ;
- byte []data5= "testdata" .getBytes( "UTF-8" );
- client.insert(keyspace,data5,
- ConsistencyLevel.ONE);
-
- key="d" ;
- byte []data6= "testdata" .getBytes( "UTF-8" );
- client.insert(keyspace,data6,
- ConsistencyLevel.ONE);
-
- Thread.sleep(1000 );
-
- ColumnPathrowpath=new ColumnPath(cf);
- rowpath.setColumn(columnName);
-
-
-
-
-
-
- try {
- ColumnOrSuperColumncosc=client.get(keyspace,
- ConsistencyLevel.ONE);
-
- System.out.println("Whoops!NotFoundExceptionnotthrown!" );
- }catch (NotFoundExceptione){
-
- System.out.println("OK,wegotaNotFoundException" );
- }
-
- ColumnParentparent=new ColumnParent(cf);
- SlicePredicatepredicate=new SlicePredicate();
- SliceRangerange=new SliceRange();
- range.start=new byte [ 0 ];
- range.finish=new byte [ 10 ];
-
- predicate.slice_range=range;
-
- List<KeySlice>sliceList=client.get_range_slice(keyspace,
- predicate,"a" , "d" , 1000 ,ConsistencyLevel.ONE);
-
- for (KeySlicek:sliceList){
- System.err.println("Foundkey" +k.key);
- if (key.equals(k.key)){
-
- System.out.println("butkey" +k.key
- +"shouldhavebeenremoved" );
- }
- }
- }
- }
package com.sh2999.cassandra.testapp;
import java.util.List;
import org.apache.cassandra.thrift.Cassandra;
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.KeySlice;
import org.apache.cassandra.thrift.NotFoundException;
import org.apache.cassandra.thrift.SlicePredicate;
import org.apache.cassandra.thrift.SliceRange;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
/**
* key值的区间查询 这里可以传入条件 如key区间a至c 一种有a-d的数据
*
* @since Cassandra 0.6
* @author db2admin
*
*/
public class Cassandra647TestApp {
/**
*
* OrderPreservingPartitioner should be used.
*/
public static void main(String[] args) throws Exception {
String keyspace = "Keyspace1";
String cf = "sh2999.com";
String key = "row1";
byte[] columnName = "colname".getBytes("UTF-8");
byte[] data = "testdata".getBytes("UTF-8");
TTransport transport = new TSocket("localhost",9160);
TProtocol protocol = new TBinaryProtocol(transport);
Cassandra.Client client = new Cassandra.Client(protocol);
transport.open();
ColumnPath path = new ColumnPath(cf);
path.setColumn(columnName);
client.insert(keyspace,ConsistencyLevel.ONE);
key = "testrow2";
byte[] data2 = "testdata".getBytes("UTF-8");
client.insert(keyspace,ConsistencyLevel.ONE);
key = "a";
byte[] data3 = "testdata".getBytes("UTF-8");
client.insert(keyspace,ConsistencyLevel.ONE);
key = "b";
byte[] data4 = "testdata".getBytes("UTF-8");
client.insert(keyspace,ConsistencyLevel.ONE);
key = "c";
byte[] data5 = "testdata".getBytes("UTF-8");
client.insert(keyspace,ConsistencyLevel.ONE);
key = "d";
byte[] data6 = "testdata".getBytes("UTF-8");
client.insert(keyspace,ConsistencyLevel.ONE);
Thread.sleep(1000);
ColumnPath rowpath = new ColumnPath(cf);
rowpath.setColumn(columnName);
// 删除通过
// client.remove(keyspace,// ConsistencyLevel.ONE);
// Thread.sleep(1000);
try {
ColumnOrSuperColumn cosc = client.get(keyspace,ConsistencyLevel.ONE);
System.out.println("Whoops! NotFoundException not thrown!");
} catch (NotFoundException e) {
System.out.println("OK,we got a NotFoundException");
}
ColumnParent parent = new ColumnParent(cf);
SlicePredicate predicate = new SlicePredicate();
SliceRange range = new SliceRange();
range.start = new byte[0];
range.finish = new byte[10];
predicate.slice_range = range;
// 这里可以传入条件 如key区间a至c 一种有a-d的数据
List<KeySlice> sliceList = client.get_range_slice(keyspace,predicate,ConsistencyLevel.ONE);
for (KeySlice k : sliceList) {
System.err.println("Found key " + k.key);
if (key.equals(k.key)) {
System.out.println("but key " + k.key
+ " should have been removed");
}
}
}
}
<Keyspaces> <Keyspace Name="Keyspace1"> <ColumnFamily CompareWith="BytesType" Name="wingTable" KeysCached="10%" /> <ColumnFamily CompareWith="BytesType" Name="Standard1" KeysCached="10%" /> <ColumnFamily CompareWith="BytesType" Name="Standard2" KeysCached="10%" /> <ColumnFamily CompareWith="BytesType" Name="Standardw" KeysCached="10%" /> <ColumnFamily CompareWith="BytesType" Name="sh2999.com" KeysCached="10%" /> <ReplicaPlacementStrategy>org.apache.cassandra.locator.RackUnawareStrategy</ReplicaPlacementStrategy> <ReplicationFactor>1</ReplicationFactor> <EndPointSnitch>org.apache.cassandra.locator.EndPointSnitch</EndPointSnitch> </Keyspace> (编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|