RocksDict: Python On-Disk Key-Value Store

0.3.29 · active · verified Tue Apr 14

RocksDict provides a Python binding for RocksDB, offering an efficient on-disk key-value storage solution. It enables users to store, query, and delete a large number of key-value pairs that may not fit into RAM. The library supports storing various Python objects (with Pickle) in its default mode and raw bytes in its raw mode. It also functions as an interface to inspect RocksDB databases created by other languages. The current version is 0.3.29, with a release cadence that includes frequent updates for Python version support and new features.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a RocksDict, store various data types, retrieve values, close and reopen the database, iterate through its contents, and finally clean up by destroying the database.

import os
from rocksdict import Rdict, Options

path = str("./my_test_db")

# Ensure clean start for example
if os.path.exists(path):
    Rdict(path).destroy()

# Create an Rdict with default options
db = Rdict(path)

# Store various Python objects
db[1] = "value_one"
db["key_two"] = 25
db[b"binary_key"] = b"binary_value"
db["list_key"] = [1, 2, 3]
db["dict_key"] = {"a": 1, "b": 2}

print(f"Value for key_two: {db['key_two']}")

# Reopen Rdict from disk after closing
db.close()
db = Rdict(path)

print(f"Value for list_key after reopen: {db['list_key']}")

# Iterate through items
print("\nItems in the database:")
for k, v in db.items():
    print(f"{k} -> {v}")

# Delete an item
del db[1]
assert 1 not in db

# Destroy the database (clean up)
db.destroy()

view raw JSON →