Try Out Multi-Model Functionality with the SAP HANA Database Explorer
- How to create a graph, a document store, and import spatial data.
- How the SAP HANA database explorer can be used with multi-model data.
Prerequisites
- An SAP HANA database such as SAP HANA Cloud trial or the SAP HANA, express edition that includes the SAP HANA database explorer
- You have completed the first 3 tutorials in this group.
A graph can be used to show the connections between items such as the connections between airports or between people or groups in a social network.
SAP HANA provides the ability to store and perform queries on spatial data such as a point, a line segment, or a polygon.
This tutorial is meant to be an introduction to this topic. For a deeper dive on the topics of graph and spatial, see the tutorial groups Introduction to SAP HANA Spatial Data Types and Smart Multi-Model Data Processing with SAP HANA Cloud.
- Step 1
The following steps will create a graph workspace that can display the distance between hotels in a state.
In SAP HANA, a graph is made up of a set of vertices and a set of edges. Vertices are stored in vertex tables, while edges are stored in edge tables. Vertex and edge tables are collectively denoted as graph tables.
-
Create a vertex table that represents distances between hotels by executing the following in the SQL Console.
SQLCopyCREATE COLUMN TABLE DISTANCES( DKEY INTEGER UNIQUE NOT NULL, HSOURCE INTEGER NOT NULL REFERENCES HOTEL(HNO), HTARGET INTEGER NOT NULL REFERENCES HOTEL(HNO), DIST_KM DOUBLE );
-
Populate the vertex table with distances between hotels that are in the same state.
SQLCopy--Washington INSERT INTO DISTANCES VALUES (1, 10, 11, 11.8); INSERT INTO DISTANCES VALUES (2, 11, 10, 11.8); --New York INSERT INTO DISTANCES VALUES (3, 12, 13, 217.3); INSERT INTO DISTANCES VALUES (4, 13, 12, 217.3); INSERT INTO DISTANCES VALUES (5, 12, 14, 71.9); INSERT INTO DISTANCES VALUES (6, 14, 12, 71.9); INSERT INTO DISTANCES VALUES (7, 12, 15, 71.5); INSERT INTO DISTANCES VALUES (8, 15, 12, 71.5); INSERT INTO DISTANCES VALUES (9, 13, 14, 212.2); INSERT INTO DISTANCES VALUES (10, 14, 13, 212.2); INSERT INTO DISTANCES VALUES (11, 13, 15, 212.1); INSERT INTO DISTANCES VALUES (12, 15, 13, 212.1); INSERT INTO DISTANCES VALUES (13, 14, 15, 0.4); INSERT INTO DISTANCES VALUES (14, 15, 14, 0.4); --Illinois INSERT INTO DISTANCES VALUES (15, 16, 17, 23.5); INSERT INTO DISTANCES VALUES (16, 17, 16, 23.5); --Florida INSERT INTO DISTANCES VALUES (17, 18, 19, 219.8); INSERT INTO DISTANCES VALUES (18, 19, 18, 219.8); INSERT INTO DISTANCES VALUES (19, 18, 20, 323.4); INSERT INTO DISTANCES VALUES (20, 20, 18, 323.4); INSERT INTO DISTANCES VALUES (21, 19, 20, 333.8); INSERT INTO DISTANCES VALUES (22, 20, 19, 333.8); --California INSERT INTO DISTANCES VALUES (23, 21, 22, 149.5); INSERT INTO DISTANCES VALUES (24, 22, 21, 149.5); INSERT INTO DISTANCES VALUES (25, 21, 23, 35.7); INSERT INTO DISTANCES VALUES (26, 23, 21, 35.7); INSERT INTO DISTANCES VALUES (27, 22, 23, 163.2); INSERT INTO DISTANCES VALUES (28, 23, 22, 163.2);
-
Create a graph workspace.
SQLCopyCREATE GRAPH WORKSPACE DISTANCEGRAPH EDGE TABLE DISTANCES SOURCE COLUMN HSOURCE TARGET COLUMN HTARGET KEY COLUMN DKEY VERTEX TABLE HOTEL KEY COLUMN HNO;
-
Navigate to graph workspaces, select the previously created graph workspace, and open it to view its properties.
For additional information, see SAP HANA Cloud, SAP HANA Database Graph Reference.
Log in to complete tutorial -
- Step 2
-
Open the graph viewer.
The graph viewer will open in a new tab as shown below.
-
Set the vertex and edge names using the graph viewer settings.
Set the vertex label to
NAME
.Set the edge label to
DIST_KM
. -
Optionally, adjust a few of the graph vertices to accommodate viewing by dragging and dropping graph vertices.
-
Apply a filter to vertices where
STATE
is NY. After specifying the filter, press the Add button and then apply it by pressing the Apply button.Apply a filter to edges where
DIST_KM
is less than 100. -
Highlight the Long Island vertex using the graph viewer settings. The color used to highlight in the image below is #E5F5FC.
Additional graph examples include the Greek Mythology Graph Example and Open Flights. The company graph example does not currently display in the SAP HANA database explorer graph viewer as it does not currently support the display of homogeneous graphs.
Log in to complete tutorial -
- Step 3
The shortest path algorithm can be used to provide the optimal route between two vertices. The nearest neighbor algorithm can be used to show only the vertices that are connected to a specified vertex.
The following steps will walk through using the shortest path algorithm to determine the optimal route from Airport Hotel in Rosemont, IL to Regency Hotel in Seattle, WA.
Execute the following in SQL to add a few connections between hotels in different states.
SQLCopy--Midtown New York to Lake Michigan Chicago INSERT INTO DISTANCES VALUES (29,14,16,1227); INSERT INTO DISTANCES VALUES (30,16,14,1227); --Long Island New York to Lake Michigan Chicago INSERT INTO DISTANCES VALUES (31,12,16,1357); INSERT INTO DISTANCES VALUES (32,16,12,1357); --Long Island New York to Beach Florida INSERT INTO DISTANCES VALUES (33,12,19,1738); INSERT INTO DISTANCES VALUES (34,19,12,1738); --Congress Seattle to Star California INSERT INTO DISTANCES VALUES (35,10,23,1817); INSERT INTO DISTANCES VALUES (36,23,10,1817); --Indian Horse California to Beach Florida INSERT INTO DISTANCES VALUES (37,22,19,3861); INSERT INTO DISTANCES VALUES (38,19,22,3861); --Atlantic Florida to Long Beach California INSERT INTO DISTANCES VALUES (39,20,21,4348); INSERT INTO DISTANCES VALUES (40,21,20,4348);
After removing the previously applied filters, navigate to the graph viewer, and select the algorithms tab. Update the Algorithm field to “Shortest Path”, specify the values shown below, and click Apply.
Log in to complete tutorial - Step 4
SAP HANA provides the ability to store and query JSON data. This can be useful if the schema of the data is often changed or if you wish to join data in SQL queries that comes from both SQL tables and JSON data.
The following steps will demonstrate how to create a JSON collection that can be used to collect notes about customers staying at a hotel.
The creation of a JSON collection is not supported in the SAP HANA Cloud free tier or trial.
-
Enable the JSON document store.
For an SAP HANA Cloud database, in the creation wizard, or for an existing instance, the Manage Configuration dialog, enable the document store.
For an on-premise server, add the document service as described at Enable the SAP HANA JSON Document Store.
-
Create a collection named
GUEST_NOTES
.SQLCopyCREATE COLLECTION GUEST_NOTES;
-
Insert some data.
SQLCopyINSERT INTO GUEST_NOTES VALUES ('{"FIRST_NAME": "Jenny", "LAST_NAME": "Porter", "REQUEST": "requested a courtesy call at 7:00 am"}'); INSERT INTO GUEST_NOTES VALUES ('{"FIRST_NAME": "Jenny", "LAST_NAME": "Porter", "REQUEST": "requested an extra blanket"}'); INSERT INTO GUEST_NOTES VALUES ('{"title": "Mr.", "FIRST_NAME": "Peter", "LAST_NAME": "Brown", "REQUEST": "requested an earl grey at 2:00"}');
Notice that the structure of the Guest Notes does not need to be defined in advance.
JSON collections can also be populated using the import data wizard.
-
The JSON data can be returned as a JSON document, in a tabular result, or can be joined with data from a table.
SQLCopySELECT * FROM GUEST_NOTES; --returns JSON SELECT FIRST_NAME, LAST_NAME, REQUEST FROM GUEST_NOTES; --returns tabular result WITH myJSON AS (SELECT GUEST_NOTES FROM GUEST_NOTES) SELECT '[' || STRING_AGG(TO_NVARCHAR(GUEST_NOTES), ',') || ']' FROM myJSON; --returns all the results as one JSON document WITH GN_VIEW AS (SELECT FIRST_NAME, LAST_NAME, REQUEST FROM GUEST_NOTES) --joins a collection with a table SELECT DISTINCT GN_VIEW.REQUEST, C.FIRSTNAME, GN_VIEW.LAST_NAME, C.ADDRESS FROM GN_VIEW INNER JOIN CUSTOMER AS C ON GN_VIEW.LAST_NAME = C.NAME;
For additional details see the SELECT Statement in the JSON Document Store guide.
-
The properties of the JSON collection can also be viewed.
Click on the glasses icon to view the JSON text.
Log in to complete tutorial -
- Step 5
This step will import an
ESRI shapefile
or optionally aGeoJSON
file containing points of interest in the city of Waterloo Ontario. TheESRI shapefile
import will result in a table while the JSON import will result in a JSON Collection. In the following step, a search will be performed to return the closest points of interest to the Delta hotel located in Waterloo.-
At the ARCGIS Hub, search for
Points of Interest in Waterloo
. Scroll through the results and choose the selection below. -
Choose to download the data as a
shapefile
. The final sub-steps provide some details on how aGeoJSON
file can be imported. -
To import a
shapefile
, start the import data wizard.Choose Import ESRI Shapefiles and select the
Points_of_Interest.zip
file.If importing an
ESRI Shapefile
from a cloud storage provider, the file must be unzipped, and the object name would befolder_name/shapefle_name_minus_the_shp_extension
. -
Choose to import the
ESRI shapefile
into the schema HOTEL.Within the downloaded
ESRI shapefile
, there is a file namedPoints_of_Interest.prj
. This file mentions the spatial reference system used by thisESRI shapefile
. Specify WGS 84 as the spatial reference system.By default, the database server adds the following spatial reference systems to a new database. Additionally, the
ST_SPATIAL_REFERENCE_SYSTEMS
System View can be queried for available spatial reference systems. -
Rename the imported table as it was created using mixed case.
SQLCopyRENAME TABLE "Points_of_Interest" TO POI_WATERLOO;
-
View the table. Notice that the points of interest locations are stored in a column of type
ST_GEOMETRY
. -
Select Open Data to view the raw data, and select View Data on the SHAPE column of a point of interest.
The selected location is shown on a map.
The Leaflet map is not shown in on-premise installs of the SAP HANA database explorer.
-
Perform the below query.
SQLCopySELECT FACILITY, SHAPE.ST_AsWKT(), SHAPE FROM POI_WATERLOO;
Notice that the location data can be formatted in a more readable format using the methods
ST_AsWKT
orST_AsEWT
which in addition shows the SRID.Additional details on spatial reference systems can be found at SAP HANA Spatial Reference for SAP HANA Cloud.
-
Optionally follow the rest of the sub-steps to import a
GeoJSON
file as a JSON Collection.After downloading the file, rename the file to have a
.json
file extension, instead of.geojson
. -
Open the file in n Microsoft VS Code.
Remove the extra metadata at the beginning and end so that the file only contains a JSON document on each line.
Remove the commas at the end of each line. Press Ctrl F and use a regular expression search and replace to search for
\},\n
and replace it with}\n
.To see the required formatting for importing JSON documents, see SAP HANA Database JSON Document Store Guide.
-
Upload the file to a cloud storage provider.
See Export and Import Data and Schema with SAP HANA Database Explorer for more information.
-
Start the import data wizard.
Choose Import Data and provide the credentials to the storage bucket containing the JSON file.
-
Choose to import the JSON file into the schema HOTEL and name the collection
POI_WATERLOO_JSON
.Alternatively, a text-based import of the JSON file may be used:
SQLCopyCREATE COLLECTION POI_WATERLOO_JSON; IMPORT FROM JSON FILE 's3-us-east-1://dansawsbucket1/Points_of_Interest.json' INTO POI_WATERLOO_JSON WITH FAIL ON INVALID DATA CREDENTIAL 'AWS';
After the import completes and the catalog is refreshed, a new JSON collection will appear.
Click on the
glasses
icon to open the JSON document viewer. -
The following SQL queries show a few examples of querying the imported JSON data including the last query which can be shown on the map viewer.
SQLCopySELECT * FROM POI_WATERLOO_JSON; --JSON data format. Can use the built-in viewer. SELECT "properties".FACILITY as name, "properties".TYPE as type, "geometry" as geometry FROM POI_WATERLOO_JSON; --Can return data in column format. SELECT "properties".FACILITY as name, "properties".TYPE as type, JSON_VALUE("geometry", '$.coordinates[0]') as long, JSON_VALUE("geometry", '$.coordinates[1]') as lat FROM POI_WATERLOO_JSON; --Can return longitude and latitude SELECT "properties".FACILITY as name, "properties".TYPE as type, ST_GeomFromGeoJSON("geometry", 4326) as shape FROM POI_WATERLOO_JSON; --Can now be shown in the map viewer
Additional details can be found at JSON_VALUE Function and ST_GeomFromGeoJSON.
Log in to complete tutorial -
- Step 6
- The following statement shows the list of points of interest within 3 kilometers of the
Delta
hotel.SQLCopySELECT P.FACILITY, P.TYPE, P.ADDRESS, ROUND(H.LOCATION.ST_Distance(P.SHAPE, 'kilometer'), 2) as DISTANCE, P.SHAPE FROM HOTEL H, POI_WATERLOO P WHERE H.HNO=26 /*Delta*/ AND TO_BOOLEAN(H.LOCATION.ST_WithinDistance(NEW ST_Point(SHAPE.ST_AsWKT(), 4326), 3, 'kilometer')) = TRUE ORDER BY DISTANCE ASC;
Each point can be viewed as shown below.
Alternatively, all the points can be shown together.
SQLCopySELECT ST_UnionAggr(P.SHAPE) FROM HOTEL H, POI_WATERLOO P WHERE H.HNO=26 /*Delta*/ AND TO_BOOLEAN(H.LOCATION.ST_WithinDistance(NEW ST_Point(SHAPE.ST_AsWKT(), 4326), 3, 'kilometer')) = TRUE;
The map may not display if the byte limit is exceeded. The byte limit can be changed in the SQL console settings.
For additional details, see ST_Point Type, ST_Distance, ST_WithinDistance, and ST_UnionAggr Method.
To view all access methods for spatial data, see SAP HANA Spatial Reference for SAP HANA Cloud - Accessing and Manipulating Spatial Data.
The latitude and longitude for a location in Google Maps can be obtained for a given address via the marker’s context menu.
It should also be noted that when a used in a ST_POINT, x is longitude and y is latitude so the above point would be represented as
NEW ST_Point('POINT (-94.71832 32.50459)', 4326)
.
Log in to complete tutorial - The following statement shows the list of points of interest within 3 kilometers of the
- Step 7
Congratulations! You have explored a few of the multi-model features in SAP HANA and are now familiar with graph workspaces, JSON collections, and spatial data when using the SAP HANA database explorer.