Cassandra 分页:如何使用 get_slice 从 Python 中使用 cql 库查询 Cassandra 1.2 数据库 [英] Cassandra pagination: How to use get_slice to query a Cassandra 1.2 database from Python using the cql library
问题描述
我有一个 Cassandra 1.2 集群,我正在使用 cql 库从 Python 中使用它.现在我需要使用 get_slice 来实现一些看起来很简单的分页功能,但是我在 cql 库中找不到任何关于如何使用这样的东西的文档:
I have a Cassandra 1.2 cluster and I'm using it from Python using the cql library. Now I need to implement some paging functionality that seems pretty straightforward using get_slice, but I can't find any documentation on how to use something like this from the cql library:
get_slice("key" : table_key,
"column_parent" : {"column_family" : "MyColumnFamily"},
"predicate" :
{ "slice_range" :
{ "start" : "SomeStartID",
"end" : "Z",
"reverse" : "false",
"count : "100" }
} )
我在 get_slice 的随机文档中看到过这种类型的语法,它看起来不像 CQL 3 语法,我如何将这种类型的查询从 Python 运行到 Cassandra 1.2 集群?,这是当前的方式使用 get_slice 还是有新的语法或 CQL 3 替代方案?
I've seen this type of syntax on random documentation for get_slice, and it doesn't look like CQL 3 syntax, how can I run this type of queries from Python to a Cassandra 1.2 cluster?, Is this the current way of using get_slice or there is a new syntax or CQL 3 alternative?
提前致谢!
推荐答案
您可以以几乎相同的方式进行分页:设置限制并从比接收到的前一列名大的列名开始.例如,我在键空间 ks1 中创建了一个表 test1:
You can do paging in much the same way: set a limit and start at a column name greater than the previous one received. As an example, I created a table test1 in keyspace ks1:
CREATE TABLE test1 (
a text,
b text,
PRIMARY KEY (a, b)
)
这里 a 是我的行键,b 是列名.然后我插入了 12 条 a=a 和 b 从 a 到 l 的记录.所以
Here a is my row key and b is the column name. I then inserted 12 records with a=a and b from a to l. So
cqlsh:ks1> select * from test1;
a | b
---+---
a | a
a | b
a | c
a | d
a | e
a | f
a | g
a | h
a | i
a | j
a | k
a | l
然后我使用 CQL 驱动程序使用这个 python 进行分页:
Then I paged with this python using the CQL driver:
import cql
con = cql.connect('localhost', keyspace='ks1', cql_version='3.0.0')
cursor = con.cursor()
last = ""
while last != None:
cursor.execute("select * from test1 where a=:a and b>:b limit 5", {"a": "a", "b": last})
last = None
for row in cursor:
print row
last = row[1]
分批5页.输出为:
[u'a', u'a']
[u'a', u'b']
[u'a', u'c']
[u'a', u'd']
[u'a', u'e']
[u'a', u'f']
[u'a', u'g']
[u'a', u'h']
[u'a', u'i']
[u'a', u'j']
[u'a', u'k']
[u'a', u'l']
这篇关于Cassandra 分页:如何使用 get_slice 从 Python 中使用 cql 库查询 Cassandra 1.2 数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!