Ser*_*rán 5 python cassandra cql3
我有一个 Cassandra 1.2 集群,我使用 cql 库从 Python 中使用它。现在我需要使用 get_slice 实现一些看起来非常简单的分页功能,但我找不到任何有关如何使用 cql 库中的类似功能的文档:
get_slice("key" : table_key,
"column_parent" : {"column_family" : "MyColumnFamily"},
"predicate" :
{ "slice_range" :
{ "start" : "SomeStartID",
"end" : "Z",
"reverse" : "false",
"count : "100" }
} )
Run Code Online (Sandbox Code Playgroud)
我在 get_slice 的随机文档中看到过这种类型的语法,它看起来不像 CQL 3 语法,我如何从 Python 到 Cassandra 1.2 集群运行这种类型的查询?,这是使用 get_slice 的当前方式或者有新的语法或 CQL 3 替代方案?
提前致谢!
您可以以大致相同的方式进行分页:设置限制并从大于前一个收到的列名称开始。作为示例,我在键空间 ks1 中创建了一个表 test1:
CREATE TABLE test1 (
a text,
b text,
PRIMARY KEY (a, b)
)
Run Code Online (Sandbox Code Playgroud)
这里 a 是我的行键,b 是列名。然后我插入了12条记录,其中a=a和b从a到l。所以
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
Run Code Online (Sandbox Code Playgroud)
然后我使用 CQL 驱动程序对这个 python 进行分页:
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]
Run Code Online (Sandbox Code Playgroud)
哪些页面批量为 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']
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6513 次 |
| 最近记录: |