Neo4j Rust Extensions
neo4j-rust-ext is an official Python library that provides Rust extensions to accelerate the Neo4j Bolt driver for Python. It offers significant performance improvements, often 3x to 10x faster, particularly for use-cases involving few but large records. The library is a drop-in replacement for the standard `neo4j` driver, currently at version 6.1.0.0, and typically releases in alignment with major `neo4j` driver versions.
Common errors
-
IOError: Broken pipe
cause This error often occurs during large data transfers or extensive write operations between the Python driver and the Neo4j database, indicating a communication breakdown.fixFor very large datasets, consider optimizing your Cypher queries, batching operations, or using Neo4j's native data import tools (e.g., `LOAD CSV`) or `.cypher` scripts instead of streaming directly from Python. -
neo4j.exceptions.AuthError: The client is unauthorized to access the database. Please check your credentials.
cause Incorrect username, password, or an authentication mechanism mismatch when connecting to the Neo4j database.fixVerify that the `username` and `password` passed to `GraphDatabase.driver` are correct for your Neo4j instance. Also, ensure the URI is correct (e.g., `bolt://localhost:7687` for local connections). -
neo4j.exceptions.ServiceUnavailable: Failed to establish connection to 'bolt://<host>:<port>': [Errno 111] Connection refused
cause The Neo4j database server is not running, is inaccessible from the client machine, or the specified host/port is incorrect.fixEnsure your Neo4j database is running and reachable from where your Python application is executed. Check firewall settings and the Neo4j configuration for the correct Bolt port (default is 7687). -
Failed to build neo4j-rust-ext
cause Pre-built wheels for your specific operating system or Python version are not available on PyPI, and your environment lacks the necessary Rust toolchain or C build tools to compile the extension from source.fixInstall the Rust toolchain (version 1.65.0 or newer) and platform-specific C build tools (e.g., `sudo apt install gcc` on Ubuntu, or Visual Studio Build Tools on Windows). Refer to `PyO3` and `Maturin` documentation for detailed build environment setup.
Warnings
- breaking The version of `neo4j-rust-ext` (excluding its last segment for patches) *must* match the major.minor.patch version of the `neo4j` driver it enhances for compatibility.
- gotcha For pre-release versions of the `neo4j` driver (e.g., `X.Y.ZaA`), the `neo4j-rust-ext` versioning requires an explicit `N` instead of `*` (e.g., `X.Y.Z.NaA`).
- gotcha When troubleshooting issues, consider temporarily uninstalling `neo4j-rust-ext` to isolate whether the problem lies with the base `neo4j` driver or the Rust extensions.
- gotcha Reliance on internal APIs of the `neo4j` driver might lead to breakage in minor or patch releases, even with the Rust extension enabled.
Install
-
pip install neo4j-rust-ext
Imports
- GraphDatabase
from neo4j import GraphDatabase
Quickstart
import os
from neo4j import GraphDatabase, RoutingControl
URI = os.environ.get('NEO4J_URI', 'bolt://localhost:7687')
USERNAME = os.environ.get('NEO4J_USERNAME', 'neo4j')
PASSWORD = os.environ.get('NEO4J_PASSWORD', 'password')
DATABASE = os.environ.get('NEO4J_DATABASE', 'neo4j')
class Neo4jApp:
def __init__(self, uri, username, password):
self.driver = GraphDatabase.driver(uri, auth=(username, password))
self.driver.verify_connectivity()
def close(self):
self.driver.close()
def add_friend(self, name, friend_name):
# Queries are run against the default database unless specified.
# Explicitly setting database_='neo4j' for clarity, adjust as needed.
self.driver.execute_query(
"MERGE (a:Person {name: $name}) "
"MERGE (friend:Person {name: $friend_name}) "
"MERGE (a)-[:KNOWS]->(friend)",
name=name,
friend_name=friend_name,
database_=DATABASE,
)
def print_friends(self, name):
records, _, _ = self.driver.execute_query(
"MATCH (a:Person)-[:KNOWS]->(friend) WHERE a.name = $name "
"RETURN friend.name ORDER BY friend.name",
name=name,
database_=DATABASE,
routing_=RoutingControl.READ,
)
for record in records:
print(record["friend.name"])
if __name__ == "__main__":
app = Neo4jApp(URI, USERNAME, PASSWORD)
print("Adding friends...")
app.add_friend("Arthur", "Guinevere")
app.add_friend("Arthur", "Lancelot")
app.add_friend("Arthur", "Merlin")
print("Arthur's friends:")
app.print_friends("Arthur")
app.close()