【发布时间】:2019-10-27 01:54:32
【问题描述】:
Python 驱动程序为大型结果提供了事件/回调方法:
https://datastax.github.io/python-driver/query_paging.html
此外,还有一个 BatchQuery 类可以与 ORM 一起使用,它非常方便:
https://datastax.github.io/python-driver/cqlengine/batches.html?highlight=batchquery
现在,我需要在分页结果对象的回调处理程序中执行 BatchQuery,但脚本只是停留在当前页面上的迭代。
我猜这是因为无法在线程之间共享 cassandra 会话,而 BatchQuery 和“分页结果”方法正在使用线程来管理事件设置和回调调用。
知道如何神奇地解决这种情况吗?您可以在下面找到一些代码:
# paged.py
class PagedQuery:
"""
Class to manage paged results.
>>> query = "SELECT * FROM ks.my_table WHERE collectionid=123 AND ttype='collected'" # define query
>>> def handler(page): # define result page handler function
... for t in page:
... print(t)
>>> pq = PagedQuery(query, handler) # instantiate a PagedQuery object
>>> pq.finished_event.wait() # wait for the PagedQuery to handle all results
>>> if pq.error:
... raise pq.error
"""
def __init__(self, query, handler=None):
session = new_cassandra_session()
session.row_factory = named_tuple_factory
statement = SimpleStatement(query, fetch_size=500)
future = session.execute_async(statement)
self.count = 0
self.error = None
self.finished_event = Event()
self.query = query
self.session = session
self.handler = handler
self.future = future
self.future.add_callbacks(
callback=self.handle_page,
errback=self.handle_error
)
def handle_page(self, page):
if not self.handler:
raise RuntimeError('A page handler function was not defined for the query')
self.handler(page)
if self.future.has_more_pages:
self.future.start_fetching_next_page()
else:
self.finished_event.set()
def handle_error(self, exc):
self.error = exc
self.finished_event.set()
# main.py
# script using class above
def main():
query = 'SELECT * FROM ks.my_table WHERE collectionid=10 AND ttype=\'collected\''
def handle_page(page):
b = BatchQuery(batch_type=BatchType.Unlogged)
for obj in page:
process(obj) # some updates on obj...
obj.batch(b).save()
b.execute()
pq = PagedQuery(query, handle_page)
pq.finished_event.wait()
if not pq.count:
print('Empty queryset. Please, check parameters')
if __name__ == '__main__':
main()
【问题讨论】:
-
来自 Datastax 的 python cassandra 驱动程序人员:“您无法在查询回调中执行语句。我认为这是您遇到的问题。您无法在 handle_page 函数中执行语句”。
标签: cassandra cassandra-python-driver