Skip to main content

Getting Started with FalkorDB

This guide will walk you through setting up FalkorDB, modeling a social network as a graph, and accessing it using one of the FalkorDB client libraries with the Cypher query language.


Prerequisites

  1. FalkorDB Instance: Set up FalkorDB (on-prem or cloud).
  2. Install FalkorDB Client:
pip install falkordb

Step 1: Model a Social Network as a Graph

Let's create a simple graph for a social network where:

  • Nodes represent User and Post.
  • Relationships connect Users with a FRIENDS_WITH relationship, and Users are connected via a CREATED relationship to Posts

Graph Schema

Node Types:

Node TypePropertiesDescription
Userid, name, emailRepresents a user in the social network
Postid, content, dateRepresents a post created by a user

Relationship Types:

Relationship TypeStart NodeEnd NodePropertiesDescription
FRIENDS_WITHUserUsersinceIndicates friendship between two users
CREATEDUserPosttimeConnects a user to their created posts

FalkorDB-Model a Social Network as a Graph


Step 2: Load Data into FalkorDB

Here's how you can model and load the data.

Cypher Query to Create the Data

You can execute these commands using the FalkorDB Python client or any supported client.


Step 3: Access Your Data

Connect to FalkorDB

from falkordb import FalkorDB

# Connect to FalkorDB
client = FalkorDB(host="localhost", port=6379, password="your-password")
graph = client.select_graph('social')

Execute Cypher Queries

Create the Graph

create_query = """
CREATE (alice:User {id: 1, name: "Alice", email: "alice@example.com"})
CREATE (bob:User {id: 2, name: "Bob", email: "bob@example.com"})
CREATE (charlie:User {id: 3, name: "Charlie", email: "charlie@example.com"})

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!")

image

Query the Graph

# Find all friends of Alice
query = """
MATCH (alice:User {name: 'Alice'})-[:FRIENDS_WITH]->(friend)
RETURN friend.name AS Friend
"""

result = graph.ro_query(query).result_set

print("Alice's friends:")
for record in result:
print(record[0])

Query Relationships

# Find posts created by Bob
query = """
MATCH (bob:User {name: 'Bob'})-[:CREATED]->(post:Post)
RETURN post.content AS PostContent
"""

result = graph.ro_query(query).result_set

print("Posts created by Bob:")
for record in result:
print(record[0])

Step 4: Explore Further

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