Quick Start¶
This guide covers getting up and running with Grafeo in just a few minutes.
Create a Database¶
Add Data¶
Use GQL to insert nodes and edges:
# Create nodes
db.execute("""
INSERT (:Person {name: 'Alix', age: 30})
INSERT (:Person {name: 'Gus', age: 25})
INSERT (:Person {name: 'Harm', age: 35})
""")
# Create edges
db.execute("""
MATCH (a:Person {name: 'Alix'}), (b:Person {name: 'Gus'})
INSERT (a)-[:KNOWS {since: 2020}]->(b)
""")
db.execute("""
MATCH (b:Person {name: 'Gus'}), (c:Person {name: 'Harm'})
INSERT (b)-[:KNOWS {since: 2022}]->(c)
""")
let mut session = db.session();
// Create nodes
session.execute(r#"
INSERT (:Person {name: 'Alix', age: 30})
INSERT (:Person {name: 'Gus', age: 25})
INSERT (:Person {name: 'Harm', age: 35})
"#)?;
// Create edges
session.execute(r#"
MATCH (a:Person {name: 'Alix'}), (b:Person {name: 'Gus'})
INSERT (a)-[:KNOWS {since: 2020}]->(b)
"#)?;
session.execute(r#"
MATCH (b:Person {name: 'Gus'}), (c:Person {name: 'Harm'})
INSERT (b)-[:KNOWS {since: 2022}]->(c)
"#)?;
Query Data¶
Retrieve data using pattern matching:
# Find all people
result = db.execute("""
MATCH (p:Person)
RETURN p.name, p.age
ORDER BY p.age
""")
for row in result:
print(f"{row['p.name']} is {row['p.age']} years old")
# Find who Alix knows
result = db.execute("""
MATCH (a:Person {name: 'Alix'})-[:KNOWS]->(friend)
RETURN friend.name
""")
for row in result:
print(f"Alix knows {row['friend.name']}")
# Find friends of friends
result = db.execute("""
MATCH (a:Person {name: 'Alix'})-[:KNOWS]->()-[:KNOWS]->(fof)
RETURN DISTINCT fof.name
""")
for row in result:
print(f"Friend of friend: {row['fof.name']}")
let mut session = db.session();
// Find all people
let result = session.execute(r#"
MATCH (p:Person)
RETURN p.name, p.age
ORDER BY p.age
"#)?;
for row in result.rows {
println!("{:?}", row);
}
// Find who Alix knows
let result = session.execute(r#"
MATCH (a:Person {name: 'Alix'})-[:KNOWS]->(friend)
RETURN friend.name
"#)?;
for row in result.rows {
println!("Alix knows {:?}", row);
}
Update Data¶
Modify existing nodes and edges:
Delete Data¶
Remove nodes and edges:
Transactions¶
For atomic operations, use transactions:
# Begin a transaction
with db.begin_transaction() as tx:
tx.execute("INSERT (:Person {name: 'Vincent'})")
tx.execute("INSERT (:Person {name: 'Jules'})")
tx.commit() # Both inserts committed atomically
# Or rollback on error
with db.begin_transaction() as tx:
tx.execute("INSERT (:Person {name: 'Mia'})")
tx.rollback() # Changes discarded
Next Steps¶
- First Graph - Build a complete graph application
- GQL Query Language - Learn more about queries
- Python API - Python-specific features
- Rust API - Rust-specific features