本文介绍了Cassandra分页:如何使用get_slice从Python使用cql库查询Cassandra 1.2数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Cassandra 1.2集群,我使用它从Python使用的cql库。现在我需要实现一些分页功能,看起来很简单使用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?

提前感谢!

推荐答案

您可以以同样的方式进行分页:例如,我在keyspace 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的页面。输出是:

which pages in batches of 5. The output is:

[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数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:42