DEV Community

Kamlesh Kumar
Kamlesh Kumar

Posted on

Apache-age: A Powerful and Open Source Graph Database Solution. [Part # 2]

Querying the Data

Welcome back to our series on building a social network app with apche-age! In our previous blog post, we covered the basics of data modeling, setting up Neo4j, creating the graph, and adding sample data. Now that we have our graph populated with users, posts, comments, and relationships, it's time to start querying the data. In this post, we'll explore some example queries for retrieving useful information from our social network app. If you haven't read our previous post yet, we recommend starting there to get caught up on the basics of building a social network app with apche-age here

Query to get all posts

SELECT * from cypher('social_network', $$

        MATCH (p:Post)
        RETURN p.title, p.content

$$) as (V agtype, C agtype);
Enter fullscreen mode Exit fullscreen mode

This query returns the title and content of all posts in the graph.

Image description

Get all posts and their comments

SELECT * from cypher('social_network', $$
        MATCH (V)-[R:COMMENTED]-(V2)
        RETURN V,R,V2
$$) as (V agtype, R agtype, V2 agtype);
Enter fullscreen mode Exit fullscreen mode

This query returns the title of each post and the content of each comment on that post.

Image description

Get all posts and their likes

SELECT * from cypher('social_network', $$
       MATCH (p:Post)<-[:LIKED]-(u:User)
       RETURN p.title, COUNT(u) as likes
$$) as (V agtype, R agtype);

Enter fullscreen mode Exit fullscreen mode

This query returns the title of each post and the number of likes it has.

Image description

Get all posts and their authors

SELECT * from cypher('social_network', $$

    MATCH (u:User)-[:POSTED]->(p:Post)
    RETURN p.title, u.name

$$) as (V agtype, R agtype);

Enter fullscreen mode Exit fullscreen mode

This query returns the title of each post and the name of its author.

Image description

Get all users and the users they follow

SELECT * from cypher('social_network', $$
       MATCH (u:User)-[:FOLLOWS]->(f:User)
       RETURN u.name, COLLECT(f.name) as following
$$) as (V agtype, R agtype);
Enter fullscreen mode Exit fullscreen mode

This query returns the name of each user and a list of the names of the users they follow.

Image description

References

For further help you can look though the documentation here and Apache-age repo here.

Top comments (0)