|
| 1 | +# RedisGraph-PHP: Redislabs Redis Graph PHP Client |
| 2 | + |
| 3 | +RedisGraph-PHP provides PHP Client for Redislabs' RedisGraph Module. This library supports both widely used redis clients ([PECL Redis Extension](https://github.com/phpredis/phpredis/#readme) and [Predis](https://github.com/nrk/predis)). |
| 4 | + |
| 5 | + |
| 6 | +[](https://travis-ci.org/mkorkmaz/redislabs-redisgraph-php) [](https://coveralls.io/github/mkorkmaz/redislabs-redisgraph-php?branch=master) [](https://scrutinizer-ci.com/g/mkorkmaz/redislabs-redisgraph-php/?branch=master) [](https://packagist.org/packages/mkorkmaz/redislabs-redisgraph-php) [](https://packagist.org/packages/mkorkmaz/redislabs-redisgraph-php) [](https://packagist.org/packages/mkorkmaz/redislabs-redisgraph-php) [](https://packagist.org/packages/mkorkmaz/redislabs-redisgraph-php) |
| 7 | + |
| 8 | + |
| 9 | +## About RedisGraph |
| 10 | + |
| 11 | +"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." |
| 12 | + |
| 13 | +[More info about Redis Graph](https://oss.redislabs.com/redisgraph/). |
| 14 | + |
| 15 | + |
| 16 | +## RedisGraph-PHP Interface |
| 17 | + |
| 18 | +You can run any Redis Query command using these functions. |
| 19 | + |
| 20 | +```php |
| 21 | +<?php |
| 22 | + |
| 23 | +use Redislabs\Module\RedisGraph\Interfaces\QueryInterface; |
| 24 | +use Redislabs\Module\RedisGraph\Result; |
| 25 | + |
| 26 | +interface RedisGraph |
| 27 | +{ |
| 28 | + public function rawQuery(QueryInterface $query) : array |
| 29 | + public function query(QueryInterface $query) : Result |
| 30 | + public function delete(string $name) : string; |
| 31 | + public function explain(QueryInterface $query) : string; |
| 32 | + public function commit(QueryInterface $query) : Result |
| 33 | +} |
| 34 | + |
| 35 | +``` |
| 36 | + |
| 37 | +## Installation |
| 38 | + |
| 39 | +The recommended method to installing RedisGraph-PHP is with composer. |
| 40 | + |
| 41 | +```bash |
| 42 | +composer require mkorkmaz/redislabs-redisgraph-php |
| 43 | +``` |
| 44 | + |
| 45 | +## Usage |
| 46 | + |
| 47 | +You need PECL Redis Extension or Predis to use RedisGraph-PHP. |
| 48 | + |
| 49 | +### Creating RedisGraph Client |
| 50 | + |
| 51 | +##### Example for PECL Redis Extension |
| 52 | + |
| 53 | +```php |
| 54 | +<?php |
| 55 | +declare(strict_types=1); |
| 56 | + |
| 57 | +use Redis; |
| 58 | +use Redislabs\Module\RedisGraph\RedisGraph; |
| 59 | + |
| 60 | +$redisClient = new Redis(); |
| 61 | +$redisClient->connect('127.0.0.1'); |
| 62 | +$redisGraph = RedisGraph::createWithPhpRedis($redisClient); |
| 63 | +``` |
| 64 | + |
| 65 | +##### Example for Predis |
| 66 | + |
| 67 | +```php |
| 68 | +<?php |
| 69 | +declare(strict_types=1); |
| 70 | + |
| 71 | +use Predis; |
| 72 | +use Redislabs\Module\RedisGraph\RedisGraph; |
| 73 | + |
| 74 | +$redisClient = new Predis\Client(); |
| 75 | +$redisGraph = RedisGraph::createWithPredis($redisClient); |
| 76 | +``` |
| 77 | + |
| 78 | +### Constructing a Graph. |
| 79 | + |
| 80 | +```php |
| 81 | +<?php |
| 82 | + |
| 83 | + |
| 84 | +use Redislabs\Module\RedisGraph\Node; |
| 85 | +use Redislabs\Module\RedisGraph\Edge; |
| 86 | +use Redislabs\Module\RedisGraph\GraphConstructor; |
| 87 | + |
| 88 | +$labelSource = 'person'; |
| 89 | +$labelDestination = 'country'; |
| 90 | + |
| 91 | +$propertiesSource = ['name' => 'John Doe', 'age' => 33, 'gender' => 'male', 'status' => 'single']; |
| 92 | +$propertiesDestination = ['name' => 'Japan']; |
| 93 | +$edgeProperties = ['purpose' => 'pleasure', 'duration' => 'two weeks']; |
| 94 | + |
| 95 | +$person = Node::createWithLabel($labelSource) |
| 96 | + ->withProperties($propertiesSource) |
| 97 | + ->withAlias('CatOwner'); |
| 98 | +$country = Node::createWithLabelAndProperties($labelDestination, $propertiesDestination) |
| 99 | + ->withAlias('CatCountry'); |
| 100 | + |
| 101 | +$edge = Edge::create($person, 'visited', $country) |
| 102 | + ->withProperties($edgeProperties); |
| 103 | + |
| 104 | +$graph = new GraphConstructor('TRAVELLERS'); |
| 105 | +$graph->addNode($person); |
| 106 | +$graph->addNode($country); |
| 107 | +$graph->addEdge($edge); |
| 108 | +$commitQuery = $graph->getCommitQuery(); |
| 109 | + |
| 110 | +$result = $redisGraph->commit($commitQuery); |
| 111 | + |
| 112 | +var_dump(result->getLabelsAdded()); // int(2) |
| 113 | +var_dump($result->getNodesCreated()); // int(2) |
| 114 | +var_dump(result->getLabelsAdded()); // int(2) |
| 115 | +var_dump($result->getNodesDeleted()); // int(0) |
| 116 | +var_dump($result->getRelationshipsCreated()); // int(1) |
| 117 | +var_dump($result->getRelationshipsDeleted()); // int(0) |
| 118 | +var_dump($result->getPropertiesSet()); // int(7) |
| 119 | +var_dump($result->getExecutionTime()); // float(0.9785) |
| 120 | + |
| 121 | +``` |
| 122 | + |
| 123 | +### Querying a Graph. |
| 124 | + |
| 125 | +```php |
| 126 | +use Redislabs\Module\RedisGraph\Query; |
| 127 | + |
| 128 | +$matchQueryString = 'MATCH (p:person)-[v:visited {purpose:"pleasure"}]->(c:country) |
| 129 | + RETURN p.name, p.age, v.purpose, c.name'; |
| 130 | +$matchQuery = new Query('TRAVELLERS', $matchQueryString); |
| 131 | + |
| 132 | +$result = $this->redisGraph->query($matchQuery); |
| 133 | +$resultSet = $result->getResultSet(); |
| 134 | + |
| 135 | +var_dump($resultSet[0]) // Dumps column labels |
| 136 | +var_dump($resultSet[1]) // Dumps first result |
| 137 | +... |
| 138 | + |
| 139 | +$result->prettyPrint(); |
| 140 | + |
| 141 | +/* Prints |
| 142 | + |
| 143 | +----------------------------------------- |
| 144 | +| p.name | p.age | v.purpose | c.name | |
| 145 | +----------------------------------------- |
| 146 | +| John Doe | 33 | pleasure | Japan | |
| 147 | +----------------------------------------- |
| 148 | + |
| 149 | +*/ |
| 150 | + |
| 151 | +``` |
| 152 | + |
| 153 | + |
| 154 | +## Test and Development |
| 155 | + |
| 156 | +You can use Docker Image provided by Redislabs. |
| 157 | + |
| 158 | +```bash |
| 159 | +docker run -p 6379:6379 --name redis-redisgraph redislabs/redisgraph:latest |
| 160 | +``` |
0 commit comments