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