Introduction
Apache-age is a popular graph database that is widely used for building applications that deal with highly interconnected data. In this blog post, we'll walk you through building a social network app using apache-age. We'll cover the data model, setting up apache-age, creating the graph, adding sample data, and querying the data.
The Data Model
A social network app has many interconnected entities, such as users, posts, comments, and likes. In a graph database like apache-age, we can represent each of these entities as a node, and the relationships between them as edges. Here's an example data model:
- User: A user of the app
- Post: A post made by a user
- Comment: A comment made on a post
- Like: A like given to a post
We'll also have the following relationships between entities:
- FOLLOWS: A user follows another user
- POSTED_BY: A post is made by a user
- COMMENTED_ON: A comment is made on a post
- LIKED: A user likes a post
Here's a diagram of the data model:
┌─────┐ FOLLOWS ┌─────┐
│User │────────>│User │
└─────┘ └─────┘
| |
| POSTED_BY |
v v
┌─────┐ ┌─────┐
│Post │ │User │
└─────┘ └─────┘
| |
| COMMENTED_ON|
v v
┌───────┐ ┌─────┐
│Comment│ │Post │
└───────┘ └─────┘
| |
| LIKED |
v v
┌─────┐ ┌─────┐
│User │ │Post │
└─────┘ └─────┘
In this data model, each node represents an entity in the social network, and each edge represents a relationship between them. For example, a User node might be connected to a Post node by a POSTED edge, indicating that the user created the post. Similarly, a User node might be connected to another User node by a FOLLOWS edge, indicating that the user is following the other user.
Setting up Apache Age
Before we can start building our social network app, we need to set up apache-age. Using the blog at link. Once installed, we can start the apache-age server and open the apache-age viewer in browser.
Creating the Graph
Once we have apache-age set up, we can create the graph using Cypher
select create_graph('social_network');
Here's some Cypher code to create the nodes and edges in our social network graph:
SELECT * from cypher('social_network', $$
CREATE (Alice:User {name: 'Alice'})
CREATE (Bob:User {name: 'Bob'})
CREATE (Charlie:User {name: 'Charlie'})
CREATE (Dave:User {name: 'Dave'})
CREATE (Post1:Post {title: 'My First Post', content: 'Hello world!'})
CREATE (Post2:Post {title: 'My Second Post', content: 'This is my second post.'})
CREATE (Post3:Post {title: 'My Third Post', content: 'Just testing things out.'})
CREATE (Comment1:Comment {content: 'Nice post!'})
CREATE (Comment2:Comment {content: 'I agree.'})
CREATE (Comment3:Comment {content: 'Could use some work.'})
CREATE (Alice)-[:POSTED]->(Post1)
CREATE (Bob)-[:POSTED]->(Post2)
CREATE (Alice)-[:POSTED]->(Post3)
CREATE (Charlie)-[:COMMENTED]->(Comment1)
CREATE (Dave)-[:COMMENTED]->(Comment2)
CREATE (Bob)-[:COMMENTED]->(Comment3)
CREATE (Alice)-[:LIKED]->(Post2)
CREATE (Bob)-[:LIKED]->(Post1)
CREATE (Charlie)-[:LIKED]->(Post2)
CREATE (Dave)-[:LIKED]->(Post3)
CREATE (Alice)-[:FOLLOWS]->(Bob)
CREATE (Alice)-[:FOLLOWS]->(Charlie)
CREATE (Bob)-[:FOLLOWS]->(Charlie)
CREATE (Charlie)-[:FOLLOWS]->(Dave)
$$) as (V agtype);
This code creates nodes for each user and post, comment, and edges to represent the relationships between them. For example, the CREATE (Alice)-[:POSTED]->(Post1) statement creates a POSTED relationship between the Alice node and the Post1 node.
Adding more Sample Data
Now that we have created our graph, we can add some sample data to it. Here's some Cypher code to add sample data:
SELECT * from cypher('social_network', $$
CREATE (Alice)-[:POSTED]->(:Post {title: 'Hello World', content: 'My first post!'})
CREATE (Bob)-[:POSTED]->(:Post {title: 'Second Post', content: 'This is my second post.'})
CREATE (Charlie)-[:POSTED]->(:Post {title: 'Third Post', content: 'Just testing things out.'})
CREATE (Dave)-[:POSTED]->(:Post {title: 'Fourth Post', content: 'Hello everyone!'})
CREATE (Charlie)-[:COMMENTED]->(:Comment {content: 'Nice post!'})
CREATE (Dave)-[:COMMENTED]->(:Comment {content: 'I agree.'})
CREATE (Bob)-[:COMMENTED]->(:Comment {content: 'Could use some work.'})
CREATE (Alice)-[:LIKED]->(:Post {title: 'Second Post'})
CREATE (Bob)-[:LIKED]->(:Post {title: 'Hello World'})
CREATE (Charlie)-[:LIKED]->(:Post {title: 'Second Post'})
CREATE (Dave)-[:LIKED]->(:Post {title: 'Fourth Post'})
CREATE (Alice)-[:FOLLOWS]->(Bob)
CREATE (Alice)-[:FOLLOWS]->(Charlie)
CREATE (Bob)-[:FOLLOWS]->(Charlie)
CREATE (Charlie)-[:FOLLOWS]->(Dave)
$$) as (V agtype);
This code adds more posts, comments, and edges to the graph.
You can query to see the data:
SELECT * from cypher('social_network', $$
MATCH (V)-[R]-(V2)
RETURN V,R,V2
$$) as (V agtype, R agtype, V2 agtype);
So at this point we have completed our data creation, now our model is ready and we can query the data as per our requirements.
To learn more about querying the data you can follow the part 2 of the blog at link.
References
For further help you can look though the documentation here and Apache-age repo here.
Top comments (0)