Calling Procedures¶
Introduction¶
Procedures are called using the CALL clause. Most procedures return a stream of records with a fixed set of result fields, similar to how running a Cypher query returns a stream of records.
The YIELD sub-clause is used to explicitly select which of the available result fields are returned as newly-bound variables from the procedure call.
Thus, in order to be able to use YIELD for explicit columns, the names (and types) of the output parameters need be known in advance.
CALL gds.graph.list()
YIELD graphName as name, memoryUsage as mem
If you do not know the possible output fields :
CALL gds.graph.list()
YIELD *
Counting the number of labels in a graph :
CALL db.labels() YIELD label
RETURN count(label) AS numberofLabels
This script below calls the built-in procedure db.propertyKeys as part of counting the number of nodes per property key that is currently used in the database.
CALL db.propertyKeys() YIELD propertyKey AS prop
MATCH (n)
WHERE n[prop] IS NOT NULL
RETURN prop, count(n) AS numNodes
Signature of procedure¶
Imagine you want to find out more on a function to find the shortest path between the nodes in a graph. This can be achieved using the Dykstra algorithm. You know that this function is part of the gds library and is called shortestPath.dijkstra.stream.
With the function dbms.procedures() it is possible to understand the required input and output parameters:
CALL dbms.procedures() YIELD name, signature
WHERE name='gds.shortestPath.dijkstra.stream'
RETURN signature
which returns: “gds.shortestPath.dijkstra.stream(graphName :: ANY?, configuration = {} :: MAP?) :: (index :: INTEGER?, sourceNode :: INTEGER?, targetNode :: INTEGER?, totalCost :: FLOAT?, nodeIds :: LIST? OF INTEGER?, costs :: LIST? OF FLOAT?, path :: PATH?)”
Where (graphName :: ANY?, configuration = {} :: MAP?) discloses the fact that we need to provide the name of a graph. The configuration parameter is optional.
The output contains the following results:
index: 0-based index of the found path.
sourceNode: Source node of the path.
targetNode: Target node of the path.
totalCost: Total cost from source to target.
nodeIds: Node ids on the path in traversal order.
costs: Accumulated costs for each node on the path.
path: The path represented as Cypher entity.