In this SAP HANA Cloud tutorial, we will discuss SAP HANA Cloud Graph and its concept with how to create a graph workspace, and also create and visualize the graph using the graph viewer.
- What is SAP HANA Cloud Graph
- SAP HANA Cloud Graph Workspace
- Creating SAP HANA Cloud Graph-Based Table
- How to create SAP HANA Cloud Graph Workspace
- How to open SAP HANA Cloud Graph Viewer
- How to customize the SAP HANA Cloud Graph
Also, Read: How to Use Select Statement in SAP HANA Cloud
What is SAP HANA Cloud Graph
The main capability of SAP HANA includes SAP HANA Graph. It adds native functionality for graph processing to SAP HANA and enables you to perform standard graph operations on data stored in an SAP HANA system.
In computing, a graph database represents and stores data using nodes, edges, and characteristics. The graph explains the connections (=edges) between the various entities (=nodes), as well as all the things’ characteristics.
- Graphs are a strong abstraction that may be used to model many network types and connected data from a variety of industries, including logistics and transportation, utility networks, knowledge representation, text processing, and others.
- A set of vertices and a set of edges make up a graph in SAP HANA. Each edge joins two vertices, the source vertex and the target vertex are identified by their respective symbols. There may be two or more edges linking the same two vertices, and edges are always directed.
- The amount of properties that vertices and edges can have is infinite. A name connected to a data type and a value make up a vertex attribute.
Through the relationships, data can be directly linked and retrieved in a single process. In graph databases, the connections between the data are given top attention. Because relationships are continuously saved in the database, querying relationships is quick.
Graph databases are excellent for highly connected data because they allow for the intuitive visualization of relationships.
Since data need not be stored in pre-determined formats, graph databases, a subset of NoSQL databases, can be extremely flexible. As they go, users can gradually add or delete characteristics from edges and nodes.
NoSQL databases are not SAP HANA databases. In SAP HANA, a schema contains all of the data. How then do we provide graph capabilities?
- The vertex table and edge table, which are sets of vertices and edges, respectively, are where all data are kept. The columns of the vertex table correspond to the vertex attributes. Edge table columns correspond to edge attributes.
- The vertex key, one of the vertex properties, uniquely identifies vertices.
- The edge key, one of the edge properties, uniquely identifies edges.
Each edge’s source vertex and target vertex are listed in two additional columns in the edge table.
Currently, It is referred to as tables, but they are not necessarily actual tables. They might be built on views that combine data from various tables without replicating them. They might also be built on virtual tables that connect to information kept in another database.
Relational storage enables the application of all SAP HANA features, including access control, backup, and recovery, to the graph data.
Additionally, it enables the use of all SAP HANA Graph functions on graph data from business applications that are stored in relational format. For defining a graph in terms of the current SAP HANA tables, SAP HANA Graph offers a specialized catalog item known as a graph workspace.
Check out: Operators in SAP HANA Cloud
SAP HANA Cloud Graph Workspace
An object from a catalog called a graph workspace defines a graph in terms of vertex and edge tables.
- One or more vertex tables and one or more edge tables make up a graph workspace, both vertex tables and edge tables can be specified using the same table. The same data type must be used for all vertex characteristics with the same name. Similarly to this, edge attributes must have the same data type as their names indicate.
- A label can be supplied as an option for each graph table. Without a label supplied, the default label is the name of the relevant vertex or edge table. Within a specific graph workspace, vertex labels and edge labels must be distinct from one another.
- The related label and one or more vertex attributes, known as the vertex key attributes, uniquely identify each vertex. Similarly, the label and one or more edge qualities known as the edge key attributes serve to identify each edge uniquely.
- A given edge table’s source vertices must all make reference to vertices from the same vertex table within the specified graph workspace. Similar to this, each target vertex described in a given edge table must make reference to a vertex from the same vertex table within the specified graph workspace.
- Therefore, it is necessary to define the vertex table that is referred to by the source and target vertices of each edge table if a graph workspace comprises multiple vertex tables.
- One or more edge properties referred to as source and target attributes, respectively uniquely identify the source and target vertices for each edge. The source and target attributes’ data types and order must coincide with the referenced vertex table’s key attributes’ data types and ordering.
- The database schema it sits in and the workspace name together serve as a unique identifier for a graph workspace. Multiple graph workspaces with the same schema but different workspace names or separate database schemas may be present in an SAP HANA instance.
Creating SAP HANA Cloud Graph-Based Table
Now we know what is a graph in SAP HANA Cloud, let’s see how to create a table based on the graph.
Connect to your database in the SQL Editor of SAP HANA Database Explorer. Although you can choose any other schema of your choice, it is assumed in these exercises that you are using a schema called Railway station.
- Each station has an identification number that will turn it into a vertex on our network. They also possess the following properties:
- You start the journey by purchasing a ticket at the railticket_office, which is set to ‘TRUE’ (string, not boolean).
- A stop for a superb coffee and snacks can be taken in the rail_restaurant “TRUE.”
- Rail lines run between stations. These lines will be graph edges with the following characteristics:
- start and end will be the station ids that are connected.
- The attribute length for each of them will be measured in meters.
Write the following code into the SQL console on SAP HANA Database.
CREATE SCHEMA "RAIL";
SET SCHEMA "RAIL";
CREATE TABLE "STNODES"(
"stnode_id" INTEGER NOT NULL,
"stname" NVARCHAR(16),
"stticket_office" NVARCHAR(4),
"strestaurant" NVARCHAR(4),
PRIMARY KEY (
"stnode_id"
)
);
CREATE TABLE "STEDGES"(
"stedge_id" INTEGER NOT NULL,
"stlength" INTEGER,
"stdifficulty" NVARCHAR(16),
"ststart" INTEGER NOT NULL,
"stend" INTEGER NOT NULL,
"stmode" NVARCHAR(8),
"status" NVARCHAR(8),
PRIMARY KEY (
"stedge_id"
)
);
ALTER TABLE "STEDGES" ADD FOREIGN KEY ("ststart") REFERENCES "STNODES" ("stnode_id") ON UPDATE CASCADE ON DELETE CASCADE ENFORCED VALIDATED
;
ALTER TABLE "STEDGES" ADD FOREIGN KEY ( "stend" ) REFERENCES "STNODES" ("stnode_id") ON UPDATE CASCADE ON DELETE CASCADE ENFORCED VALIDATED
;
After creating the tables select the schema to view your created tables as shown in the below picture.
After selecting the schema, you see your created tables as shown in the below picture.
Now we are going to insert the following data into both STNODES and STEDGES that we have created.
Insert the data into the STNODES table.
SET SCHEMA "RAIL";
INSERT INTO "STNODES" VALUES (1, 'Lathrop', 'TRUE', '') ;
INSERT INTO "STNODES" VALUES (2, 'Tracy', '', 'TRUE') ;
INSERT INTO "STNODES" VALUES (3, 'Vasco', '', 'TRUE') ;
INSERT INTO "STNODES" VALUES (4, 'Livermore', 'TRUE', '') ;
INSERT INTO "STNODES" VALUES (5, 'Pleasanton', '', 'TRUE') ;
INSERT INTO "STNODES" VALUES (6, 'Fremont', '', '') ;
INSERT INTO "STNODES" VALUES (7, 'Great America', '', '') ;
INSERT INTO "STNODES" VALUES (8, 'Santa Clara', '', '') ;
INSERT INTO "STNODES" VALUES (9, 'San Jose', '', '') ;
INSERT INTO "STNODES" VALUES (10, 'Old North', '', '') ;
INSERT INTO "STNODES" VALUES (11, 'Midtown', '', '') ;
INSERT INTO "STNODES" VALUES (12, 'Elk Grove', '', '') ;
INSERT INTO "STNODES" VALUES (13, 'Manteca', '', '') ;
INSERT INTO "STNODES" VALUES (14, 'Ripon', '', '') ;
INSERT INTO "STNODES" VALUES (15, 'Modesto', 'TRUE', '') ;
Again insert the data into the STEDGES table.
INSERT INTO "STEDGES" VALUES (61, 1755, '', 1, 2, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (62, 1718, '', 2, 3, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (60, 2453, '', 4, 5, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (63, 123, '', 12, 2, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (64, 1050, '', 12, 6, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (65, 1041, '', 8, 7, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (66, 365, '', 9, 7, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (67, 1312, '', 11, 3, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (68, 360, '', 9, 10, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (69, 1365, '', 13, 10, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (70, 1223, '', 14, 13, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (71, 1151, '', 15, 14, 'stopped', 'open') ;
INSERT INTO "STEDGES" VALUES (100, 2600, 'black', 5, 4, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (101, 3100, 'red', 3, 2, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (102, 2500, 'black', 3, 2, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (103, 2000, 'black', 3, 9, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (104, 2100, 'red', 3, 9, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (105, 1200, 'red', 9, 11, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (106, 2000, 'red', 7, 11, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (107, 1800, 'red', 7, 8, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (108, 700, 'red', 7, 5, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (1108, 1000, 'red', 5, 8, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (109, 1900, 'blue', 6, 2, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (110, 200, 'blue', 2, 12, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (111, 700, 'blue', 10, 9, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (112, 1500, 'red', 10, 13, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (113, 700, 'black', 3, 5, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (114, 600, 'blue', 6, 2, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (116, 2100, 'red', 3, 9, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (117, 2300, 'black', 12, 4, 'running', 'open') ;
INSERT INTO "STEDGES" VALUES (118, 200, 'blue', 6, 5, 'running', 'open') ;
We have inserted the data, and now preview the data. To view the table data, first, select the table and click on the button Open Data as shown in the below picture.
Read: SAP HANA Cloud ABAP Environment
How to create SAP HANA Cloud Graph Workspace
Write or copy the below code into your SQL console or editor to create a graph workspace.
SET SCHEMA "RAIL";
CREATE GRAPH WORKSPACE "RAIL"
EDGE TABLE "STEDGES"
SOURCE COLUMN "ststart"
TARGET COLUMN "stend"
KEY COLUMN "stedge_id"
VERTEX TABLE "STNODES"
KEY COLUMN "stnode_id"
;
In your schema’s Graph Workspaces folder, locate the recently established RAIL workspace. To view its definition, click on it.
How to open SAP HANA Cloud Graph Viewer
View the Graph by right-clicking on the RAIL graph workspace.
The graph viewer will launch and load the default graph view.
Keep in mind that every time a graph is rendered, the nodes and edges will seem different.
How to customize the SAP HANA Cloud Graph
Click on the setting icon as shown below picture and you can customize the vertex, edges and etc.
By customizing the vertex, edge, and preferences, you can change the appearance of the graph as you want.
Also, you may like some more SAP tutorials:
- How to Create a Foreign Key in SAP ABAP
- How to Create SAP ABAP List View Report (ALV)
- How to Create Table Maintenance Generator in SAP ABAP
Conclusion
In this SAP HANA Cloud, we have learned about SAP HANA Cloud Graph and how to create a graph workspace, then created graph based table. Also learned about graph viewer and how to visualize and customize the graph.
- What is SAP HANA Cloud Graph
- SAP HANA Cloud Graph Workspace
- Creating SAP HANA Cloud Graph-Based Table
- How to create SAP HANA Cloud Graph Workspace
- How to open SAP HANA Cloud Graph Viewer
- How to customize the SAP HANA Cloud Graph
I am Chris Waldron, working as a Senior SAP HANA Consultant at Halliburton, Houston, Texas, United States. I have been working in SAP for more than 15 years, especially in SAP IT consulting and business consulting. I worked in various industries in Sales & Distribution, Customer Relationship Management, banking, Risk Management, etc. And I am an SAP Certified Development Specialist – ABAP for SAP HANA 2.0 and SAP HANA Modeling Certified consultant. Read more