`
wing5jface
  • 浏览: 7908 次
  • 性别: Icon_minigender_1
  • 来自: 珠海
社区版块
存档分类

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


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


其它关于Cassandra资料收集见:
http://www.sh2999.com/sh/posts/list/325.page
0
0
分享到:
评论
1 楼 cmzx3444 2010-05-26  
#   List<KeySlice> sliceList = client.get_range_slice(keyspace, parent, 
#                 predicate, "a", "d", 1000, ConsistencyLevel.ONE); 
"a", "d"是什么意思,是表示key 的头一个字母吗,我的key用的是数字,但查询“1”,“10”的时候出来的东西却不是按照1-10这个顺序的,我的key是“1”-“100”

相关推荐

Global site tag (gtag.js) - Google Analytics