Rocksdb Python Binding
Maintained. Last release 12 months ago. Weaker on release cadence.
pip install rocksdictRocksDict / SpeeDict
Key-value storage for Python & Wrapper of Rocksdb and Speedb
Installation
Wheels available for macOS amd64/arm64, linux amd64/arm64, and windows amd64.
pip install rocksdictfor rocksdb backend, thenfrom rocksdict import Rdictpip install speedictfor speedb backend, thenfrom speedict import Rdict
Introduction
This library has two purposes.
- As an on-disk key-value storage solution for Python.
- As a RocksDB / Speedict interface.
These two purposes operate in different modes:
-
Default mode, which allows storing
int,float,bool,str,bytes, and other python objects (withPickle). -
Raw mode (
options=Options(raw_mode=True)), which allows storing onlybytes.
Easily inspect RocksDB created by C++, Java, or Other Languages
Since v0.3.24b2.
from rocksdict import Rdict
# This will automatically load latest options and column families.
# Note also that this is automatically RAW MODE,
# as it knows that the db is not created by RocksDict
# (since v0.3.24b2).
db = Rdict("db_path")
# list column families
cfs = Rdict.list_cf("db_path")
print(cfs)
# use one of the column families
cf1 = db.get_column_family(cfs[1])
# iterate through all key-value pairs in cf1
for k, v in cf1.items():
print(f"{k} -> {v}")
# iterate through all wide columns in cf1
for k, v in cf1.entities():
print(f"{k} -> {v}")
Examples
A minimal example
from rocksdict import Rdict
import numpy as np
import pandas as pd
path = str("./test_dict")
# create a Rdict with default options at `path`
db = Rdict(path)
db[1.0] = 1
db["huge integer"] = 2343546543243564534233536434567543
db["good"] = True
db["bytes"] = b"bytes"
db["this is a list"] = [1, 2, 3]
db["store a dict"] = {0: 1}
db[b"numpy"] = np.array([1, 2, 3])
db["a table"] = pd.DataFrame({"a": [1, 2], "b": [2, 1]})
# reopen Rdict from disk
db.close()
db = Rdict(path)
assert db[1.0] == 1
assert db["huge integer"] == 2343546543243564534233536434567543
assert db["good"] == True
assert db["bytes"] == b"bytes"
assert db["this is a list"] == [1, 2, 3]
assert db["store a dict"] == {0: 1}
assert np.all(db[b"numpy"] == np.array([1, 2, 3]))
assert np.all(db["a table"] == pd.DataFrame({"a": [1, 2], "b": [2, 1]}))
# iterate through all elements
for k, v in db.items():
print(f"{k} -> {v}")
# batch get:
print(db[["good", "bad", 1.0]])
# [True, False, 1]
# delete Rdict from dict
db.close()
Rdict.destroy(path)