Python connector
Install, connect, query and handle errors with the Raft DB Python connector (standard library only).
The Python connector speaks the Raft DB binary protocol (specification) and offers the same operations as every other connector. Download the package from the Download page.
No TLS yet. User names, passwords and tokens travel in clear text. Use only on trusted networks.
Requirements#
Python 3.9 or newer. Standard library only.
Install#
Download the wheel from the Download page and install it:
pip install raftdb_python_connector-<version>-py3-none-any.whlThe package contains the module raftdb (import raftdb) and a README.txt.
Connect#
import raftdb
# user=..., password=... or token=... if the server needs them; timeout=30.0 seconds by default
with raftdb.connect("127.0.0.1", 7480) as db:
db.ping()Query#
with raftdb.connect("127.0.0.1", 7480) as db:
db.put("u1", {"name": "Asha", "age": 31}) # dict or JSON text; True if the key already existed
print(db.get("u1")) # dict, or None if absent
print(db.count({"age": {"$gte": 30}}))
for row in db.scan("SELECT name FROM documents ORDER BY name"):
print(row)Paging and indexes#
db.create_index("age", True)
page = db.query({"age": {"$gte": 30}}, limit=10)
print("page rows:", len(page["rows"]), "next:", page["next"]) # next == 0: no more rows
print("count sql:", db.count_sql("SELECT COUNT(*) FROM documents WHERE age >= 40"))Errors#
raftdb.RaftDBError has .code and .message. Network problems are ordinary OSErrors. After an AUTH error or any I/O error (a timeout included) the client closes itself.
try:
db.count('{"age":')
except raftdb.RaftDBError as e:
print(e.code, e.message) # 4 ...Transactions and prepared statements#
The connector has no transaction call and no prepared-statement call: the protocol has none (every operation is one request and one reply, see the specification), and none of the connector's README, tests or examples uses one. Use the MySQL or MariaDB front-end with a standard driver for those: see Standard MySQL and MariaDB drivers.
Notes#
- A client is not thread-safe: use one connection per thread.
scanis a generator; a string that starts withSELECTis SQL.
Conformance status#
24/24 conformance scenarios. The scenarios are listed in the specification page. The package's README.txt has the install steps and a short example.