Nosql Cassandra 0.6 key值的区间查询例子

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);

  1. package  com.sh2999.cassandra.testapp;  
  2.   
  3. import  java.util.List;  
  4. import  org.apache.cassandra.thrift.Cassandra;  
  5. import  org.apache.cassandra.thrift.ColumnOrSuperColumn;  
  6. import  org.apache.cassandra.thrift.ColumnParent;  
  7. import  org.apache.cassandra.thrift.ColumnPath;  
  8. import  org.apache.cassandra.thrift.ConsistencyLevel;  
  9. import  org.apache.cassandra.thrift.KeySlice;  
  10. import  org.apache.cassandra.thrift.NotFoundException;  
  11. import  org.apache.cassandra.thrift.SlicePredicate;  
  12. import  org.apache.cassandra.thrift.SliceRange;  
  13. import  org.apache.thrift.protocol.TBinaryProtocol;  
  14. import  org.apache.thrift.protocol.TProtocol;  
  15. import  org.apache.thrift.transport.TSocket;  
  16. import  org.apache.thrift.transport.TTransport;  
  17.   
  18. /**  
  19.  * key 值的区间查询 这里可以传入条件 如key区间a至c 一种有a-d的数据  
  20.  *   
  21.  * @since Cassandra 0.6  
  22.  * @author db2admin  
  23.  *   
  24.  */   
  25. public   class  Cassandra647TestApp {  
  26.     /**  
  27.      *   
  28.      * OrderPreservingPartitioner should be used.  
  29.      */   
  30.     public   static   void  main(String[] args)  throws  Exception {  
  31.   
  32.         String keyspace = "Keyspace1" ;  
  33.         String cf = "sh2999.com" ;  
  34.         String key = "row1" ;  
  35.         byte [] columnName =  "colname" .getBytes( "UTF-8" );  
  36.         byte [] data =  "testdata" .getBytes( "UTF-8" );  
  37.   
  38.         TTransport transport = new  TSocket( "localhost" 9160 );  
  39.         TProtocol protocol = new  TBinaryProtocol(transport);  
  40.   
  41.         Cassandra.Client client = new  Cassandra.Client(protocol);  
  42.         transport.open();  
  43.   
  44.         ColumnPath path = new  ColumnPath(cf);  
  45.         path.setColumn(columnName);  
  46.   
  47.         client.insert(keyspace, key, path, data, System.currentTimeMillis(),  
  48.                 ConsistencyLevel.ONE);  
  49.         key = "testrow2" ;  
  50.         byte [] data2 =  "testdata" .getBytes( "UTF-8" );  
  51.         client.insert(keyspace, key, path, data2, System.currentTimeMillis(),  
  52.                 ConsistencyLevel.ONE);  
  53.   
  54.         key = "a" ;  
  55.         byte [] data3 =  "testdata" .getBytes( "UTF-8" );  
  56.         client.insert(keyspace, key, path, data3, System.currentTimeMillis(),  
  57.                 ConsistencyLevel.ONE);  
  58.   
  59.         key = "b" ;  
  60.         byte [] data4 =  "testdata" .getBytes( "UTF-8" );  
  61.         client.insert(keyspace, key, path, data4, System.currentTimeMillis(),  
  62.                 ConsistencyLevel.ONE);  
  63.         key = "c" ;  
  64.         byte [] data5 =  "testdata" .getBytes( "UTF-8" );  
  65.         client.insert(keyspace, key, path, data5, System.currentTimeMillis(),  
  66.                 ConsistencyLevel.ONE);  
  67.   
  68.         key = "d" ;  
  69.         byte [] data6 =  "testdata" .getBytes( "UTF-8" );  
  70.         client.insert(keyspace, key, path, data6, System.currentTimeMillis(),  
  71.                 ConsistencyLevel.ONE);  
  72.   
  73.         Thread.sleep(1000 );  
  74.   
  75.         ColumnPath rowpath = new  ColumnPath(cf);  
  76.         rowpath.setColumn(columnName);  
  77.   
  78.         // 删除通过   
  79.         // client.remove(keyspace, key, rowpath, System.currentTimeMillis(),   
  80.         // ConsistencyLevel.ONE);   
  81.         // Thread.sleep(1000);   
  82.   
  83.         try  {  
  84.             ColumnOrSuperColumn cosc = client.get(keyspace, key, path,  
  85.                     ConsistencyLevel.ONE);  
  86.   
  87.             System.out.println("Whoops! NotFoundException not thrown!" );  
  88.         } catch  (NotFoundException e) {  
  89.   
  90.             System.out.println("OK, we got a NotFoundException" );  
  91.         }  
  92.   
  93.         ColumnParent parent = new  ColumnParent(cf);  
  94.         SlicePredicate predicate = new  SlicePredicate();  
  95.         SliceRange range = new  SliceRange();  
  96.         range.start = new   byte [ 0 ];  
  97.         range.finish = new   byte [ 10 ];  
  98.   
  99.         predicate.slice_range = range;  
  100.         // 这里可以传入条件 如key区间a至c 一种有a-d的数据   
  101.         List<KeySlice> sliceList = client.get_range_slice(keyspace, parent,  
  102.                 predicate, "a" "d" 1000 , ConsistencyLevel.ONE);  
  103.   
  104.         for  (KeySlice k : sliceList) {  
  105.             System.err.println("Found key "  + k.key);  
  106.             if  (key.equals(k.key)) {  
  107.   
  108.                 System.out.println("but key "  + k.key  
  109.                         + "  should have been removed" );  
  110.             }  
  111.         }  
  112.     }  
  113. }  

 

 

<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>

你可能感兴趣的:(exception,String,NoSQL,Path,cassandra,byte)