Connect to Neo4j graph databases and execute Cypher queries for efficient knowledge graph management.
This skill enables seamless interaction with Neo4j graph databases using the official Python driver. It provides connection management, query execution, transaction support, and result mapping for the property graph model.
text
bolt:// - Unencrypted connection
neo4j:// - Standard connection (recommended)
neo4j+s:// - TLS-encrypted connection
neo4j+ssc:// - Self-signed certificate
python
config = {
"uri": "neo4j://localhost:7687",
"username": "neo4j",
"password": "secure_password",
"encrypted": True,
"trust": "TRUST_ALL_CERTIFICATES"
}
Neo4j uses a property graph model with three core elements:
Represent entities with labels and properties.
cypher
CREATE (p:Person {name: "Alice", age: 30, email: "alice@example.com"})
CREATE (c:Company {name: "TechCorp", industry: "Technology"})
Properties:
Connect nodes with typed, directed edges and properties.
cypher
CREATE (a:Person)-[:WORKS_AT {since: 2020}]->(c:Company)
CREATE (a:Person)-[:KNOWS {strength: 0.8}]->(b:Person)
Characteristics:
<-Attributes on nodes and relationships.
text
String: "text value"
Integer: 42
Float: 3.14
Boolean: true/false
DateTime: timestamp
List: [1, 2, 3]
cypher
MATCH (p:Person) WHERE p.age > 30 RETURN p.name, p.age
cypher
CREATE (p:Person {name: "Bob", age: 25}) RETURN p
cypher
MERGE (p:Person {name: "Alice"}) SET p.age = 31 RETURN p
cypher
MATCH (a:Person {name: "Alice"}), (b:Person {name: "Bob"})
CREATE (a)-[:KNOWS]->(b) RETURN a, b
cypher
MATCH (p:Person {name: "Old Person"}) DELETE p
cypher
MATCH (p:Person) RETURN p ORDER BY p.age DESC LIMIT 10
cypher
MATCH (p:Person) RETURN COUNT(p), AVG(p.age), MAX(p.age)
cypher
MATCH (p:Person)-[:KNOWS]->(friends:Person)
RETURN p.name, COLLECT(friends.name) AS friend_list
cypher
MATCH (p:Person)
RETURN p.name, CASE WHEN p.age > 30 THEN "Senior" ELSE "Junior" END AS level
cypher
MATCH path = (a:Person)-[:KNOWS*1..3]->(b:Person)
WHERE a.name = "Alice" AND b.name = "Bob"
RETURN path, LENGTH(path) AS hops
cypher
MATCH (n:Person) WHERE exists(n.pagerank) RETURN n ORDER BY n.pagerank DESC
cypher
BEGIN
CREATE (p:Person {name: "Alice"})
CREATE (c:Company {name: "TechCorp"})
COMMIT
cypher
BEGIN
CREATE (p:Person {name: "Alice"})
ROLLBACK
cypher
CREATE INDEX person_name FOR (p:Person) ON (p.name)
CREATE INDEX company_id FOR (c:Company) ON (c.id)
cypher
LOAD CSV WITH HEADERS FROM "file:///data.csv" AS row
CREATE (p:Person {name: row.name, age: toInteger(row.age)})
cypher
UNWIND $nodes AS node
CREATE (n {id: node.id, name: node.name})
cypher
UNWIND $updates AS update
MATCH (p:Person {id: update.id})
SET p.age = update.age
cypher
MATCH (p:Person) RETURN p.name, p.age
Maps to:
python
[
{"name": "Alice", "age": 30},
{"name": "Bob", "age": 25}
]
cypher
MATCH (p:Person) RETURN p
Maps to Python Node objects with:
cypher
MATCH (a)-[r]->(b) RETURN r
Maps to Relationship objects with:
ErrorCauseSolutionConnection refusedNeo4j not runningStart Neo4j serverAuthentication failedWrong credentialsVerify username/passwordSyntax errorInvalid CypherCheck query syntaxConstraint violationDuplicate/invalid dataCheck constraintsTimeoutQuery too slowAdd indexes, optimize queryOut of memoryToo much dataBatch operations, paginate
✓ Use parameterized queries - Prevent injection attacks
✓ Create appropriate indexes - Improve query performance
✓ Batch large imports - Avoid memory exhaustion
✓ Use transactions - Ensure data consistency
✓ Profile queries - Identify performance bottlenecks
✓ Close connections - Prevent resource leaks
✓ Limit result sets - Avoid network overhead
✓ Normalize node names - Prevent duplicate nodes
✓ Document schemas - Maintain data governance
✓ Monitor database - Track performance metrics
This skill integrates with:
neo4j - Official driver (4.x/5.x)neomodel - Python ORM for Neo4jpy2neo - Pythonic interfacecypher-dsl-python - Build Cypher programmaticallyipython-cypher - Jupyter integrationpandas - Data frame operationspolars - Efficient data loadingnetworkx - Graph analysisgraphistry - Interactive graph visualizationpyvis - Network visualizationneovis.js - Neo4j visualizationVersion: 1.0.0