Graphs can model different kinds of networks and linked data coming from many industries, such as logistics and transportation, utility networks, knowledge representation, and text processing.
A graph is made up of a set of vertices and a set of edges. Vertices are the entities in the graph while the edges describe the relationships between vertices.
Each edge connects two vertices - one vertex is denoted as the source and the other as the target. Edges are always directed, but they can be oriented in either direction.
The SAP HANA Cloud Graph Engine applies the use of edges and vertices to analyze complex relationships in business data and processes to boost performance and business understanding. Key features include:

Explore the SAP HANA Cloud Graph engine with the following exercise which uses the dataset from table GX_REVIEWS in the local schema.

1234567ALTER TABLE "GX_EDGES"
ADD FOREIGN KEY ("start") REFERENCES "GX_NODES" ("node_id")
ON UPDATE CASCADE ON DELETE CASCADE ENFORCED VALIDATED;
ALTER TABLE "GX_EDGES"
ADD FOREIGN KEY ("end") REFERENCES "GX_NODES" ("node_id")
ON UPDATE CASCADE ON DELETE CASCADE ENFORCED VALIDATED;
1234567CREATE GRAPH WORKSPACE "GX_DELIVERIES"
EDGE TABLE "GX_EDGES"
SOURCE COLUMN "start"
TARGET COLUMN "end"
KEY COLUMN "edge_id"
VERTEX TABLE "GX_NODES"
KEY COLUMN "node_id" ;
Now explore the GX_DELIVERIES graph.


Analysis of the graph can be enhanced by adding context such as labels and color schemes to highlight relationships. The following steps will change the color of the Vertices (Nodes) for priority deliveries to red.


It’s also possible to use this method to change the Edge settings on the graph. Alter the Edge configuration to display the delivery method and highlight any drone deliveries to red also.


As can be seen on the graph, delivery method edges of type drone are now highlighted in red where applicable.
SAP HANA Cloud Graph provides a new graph calculation node that can be used in calculation scenarios.
This node executes one of the available actions on the given graph workspace and provides the result as table output.
Calculation scenarios can be created with plain SQL as shown in the following section, or with tools such as the SAP HANA Modeler or the native SAP HANA Cloud Graph Viewer.
Calculate the shortest path from one point to another in the graph using the built-in Shortest Path algorithm.
Select the Algorithms icon beside the settings icon to open the algorithms dialog menu.
In the Algorithm drop down option, select Shortest Path.
Enter the following values as the inputs to the algorithm:


Find all nodes which are related to a particular node with the Neighborhood algorithm. The parameters for this algorithm include depth of search, and whether to search for incoming or outgoing edges. Let’s see what other nodes are related to the Mannheim vertex.
In the Algorithm drop-down box, select Neighborhood.
Enter the following values as inputs:


GraphScript is a high-level, powerful, domain-specific language. GraphScript eases the development and integration of complex graph algorithms.
123456789101112131415CREATE TYPE "TT_NODES" AS TABLE ("node_id" NVARCHAR(50), "name" NVARCHAR(50));
CREATE OR REPLACE PROCEDURE "SP_NHOOD"(
IN startV NVARCHAR(50),
IN minDepth INTEGER,
IN maxDepth INTEGER,
OUT res "TT_NODES")
LANGUAGE GRAPH READS SQL DATA AS
BEGIN
GRAPH g = Graph("GX_DELIVERIES");
VERTEX v_s = Vertex(:g, :startV);
MULTISET<VERTEX> ms_n = Neighbors(:g, :v_s, :minDepth, :maxDepth);
res = SELECT :v."node_id", :v."name" FOREACH v IN :ms_n;
END;
1CALL "SP_NHOOD"('Berlin', 0, 2, ?);
Use GraphScript to create a custom traverse algorithm.
123456789101112131415161718192021CREATE TYPE "TT_PRIORITY" AS TABLE
("node_id" NVARCHAR(50), "distance" INTEGER, "hops" BIGINT);
CREATE OR REPLACE PROCEDURE "SP_PRIORITY"
(IN startV NVARCHAR(50), OUT res "TT_PRIORITY")
LANGUAGE GRAPH READS SQL DATA AS
BEGIN
GRAPH g = Graph("GX_DELIVERIES");
VERTEX v_s = Vertex(:g, :startV);
MULTISET<Vertex> rests = v IN Vertices(:g) WHERE :v."IN_SCOPE" == 1;
ALTER g ADD TEMPORARY VERTEX ATTRIBUTE (INT "distance" = 0);
ALTER g ADD TEMPORARY VERTEX ATTRIBUTE (BIGINT "hops" = 0L);
FOREACH rest in :rests {
VERTEX v_rest = Vertex(:g, :rest."node_id");
WeightedPath<INT> p = Shortest_Path(:g, :v_s, :v_rest,
(Edge conn) => INTEGER { return :conn."length"; } );
rest."hops" = Length(:p);
rest."distance" = Weight(:p);
}
res = SELECT :v."node_id", :v."distance", :v."hops" FOREACH v IN :rests;
END;


The result of the GraphScript is the shortest distance in meters to each city plus the number of hops to get there.

Application builders can also apply pattern matching to their solutions. OpenCypher is a declarative graph query language for graph pattern matching developed by the OpenCypher Implementers Group.
Use OpenCypher directly within a SQL statement:
1234567SELECT * FROM OPENCYPHER_TABLE( GRAPH WORKSPACE "GX_DELIVERIES" QUERY
'
MATCH (point1)
WHERE point1.fragile = ''TRUE''
RETURN point1.node_id as destination, point1.fragile as fragile_deliveries
'
) ORDER BY "destination";
12345678SELECT * FROM OPENCYPHER_TABLE( GRAPH WORKSPACE GX_DELIVERIES QUERY
'
MATCH (point1)-[conn]->(point2)
WHERE point1.IN_SCOPE = 1 AND point2.priority = ''TRUE''
RETURN point1.node_id as delivery_start, conn.length as distance,
conn.mode as delivery_type, point2.node_id as delivery_end
'
) ORDER BY "delivery_start";
Well done! This completes the lesson on the SAP HANA Cloud Graph Engine. For further information on this topic, check out the following link.