【问题标题】:Possible to filter row values using DataStax python cassandra-driver?可以使用 DataStax python cassandra-driver 过滤行值吗?
【发布时间】: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 的方法?还是直接查询?

标签: python cassandra datastax


【解决方案1】:

您是否表明您只处理 field1field2 设置为特定值的行?

它并非完全为此目的而构建,但您可以使用自定义 row_factory 在较低级别实现此过滤,并避免命名元组、元组和其他生成器之间的转换。

【讨论】:

    猜你喜欢
    • 2014-04-22
    • 2016-07-31
    • 2016-12-11
    • 2013-11-09
    • 2021-11-21
    • 2020-10-20
    • 2019-10-30
    • 2020-07-14
    • 2017-03-18
    相关资源
    最近更新 更多