python-3.x 由于数据库大小和列族较大,无法从RocksDB获取键

5uzkadbs  于 2022-11-26  发布在  Python
关注(0)|答案(1)|浏览(108)

我想从RocksDB中获取所有键,但是当我尝试创建迭代器的list时,得到了一个空列表:

it = db.iterkeys()
it.seek_to_first()

数据库包含数据,我确信这一点。为了提供最少的可行代码,数据库是here。我的完整代码是:

import rocksdb
from pprint import pprint
column_families = {}
columns = ["col9", "col8", "col7", "col6", "col5", "col4", "col3", "col2", "col1", "col11", "col0", "col10"]
for column in columns:
    column_families[bytes("{}".format(column), encoding='utf-8')] = rocksdb.ColumnFamilyOptions()

db_path = "db"
db = rocksdb.DB(db_path, rocksdb.Options(create_if_missing=False), column_families = column_families)

print("DB opened.")

it = db.iterkeys()
it.seek_to_first()

keys = list(it)
counter = 0
for key in keys:
    print(key)
    if counter == 10:
        break
    counter += 1

一开始,我只是在阅读数据库,但在包含列族时出现错误,这解决了“无法打开DB”错误,但我没有得到空的键列表。
我怎么拿到所有的钥匙?

wmomyfyw

wmomyfyw1#

你需要指定你要迭代的列族。比如说,如果你想读取所有存储在列族'col9'中的键,你可以尝试以下方法:

col9_cf_handle = db.get_column_family(b'col9')
it = db.iterkeys(col9_cf_handle)
it.seek_to_first()

相关问题