title | nav_order | description |
---|---|---|
Getting Started |
2 |
Getting Started with FalkorDB Graph Database.
|
This guide will walk you through setting up FalkorDB, modeling a social network as a graph, and accessing it using the FalkorDB Python client with Cypher.
-
FalkorDB Instance: Set up FalkorDB (on-prem or cloud).
-
Python Installed: Ensure you have Python 3.8+ installed.
-
Install FalkorDB Python Client:
pip install falkordb
Let's create a simple graph for a social network where:
- Nodes represent
User
andPost
. - Relationships connect
User
s with aFRIENDS_WITH
relationship, andUser
s are connected via aCREATED
relationship toPost
s
Node Type | Properties |
---|---|
User | id , name , email |
Post | id , content , date |
Relationship Type | Start Node | End Node | Properties |
---|---|---|---|
FRIENDS_WITH | User | User | since |
CREATED | User | Post | time |
Here’s how you can model and load the data.
CREATE (alice:User {id: 1, name: "Alice", email: "[email protected]"})
CREATE (bob:User {id: 2, name: "Bob", email: "[email protected]"})
CREATE (charlie:User {id: 3, name: "Charlie", email: "[email protected]"})
CREATE (post1:Post {id: 101, content: "Hello World!", date: 1701388800})
CREATE (post2:Post {id: 102, content: "Graph Databases are awesome!", date: 1701475200})
CREATE (alice)-[:FRIENDS_WITH {since: 1640995200}]->(bob)
CREATE (bob)-[:FRIENDS_WITH {since: 1684108800}]->(charlie)
CREATE (alice)-[:CREATED {time: 1701388800}]->(post1)
CREATE (bob)-[:CREATED {time: 1701475200}]->(post2)
You can execute these commands using the FalkorDB Python client.
from falkordb import FalkorDB
# Connect to FalkorDB
client = FalkorDB(host="localhost", port=6379, password="your-password")
graph = client.select_graph('social')
create_query = """
CREATE (alice:User {id: 1, name: "Alice", email: "[email protected]"})
CREATE (bob:User {id: 2, name: "Bob", email: "[email protected]"})
CREATE (charlie:User {id: 3, name: "Charlie", email: "[email protected]"})
CREATE (post1:Post {id: 101, content: "Hello World!", date: 1701388800})
CREATE (post2:Post {id: 102, content: "Graph Databases are awesome!", date: 1701475200})
CREATE (alice)-[:FRIENDS_WITH {since: 1640995200}]->(bob)
CREATE (bob)-[:FRIENDS_WITH {since: 1684108800}]->(charlie)
CREATE (alice)-[:CREATED {time: 1701388800}]->(post1)
CREATE (bob)-[:CREATED {time: 1701475200}]->(post2)
"""
graph.query(create_query)
print("Graph created successfully!")
# Find all friends of Alice
query = """
date and time as they are right now can be confusing, either use Python to create timestamps from actual dates or consider changing the attribute to something else which doesn't require time / date datatype
MATCH (alice:User {name: "Alice"})-[:FRIENDS_WITH]->(friend)
RETURN friend.name AS Friend
"""
result = graph.ro_query(query)
print("Alice's friends:")
for record in result:
print(record["Friend"])
# Find posts created by Bob
query = """
MATCH (bob:User {name: "Bob"})-[:CREATED]->(post:Post)
RETURN post.content AS PostContent
"""
result = graph.ro_query(query)
print("Posts created by Bob:")
for record in result:
print(record["PostContent"])
Congratulations! 🎉 You have successfully modeled, loaded, and queried a social network graph with FalkorDB.
Next, dive deeper into FalkorDB's powerful features:
For questions or support, visit our community forums