Component for handling RedisGraph – a fast graph database powered by linear algebra and matrix multiplication based on Redis.
According to RedisGraph official documentation its main features are:
- Property graph model based on openCypher query language
- Cypher queries translate into linear algebra expressions
- Nodes can have any number of labels
- Relationships have a relationship type
- Graphs represented as sparse adjacency matrices
- TPBRedisGraph.GRAPH_QUERY
- TPBRedisGraph.GRAPH_LIST
- TPBRedisGraph.GRAPH_PROFILE
- …
Additional commands: CloneGraph, DumpGraph, RestoreGraph
Simplified Cypher commands for NODEs and EDGEs, like CREATE, MATCH and DELETE, will make sure you will get the data in the proper format:
- GRAPH_NODE_CREATE(‘label’, ‘{“key_str”: “value_str”, “key_int”: value_int, …, “key_n”: “value_n”}’) – based on native GRAPH.QUERY CREATE this function creates a new ‘label’ node with corresponding ‘{“key”:”value”}’ pairs.
- GRAPH_NODE_MATCH(‘label’, ‘{“key_1”: “value_1”, …, “key_n”: “value_n”}’, [‘key_1’, …, ‘key_n’]) – based on native GRAPH.QUERY MATCH this function searches for all the ‘label’ nodes with matching ‘{“key”:”value”}’ pairs, and returns values of selected [‘keys’].
- GRAPH_EDGE_CREATE(source_int, destination_int, ‘label’, ‘{“key_str”: “value_str”, “key_int”: value_int, …, “key_n”:”value_n”}’) – based on native GRAPH.QUERY CREATE this function creates a new ‘label’ edge between source and destination nodes with corresponding ‘{“key”:”value”}’ pairs.
- GRAPH_EDGE_MATCH(‘label’, ‘{“key_1”: “value_1”, …, “key_n”: “value_n”}’, [‘key_1’, …, ‘key_n’]) – based on native GRAPH.QUERY MATCH this function searches for all the ‘label’ edges with matching ‘{“key”:”value”}’ pairs, and returns values of selected [‘keys’].
- res2dic(JsonString) – converts RedisGraph JSON string to the usual JSON string. This is necessary because JSON returned by RedisGraph has unusual formatting.
Prerequirements
- In order for this component to work you have to obtain, install and run the respective Redis software and its licenses. We do not provide any Redis software.
- PBClient for RedisGraph requires PBClient for Redis as the core component. It should be installed to provide a connection with the Redis server.
OS compatibility
7 SP2 x64 |
8 x64 |
8.1 x64 |
10 x64 |
11 x64 |
RAD Studio compatibility
10 Seattle |
10.1 Berlin |
10.2 Tokyo |
10.3 Rio |
10.4 Sydney |
11 Alexandria |
1 user
3 users
5 users
10 users
Site license
Free components in a bundle
Buy a bundle now and enjoy all new component releases for free during the update period. |
Keep track of multiple versions
You will get access to all older versions and a detailed changelog. |
Free new features
Get all new component features for free during the free update period. |
Install once use forever
Our products are not time limited. |
Source code promise
In case our component will become obsolete and we no longer support it, we will provide you its source code. So you don’t have to worry about a replacement. |
References:
> QSG code example provided with the installer
> RedisGraph native commands and documentation, Cypher query syntax
After installation, a group PyBridge will appear in the Tool Palette of Delphi, where you can find the new component TPBRedisGraph. PBClient for RedisGraph requires PBClient for Redis as the core component. It should be installed to provide a connection with the Redis server.
New component TPBRedisGraph can be found in the Tool Palette under PyBridge group
Place TPBRedisGraph onto the form and set up its main properties:
- AutoConnect – set to True for automatic connection to Redis
- RedisServer – host, password and port number to access Redis
The default Redis database ID that TPBRedisGraph is going to work with is 0, but you may specify a different database ID. To do so – create the OnConnect event handler procedure of the PBRedisClient object. Keep in mind that the trial version does not support the selection of databases.
1 2 3 4 |
procedure TForm1.PBRedisClient1Connect; begin PBRedisClient1.Select(0); // PBRedisClient. Selecting a database number in redis. end; |
Place TPBRedisGraph onto the form and set up its main properties:
- RedisClient – select the PBRedisClient object which should provide a connection with the Redis server
- GraphName – setup the default Graph name. You should set a new value of the parameter in your code every time when you intend to work with a different graph
Now PBClient for RedisGraph is ready to be used. PBClient for RedisGraph provides entire Redis GRAPH.QUERY syntax based on Cypher. In addition, PBClient for RedisGraph has special tools for the most used Cypher commands for nodes and edges, such as: CREATE, MATCH and DELETE. To demonstrate how PBClient for RedisGraph works, let’s create a simple application to manage the nodes and edges of the RedisGraph base. Add the corresponding Buttons, Edits and Memo to your form:
In ‘GRAPH_NODE_CREATE’ and ‘GRAPH_NODE_MATCH’ ButtonClick event handler procedures we are using following PBRedisGraph functions:
- GRAPH_NODE_CREATE(‘label’, ‘{“key_str”: “value_str”, “key_int”: value_int, …, “key_n”: “value_n”}’) – based on native GRAPH.QUERY CREATE this function creates a new ‘label’ node with corresponding ‘{“key”:”value”}’ pairs.
- GRAPH_NODE_MATCH(‘label’, ‘{“key_1”: “value_1”, …, “key_n”: “value_n”}’, [‘key_1’, …, ‘key_n’]) – based on native GRAPH.QUERY MATCH this function searches for all the ‘label’ nodes with matching ‘{“key”:”value”}’ pairs, and returns values of selected [‘keys’].
-
res2dic(JsonString) – converts RedisGraph JSON string to the usual JSON string. This is necessary because JSON returned by RedisGraph has unusual formatting.
When providing a JSON string as function parameter, you can use single quotes instead of double quotes:
‘{“key_str”: “value_str”, …}’ – double quotes
‘{”key_str”: ”value_str”, …}’ – escaped single quotes
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | procedure TForm2.GRAPH_NODE_CREATEClick(Sender: TObject); var Rslt, jres: string; begin Rslt:= PBRedisGraph1.GRAPH_NODE_CREATE('Person', '{"name": "Alice", "age": 23, "hair": "blonde"}'); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Rslt:= PBRedisGraph1.GRAPH_NODE_CREATE('Person', '{"name": "Alice", "age": 27, "hair": "brown"}'); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Rslt:= PBRedisGraph1.GRAPH_NODE_CREATE('Person', '{"name": "Kurt", "age": 27}'); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Rslt:= PBRedisGraph1.GRAPH_NODE_CREATE('Movie', '{"name": "Wall Street"}'); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); end procedure TForm2.GRAPH_NODE_MATCHClick(Sender: TObject); var Rslt, jres: string; begin Rslt:= PBRedisGraph1.GRAPH_NODE_MATCH('Person', '{"name": "Alice", "hair": "blonde"}', ['name', 'age']); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Rslt:= PBRedisGraph1.GRAPH_NODE_MATCH('Person', '{"name": "Alice", "age": 23}', ['name', 'hair']); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Memo1.Lines.Append('') end; |
To delete a node we should provide its ID to the GRAPH_NODE_DELETE function.
1 2 3 4 5 6 |
procedure TForm2.GRAPH_NODE_DELETEClick(Sender: TObject); begin if trim(EditDelNode.Text) = '' then exit; PBRedisGraph1.GRAPH_NODE_DELETE(StrToInt(EditDelNode.Text)) end; |
In ‘GRAPH_EDGE_CREATE’ and ‘GRAPH_EDGE_MATCH’ ButtonClick event handler procedures we are using following PBRedisGraph functions:
- GRAPH_EDGE_CREATE(source_int, destination_int, ‘label’, ‘{“key_str”: “value_str”, “key_int”: value_int, …, “key_n”:”value_n”}’) – based on native GRAPH.QUERY CREATE this function creates a new ‘label’ edge between source and destination nodes with corresponding ‘{“key”:”value”}’ pairs.
- GRAPH_EDGE_MATCH(‘label’, ‘{“key_1”: “value_1”, …, “key_n”: “value_n”}’, [‘key_1’, …, ‘key_n’]) – based on native GRAPH.QUERY MATCH this function searches for all the ‘label’ edges with matching ‘{“key”:”value”}’ pairs, and returns values of selected [‘keys’].
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 | procedure TForm2.GRAPH_NODE_CREATEClick(Sender: TObject); var Rslt, jres: string; begin Rslt:= PBRedisGraph1.GRAPH_NODE_CREATE('Person', '{"name": "Alice", "age": 23, "hair": "blonde"}'); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Rslt:= PBRedisGraph1.GRAPH_NODE_CREATE('Person', '{"name": "Alice", "age": 27, "hair": "brown"}'); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Rslt:= PBRedisGraph1.GRAPH_NODE_CREATE('Person', '{"name": "Kurt", "age": 27}'); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Rslt:= PBRedisGraph1.GRAPH_NODE_CREATE('Movie', '{"name": "Wall Street"}'); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); end procedure TForm2.GRAPH_NODE_MATCHClick(Sender: TObject); var Rslt, jres: string; begin Rslt:= PBRedisGraph1.GRAPH_NODE_MATCH('Person', '{"name": "Alice", "hair": "blonde"}', ['name', 'age']); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Rslt:= PBRedisGraph1.GRAPH_NODE_MATCH('Person', '{"name": "Alice", "age": 23}', ['name', 'hair']); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append(jres); Memo1.Lines.Append('') end; |
To delete an edge we should provide its ID to the GRAPH_EDGE_DELETE function.
1 2 3 4 5 6 |
procedure TForm2.GRAPH_EDGE_DELETEClick(Sender: TObject); begin if trim(EditDelEdge.Text) = '' then exit; PBRedisGraph1.GRAPH_EDGE_DELETE(StrToInt(EditDelEdge.Text)) end; |
The most powerful function of PBRedisGraph is GRAPH_QUERY. Based on native GRAPH.QUERY this function can execute any Cypher query that is supported by RedisGraph.
1 2 3 4 5 6 7 8 9 10 11 |
procedure TForm2.ButtonQueryClick(Sender: TObject); var Rslt, jres: string; begin if trim(EditQuery.Text) = '' then exit; Rslt := PBRedisGraph1.GRAPH_QUERY(trim(EditQuery.Text)); jres := PBRedisGraph1.res2dic(Rslt); Memo1.Lines.Append((jres.Replace('},{', '},' + #13#10 + '{').Replace('rslt":[', 'Result":[' + #13#10))); Memo1.Lines.Append('') end; |
Below are some sample commands for you to try. Feel free to experiment with your own requests.
1 2 3 4 5 6 7 8 9 |
MATCH (n) RETURN * MATCH (n)-[r]-() RETURN r MATCH (a:Person) WHERE a.name = 'Kurt' CREATE (a)-[:MEMBER]->(:Band {name:'Nirvana'}) CREATE (jim:Person{name:'Jim', age:29})-[:FRIENDS]->(pam:Person {name:'Pam', age:27})-[:WORKS]->(:Employer {name:'Dunder Mifflin'}) MATCH (:Person {name:'Jim'})-[r:FRIENDS]->() DELETE r MATCH (n) DELETE n MATCH (n:Person{name: 'Alice', hair: 'blonde'}) RETURN n.name, n.age MATCH (n:Person{name: 'Alice', age: 23}) RETURN n.name, n.hair MATCH ()-[r:FRIENDS]->() WHERE r.duration=3 RETURN r.duration |
This example demonstrates how easy it is to develop Delphi applications relying on RedisGraph with a help of PBClient for RedisGraph. Now you can also enjoy the full power of RedisGraph in your applications!