DEV Community

Namsi Lydia
Namsi Lydia

Posted on

Building and Querying Knowledge Graphs With Apache Age

Introduction
A knowledge graph, also known as a semantic network, represents a network of real-world entities—i.e. objects, events, situations, or concepts—and illustrates the relationship between them. This information is usually stored in a graph database and visualized as a graph structure, prompting the term knowledge “graph.”
In this article we will explore how Apache Age can be used to create knowledge graphs and demonstrate how to implement a sample knowledge graph in Apache Age.

How to understand knowledge graphs
A knowledge graph is made up of three main components: nodes, edges, and properties. Any object, place, or person can be a node. An edge defines the relationship between the nodes.
while properties provide the attributes associated with a node or with an edge. .

Practical Example of Knowledge Graphs in Apache AGE
Use case :Shoe Retail Store
In this use case we are going to create a graph based on shoe purchases across various demographic groups

Step 1:Define the graph data schema
Nodes:
1.Products -(Name,Brand,Size,Price,Color-options,Availability)
2.Category - (Athletic,Casual,Formal)
3.Brand -(Name, Country of Origin, Reputation)
4.Customer -(Name, Age, Gender, Purchase History)
5.Employee -(Name, Role, Contact Information)
6.Store -(Name, Location, Products, Opening Hours)
7.Transaction -(Date, Total Amount, Payment Method)

Edges(relationships):
1.Store-Sells->(Brand)
2.Customer-ServedBy->(employee)
3.Customer-Purchased From->(Store)
4.Brand-Has Model->(ShoeName)
6.Employee-Manages->(store)
7.Customer-Buys->(transaction)

Step 2:define the structure of the graph by populating it with nodes and relations

Firstly to be able to create the nodes and edges will first have to create the graph and to do so in Apache Age we will follow the following steps:

1.connect to the database -PostgreSQl Apache AGE works under the postgresql database then

CREATE EXTENSION IF NOT EXISTS age;
LOAD 'age';
SET search_path = ag_catalog, "$user", public;

2.Create your graph

SELECT create_graph('purchases');

3.Create all the nodes and edges

-- Nodes
select create_vlabel('purchases','Products');
select create_vlabel('purchases','Category');
select create_vlabel('purchases','Customer');
select create_vlabel('purchases','Employee');
select create_vlabel('purchases','Store');
select create_vlabel('purchases','Transaction');

-- Edges
select create_elabel('purchases','SELLS');
select create_elabel('purchases','MANAGES');
select create_elabel('purchases','BUYS');
select create_elabel('purchases','PURCHASED_FROM');
select create_elabel('purchases','HAS_MODEL');
select create_elabel('purchases','SERVED_BY');

Enter fullscreen mode Exit fullscreen mode

3.After creating the nodes and edges we will have to fill the graph with some data this can be done as follows :

select * from cypher('purchases', $$
    CREATE (p:products { name:"Sneakers",brand: "Nike",size:"39", price:"2500",availability:"Yes"})
    SET p.name = "Products"
    RETURN p
$$) as (products agtype);

Enter fullscreen mode Exit fullscreen mode

output:

products                         
--------------------------------------------------------------------------------------------------------------
----------------------------------------------------
 {"id": 4222124650659841, "label": "products", "properties": {"name": "Products", "size": "39", "brand": "Nike
", "price": "2500", "availability": "Yes"}}::vertex
Enter fullscreen mode Exit fullscreen mode

Repeat this step and add data to all the nodes in the graph

next will add relationships(edges) for the various nodes in the graph and this can be done as follows:

select * from cypher('purchases', $$
    MATCH (s:store{name :"Platform",location:"Lavington",products:"Sneakers",opening:"8:00 AM" } )
    MERGE (p:products {name:"Sneakers",brand: "Nike",size:"39", price:"2500",availability:"Yes" })
    MERGE (s)-[:SELLS]->(p)
    RETURN s
$$) as (store agtype);

Enter fullscreen mode Exit fullscreen mode

This is one simple way of adding relationships into your graph you can do this for the rest and create a define knowledge graph to check purchases and purchase behaviour of various consumers in your business.

Apache AGE provides a simplified way to create complex knowledge graphs as we can see from the sample examples above.

in the next series we are going to explore more about semantic searches in knowledge graphs using Apache AGE.

Top comments (0)