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. }  
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, key, path, data, System.currentTimeMillis(),
				ConsistencyLevel.ONE);
		key = "testrow2";
		byte[] data2 = "testdata".getBytes("UTF-8");
		client.insert(keyspace, key, path, data2, System.currentTimeMillis(),
				ConsistencyLevel.ONE);

		key = "a";
		byte[] data3 = "testdata".getBytes("UTF-8");
		client.insert(keyspace, key, path, data3, System.currentTimeMillis(),
				ConsistencyLevel.ONE);

		key = "b";
		byte[] data4 = "testdata".getBytes("UTF-8");
		client.insert(keyspace, key, path, data4, System.currentTimeMillis(),
				ConsistencyLevel.ONE);
		key = "c";
		byte[] data5 = "testdata".getBytes("UTF-8");
		client.insert(keyspace, key, path, data5, System.currentTimeMillis(),
				ConsistencyLevel.ONE);

		key = "d";
		byte[] data6 = "testdata".getBytes("UTF-8");
		client.insert(keyspace, key, path, data6, System.currentTimeMillis(),
				ConsistencyLevel.ONE);

		Thread.sleep(1000);

		ColumnPath rowpath = new ColumnPath(cf);
		rowpath.setColumn(columnName);

		// 刪除通過
		// client.remove(keyspace, key, rowpath, System.currentTimeMillis(),
		// ConsistencyLevel.ONE);
		// Thread.sleep(1000);

		try {
			ColumnOrSuperColumn cosc = client.get(keyspace, key, path,
					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, parent,
				predicate, "a", "d", 1000, 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>

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章