Environment Setup
Java Runtime Environment
Ensure a compatible Java version (JRE 17) is installed. Configure the system environment variables by setting JAVA_HOME to the installation directory and appending %JAVA_HOME%\bin to the system PATH. Verify the configuration by executing java -version in a terminal.
Neo4j Database Installation
Download the Neo4j Community Edition from the official distribution center. Initialize the database by running the neo4j console command from the bin directory. The web interface is accessible via the browser at http://localhost:7474.
Python Integration
Install the py2neo client library to interact with the graph database:
pip install py2neo
Database Connection
Establish a connection using the Graph object, providing the URI and authentication credentials.
from py2neo import Graph
# Initialize the connection
db_conn = Graph('bolt://localhost:7687', auth=('neo4j', 'your_password'))
Graph Data Operations
Creating Nodes and Relationships
Nodes can be instantiated with a label and arbitrary property dictionaries. Relationships define the directed edges between nodes.
from py2neo import Node, Relationship
# Define nodes with labels and properties
src_node = Node('Server', hostname='alpha', ip='192.168.1.10')
dst_node = Node('Server', hostname='beta', ip='192.168.1.11')
# Create a relationship with properties
link = Relationship(src_node, 'CONNECTED_TO', dst_node, latency='5ms')
# Persist to database
db_conn.create(src_node)
db_conn.create(dst_node)
db_conn.create(link)
Batch Creation with Subgraphs
Multiple elements can be combined and committed simultaneously using the union operator or by constructing a subgraph.
from py2neo import NodeMatcher
# Fetch existing nodes for demonstration
matcher = NodeMatcher(db_conn)
n1 = matcher.match('Server', hostname='alpha').first()
n2 = matcher.match('Server', hostname='beta').first()
# Define relationship type
LINK_TYPE = Relationship.type('CONNECTED_TO')
# Create subgraph structure
subgraph_structure = LINK_TYPE(n1, n2, status='active')
db_conn.create(subgraph_structure)
Querying the Database
Use NodeMatcher and RelationshipMatcher to find specific graph elements.
from py2neo import NodeMatcher, RelationshipMatcher
node_selector = NodeMatcher(db_conn)
edge_selector = RelationshipMatcher(db_conn)
# Find a specific node by property
target_server = node_selector.match('Server').where(hostname='alpha').first()
# Find relationships based on type and nodes
connections = list(edge_selector.match((target_server, None), r_type='CONNECTED_TO'))
Accessing Properties
Properties and labels of retrieved objects can be accessed directly or via iterator methods.
# Retrieve specific property
ip_address = target_server['ip']
# Check for labels
has_label = 'Server' in target_server.labels
# Get all properties as a dictionary
props_dict = dict(target_server)
Updating Data
Modifications are performed on the local object and must be pushed back to the server to take effect.
# Update property locally
target_server['status'] = 'maintenance'
# Update relationship property
if connections:
connections[0]['latency'] = '10ms'
# Synchronize changes with the database
db_conn.push(target_server)
db_conn.push(connections[0])
Deleting Data
Removing relationships must occur before removing nodes to maintain referential integrity.
# Delete a relationship
db_conn.separate(connections[0])
# Delete a node
db_conn.delete(target_server)
# Clear all data in the database
db_conn.delete_all()