mkorkmaz / redislabs-redisgraph-php
PHP Client for Redislabs Redis Graph Module.
Installs: 2 807
Dependents: 0
Suggesters: 0
Security: 0
Stars: 16
Watchers: 6
Forks: 1
Open Issues: 2
Requires
- php: ^8.0
- mkorkmaz/redislabs-common: ^1.1
- sevenecks/tableify: ^0.0.5
Requires (Dev)
- ext-redis: *
- codeception/codeception: ^5.0.5
- codeception/module-asserts: ^3.0.0
- damianopetrungaro/php-commitizen: ^0.2.0
- malukenho/mcbumpface: ^1.1.5
- php-coveralls/php-coveralls: ^v2.5.3
- predis/predis: ^v2.0.3
- roave/security-advisories: dev-master
- squizlabs/php_codesniffer: ^3.7.1
- vimeo/psalm: ^5.1.0
Suggests
- ext-redis: If your application depends of redis PCEL extention.
- mper/cypher-querybuilder: A php query builder for cypher queries.
- predis/predis: If your application depends on predis.
README
RedisGraph-PHP provides PHP Client for Redislabs' RedisGraph Module. This library supports both widely used redis clients (PECL Redis Extension and Predis).
Important
Read this announcement details before decide to use this library: RedisGraph End-of-Life Announcement
About RedisGraph
"RedisGraph is the first queryable Property Graph database to use sparse matrices to represent the adjacency matrix in graphs and linear algebra to query the graph."
RedisGraph-PHP Interface
You can run any RedisGraph Query command using these functions.
<?php use Redislabs\Module\RedisGraph\Interfaces\QueryInterface; use Redislabs\Module\RedisGraph\Result; interface RedisGraph { public function rawQuery(QueryInterface $query) : array public function query(QueryInterface $query) : Result public function delete(string $name) : string; public function explain(QueryInterface $query) : string; public function commit(QueryInterface $query) : Result }
Installation
The recommended method to install RedisGraph-PHP is with composer.
composer require mkorkmaz/redislabs-redisgraph-php
Usage
You need PECL Redis Extension or Predis to use RedisGraph-PHP.
Creating RedisGraph Client
Example for PECL Redis Extension
<?php declare(strict_types=1); use Redis; use Redislabs\Module\RedisGraph\RedisGraph; $redisClient = new Redis(); $redisClient->connect('127.0.0.1'); $redisGraph = RedisGraph::createWithPhpRedis($redisClient);
Example for Predis
<?php declare(strict_types=1); use Predis; use Redislabs\Module\RedisGraph\RedisGraph; $redisClient = new Predis\Client(); $redisGraph = RedisGraph::createWithPredis($redisClient);
Constructing a Graph.
<?php use Redislabs\Module\RedisGraph\Node; use Redislabs\Module\RedisGraph\Edge; use Redislabs\Module\RedisGraph\GraphConstructor; $labelSource = 'person'; $labelDestination = 'country'; $propertiesSource = ['name' => 'John Doe', 'age' => 33, 'gender' => 'male', 'status' => 'single']; $propertiesDestination = ['name' => 'Japan']; $edgeProperties = ['purpose' => 'pleasure', 'duration' => 'two weeks']; $person = Node::createWithLabel($labelSource) ->withProperties($propertiesSource) ->withAlias('CatOwner'); $country = Node::createWithLabelAndProperties($labelDestination, $propertiesDestination) ->withAlias('CatCountry'); $edge = Edge::create($person, 'visited', $country) ->withProperties($edgeProperties); $graph = new GraphConstructor('TRAVELLERS'); $graph->addNode($person); $graph->addNode($country); $graph->addEdge($edge); $commitQuery = $graph->getCommitQuery(); $result = $redisGraph->commit($commitQuery); var_dump($result->getLabelsAdded()); // int(2) var_dump($result->getNodesCreated()); // int(2) var_dump($result->getLabelsAdded()); // int(2) var_dump($result->getNodesDeleted()); // int(0) var_dump($result->getRelationshipsCreated()); // int(1) var_dump($result->getRelationshipsDeleted()); // int(0) var_dump($result->getPropertiesSet()); // int(7) var_dump($result->getExecutionTime()); // float(0.9785) $propertiesSource = ['name' => 'Jane Doe', 'age' => 30, 'gender' => 'female', 'status' => 'single']; $propertiesDestination = ['name' => 'Japan']; $edgeProperties = ['purpose' => 'pleasure', 'duration' => 'one weeks']; $person2 = Node::createWithLabel($labelSource)->withProperties($propertiesSource); $country2 = Node::createWithLabelAndProperties($labelDestination, $propertiesDestination); $edge2 = Edge::merge($person2, 'visited', $country2)->withProperties($edgeProperties); $propertiesSource = ['name' => 'Kedibey', 'age' => 13, 'gender' => 'male', 'status' => 'single']; $propertiesDestination = ['name' => 'Turkey']; $edgeProperties = ['purpose' => 'living', 'duration' => 'whole life']; $person3 = Node::createWithLabel($labelSource)->withProperties($propertiesSource); $country3 = Node::createWithLabelAndProperties($labelDestination, $propertiesDestination); $edge3 = Edge::merge($person3, 'visited', $country3)->withProperties($edgeProperties); $graph = new GraphConstructor('TRAVELLERS'); $graph->addNode($person2); $graph->addNode($country2); $graph->addEdge($edge2); $graph->addNode($person3); $graph->addNode($country3); $graph->addEdge($edge3); $commitQuery = $graph->getCommitQueryWithMerge(); $this->redisGraph->commit($commitQuery);
Querying a Graph.
use Redislabs\Module\RedisGraph\Query; $matchQueryString = 'MATCH (p:person)-[v:visited {purpose:"pleasure"}]->(c:country) RETURN p.name, p.age, v.purpose, v.duration, c.name'; $matchQuery = new Query('TRAVELLERS', $matchQueryString); $result = $redisGraph->query($matchQuery); $labels = $result->getLabels(); $resultSet = $result->getResultSet(); var_dump($labels); // Dumps column labels var_dump($resultSet[0]); // Dumps first result ... $result->prettyPrint(); /* Prints ------------------------------------------------------ | p.name | p.age | v.purpose | v.duration | c.name | ------------------------------------------------------ | John Doe | 33 | pleasure | two weeks | Japan | | Jane Doe | 30 | pleasure | one weeks | Japan | ------------------------------------------------------ */
Cypher Query Builder
This library does not provide Query Builder, but you can use mper/cypher-querybuilder
1. Install the library.
composer require mper/cypher-querybuilder
2. Then build your query
<?php use MP\Cypher\QueryBuilder; $queryBuilder = new QueryBuilder(); $queryBuilder->addMatch() ->addNode('p', 'person') ->relation('v', 'visited', ['purpose' => 'pleasure'])->right() ->node('c', 'country'); $matchQueryString = $queryBuilder->getQuery('p.name', 'p.age', 'v.purpose', 'c.name'); echo $matchQueryString; // Prints "MATCH (p:person)-[v:visited {purpose:'pleasure'}]->(c:country) RETURN p.name,p.age,v.purpose,c.name"
Test and Development
You can use Docker Image provided by Redislabs.
docker run -p 6379:6379 --name redis-redisgraph redislabs/redisgraph:latest