如何使用 pycassa 获取存储在 Cassandra 列族中的所有键?
有没有人有使用 pycassa 的经验,我对此表示怀疑。如何获取数据库中存储的所有密钥?
在这个小片段中,我们需要提供键才能获取关联的列(这里的键是“foo”和“bar”),这很好,但我的要求是一次获取所有键(仅键)作为 Python 列表或类似的数据结构。
cf.multiget(['foo', 'bar'])
{'foo': {'column1': 'val2'}, 'bar': {'column1': 'val3', 'column2': 'val4'}}
谢谢。
Is anyone having experience working with pycassa I have a doubt with it. How do I get all the keys that are stored in the database?
well in this small snippet we need to give the keys in order to get the associated columns (here the keys are 'foo' and 'bar'),that is fine but my requirement is to get all the keys (only keys) at once as Python list or similar data structure.
cf.multiget(['foo', 'bar'])
{'foo': {'column1': 'val2'}, 'bar': {'column1': 'val3', 'column2': 'val4'}}
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
尝试:
这里有更多好东西:http://github.com/vomjom/pycassa
try:
more good stuff here: http://github.com/vomjom/pycassa
您可以尝试:
cf.get_range(column_count=0,filter_empty=False)
。You can try:
cf.get_range(column_count=0,filter_empty=False)
.get_range([开始][, 结束][, 列][, 列开始][, 列完成][, 列反转][, 列计数][, 行计数][, 包含时间戳][, 超级列][, 读取一致性级别][,缓冲区大小])
http://pycassa.github.com/pycassa /api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get_range
get_range([start][, finish][, columns][, column_start][, column_finish][, column_reversed][, column_count][, row_count][, include_timestamp][, super_column][, read_consistency_level][, buffer_size])
http://pycassa.github.com/pycassa/api/pycassa/columnfamily.html#pycassa.columnfamily.ColumnFamily.get_range
Santhosh 解决方案的小改进
如果您关心顺序:
get_range 返回一个生成器。我们可以从生成器创建一个字典并从中获取密钥。
column_count=0 将结果限制为 row_key。但是,由于这些结果没有列,我们还需要 filter_empty。
filter_empty=False 将允许我们得到结果。然而,空行和范围幻影现在可能包含在我们的结果中。
如果我们不介意更多的开销,只获取第一列将解决空行和范围幽灵问题。
Minor improvement on Santhosh's solution
If you care about order:
get_range returns a generator. We can create a dict from the generator and get the keys from that.
column_count=0 limits results to the row_key. However, because these results have no columns we also need filter_empty.
filter_empty=False will allow us to get the results. However empty rows and range ghosts may be included in our result now.
If we don't mind more overhead, getting just the first column will resolve the empty rows and range ghosts.
Santhosh 和 有问题kzarns' 的答案,因为你正在记忆一个可能很大的字典,但你会立即丢弃它。更好的方法是使用列表推导式:
它迭代
get_range
返回的生成器,将键保留在内存中并存储列表。如果键列表也可能太大而无法一次将其全部保存在内存中,并且您只需要迭代一次,则应该使用 生成器表达式而不是列表理解:
There's a problem with Santhosh's and kzarns' answers, as you're bringing in memory a potentially huge dict that you are immediately discarding. A better approach would be using list comprehensions for this:
This iterates over the generator returned by
get_range
, keeps the key in memory and stores the list.If the list of keys where also potentially too large to keep it in memory all at once and you only need to iterate once, you should use a generator expression instead of a list comprehension: