python-驱动程序分页不工作

python-driver pagination not working

我有以下代码使用 cassandra python 驱动程序进行分页

我尝试了覆盖查询和设置会话 default_fetch_size。但是 none 的工作正常,结果总是来自 table 的所有行。我错过了什么?

from cassandra.cluster import Cluster
from cassandra.query import SimpleStatement

# setup
cluster = Cluster(["10.40.10.xxx","10.40.10.xxx","10.40.22.xxx","10.40.22.xxx"])
session = cluster.connect()
session.set_keyspace("ad_realtime")
# session.default_fetch_size = 10

query = "SELECT * from campaign_offset"
statement = SimpleStatement(query, fetch_size=10)
results = session.execute(statement)

for row in results:
   print row

Python 驱动程序中的分页并不意味着只获取部分查询。这意味着一次只获取部分查询。

您的代码

for row in results:
   print row

正在调用寻呼机。基本上这是创建一个迭代器,它一次只从查询定义的结果集中请求 fetch_size 行。

使用LIMITWHERE 子句来限制您的实际结果。

可以通过current_rows获取当前页面的行数,如:

for row in results.current_rows:
    print row

以下代码可能会帮助您以分页方式获取结果-

def fetch_rows(stmt: Statement, fetch_size: int = 10):
    stmt.fetch_size = fetch_size
    rs: ResultSet = session.execute(stmt)
    has_pages = rs.has_more_pages
    while has_pages:
        yield from rs.current_rows
        print ('-----------------------------------------')
        has_pages = rs.has_more_pages
        rs = session.execute(ps, paging_state=rs.paging_state)

def execute():
    query = "SELECT  col1, col2 FROM my_table WHERE some_partition_key='part_val1' AND some_clustering_col='clus_val1'"
    ps = session.prepare(query)
    for row in fetch_rows(ps, 20):
        print(row)
        # Process the row and perform desired operation