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;
- /**
- *key 值的区间查询这里可以传入条件如key区间a至c一种有a-d的数据
- *
- *@sinceCassandra0.6
- *@authordb2admin
- *
- */
- public class Cassandra647TestApp{
- /**
- *
- *OrderPreservingPartitionershouldbeused.
- */
- 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);
- //删除通过
- //client.remove(keyspace,rowpath,
- //ConsistencyLevel.ONE);
- //Thread.sleep(1000);
- 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;
- //这里可以传入条件如key区间a至c一种有a-d的数据
- 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" );
- }
- }
- }
- }
<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>
原文链接:https://www.f2er.com/nosql/204814.html