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资料收集见:
[url]http://www.sh2999.com/sh/posts/list/325.page[/url]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值