【发布时间】:2018-04-18 02:20:09
【问题描述】:
我需要处理大约 4,000 个 cassandra 查询。我将每个查询 ResultSet 转换为生成器以保持较低的内存占用。在生成器的每一行中,我只关心大约 50 个存在的几个字段。
我知道我不能直接在 CQL 中的值字段上进行过滤,但是 DataStax Python Cassandra 驱动程序是否有内置的东西可以做到这一点?还是在我构建生成器时这样做更有意义,即
def make_gen(response):
for row in response:
yield row.value.field1, row.value.filed2
我目前正在发出直接查询,但稍后将通过并发查询和准备好的语句转向基于模型的方法。发出请求的代码非常基础
sess = connect_cas(env)
for user in users:
q = 'select * from table ' + \
'where key1 = {} and '.format(key_1) + \
'key2 = {} and '.format(key_2) + \
'sample_time > {} '.format(t1) + \
'sample_time < {} '.format(t2)
resp_gen = make_gen(sess.execute(q)) # just a yield json.loads(Row.value)
for resp in resp_gen:
if field in resp:
// process data from this field
我只关心存在这个“字段”的行。我已经更新了我的生成器,只在这种情况下才产生数据,但是,如果 DataStax 驱动程序中内置了一些东西可以更有效地执行此操作,那么在 4,000 次查询时,节省的费用将加起来。
【问题讨论】:
-
请显示正在执行请求的代码 - 您是否使用基于
Model的方法?还是直接查询?