RedisGraph-PHP provides PHP Client for Redislabs' RedisGraph Module. This library supports both widely used redis clients (PECL Redis Extension and Predis).
Read this announcement details before decide to use this library: RedisGraph End-of-Life Announcement
"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."
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
}
The recommended method to install RedisGraph-PHP is with composer.
composer require mkorkmaz/redislabs-redisgraph-php
You need PECL Redis Extension or Predis to use RedisGraph-PHP.
<?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);
<?php
declare(strict_types=1);
use Predis;
use Redislabs\Module\RedisGraph\RedisGraph;
$redisClient = new Predis\Client();
$redisGraph = RedisGraph::createWithPredis($redisClient);
<?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);
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 |
------------------------------------------------------
*/
This library does not provide Query Builder, but you can use mper/cypher-querybuilder
composer require mper/cypher-querybuilder
<?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"
You can use Docker Image provided by Redislabs.
docker run -p 6379:6379 --name redis-redisgraph redislabs/redisgraph:latest