首页 文章

可以使用DataStax python cassandra-driver过滤行值吗?

提问于
浏览
0

我需要处理大约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个查询中,节省的成本就会增加 .

1 回答

  • 0

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

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

相关问题