python驱动程序分页不工作
下面是使用cassandra python驱动程序进行分页的代码 我尝试了覆盖查询和设置会话默认大小。但它们都不起作用,结果总是来自表中的所有行。我错过了什么python驱动程序分页不工作,python,cassandra,pagination,driver,cql,Python,Cassandra,Pagination,Driver,Cql,下面是使用cassandra python驱动程序进行分页的代码 我尝试了覆盖查询和设置会话默认大小。但它们都不起作用,结果总是来自表中的所有行。我错过了什么 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"]
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
正在调用寻呼机制。基本上,这是创建一个迭代器,该迭代器一次只请求从查询定义的结果集中提取大小为的行
使用LIMIT和WHERE子句来限制实际结果。
您可以使用当前行获取当前页面的行,如:
以下代码可能有助于以分页方式获取结果-
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
@RussS:什么时候实际从Cassandra服务器获取数据:1还是2?1.结果=session.executestatement 2。对于结果中的行:打印行如果数据是按提取大小从服务器批量提取的,这是否意味着查询是延迟计算的。如果不是,results=session.executestatement是否从服务器获取所有数据,或者从服务器获取所需的数据,如图2所示,以获取客户端大小的数据?两者都是。Execute将获取第一页。在迭代到页面末尾时,它将获取下一个页面。查询将立即计算,但后续页面将被延迟获取。如果您愿意,resultset对象还具有一次获取整个结果的方法。
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