Tinkerpop 3 has just been released with a whole new set of features and new gremlin language. These changes make working with graph databases a breeze in PHP. Let’s see how.
There are multiple advantages to working with the TP3 stack. The most important one is simply that you can switch backend databases at will.
If you don’t know whether to go with Neo4j or Titan, don’t worry you can simply start with one and switch to the other if needed.
The available backend databases are :
- Elastic Search
- OrientDB is coming
How it works – Installing the server
You simply need to install gremlin-server. Bellow is how to do it in linux. Just replace <INSTALLATION_DIR> with the directory you want to install into.
wget -O <INSTALLATION_DIR> https://www.apache.org/dist/incubator/tinkerpop/3.0.1-incubating/apache-gremlin-server-3.0.1-incubating-bin.zip unzip apache-gremlin-server-3.0.1-incubating-bin.zip
The default configuration should be good enough to get you up and running. To run the server in a terminal simply do :
cd <INSTALLATION_DIR>/apache-gremlin-server-3.0.1-incubating bin/gremlin-server.sh conf/gremlin-server-modern.yaml
Using the PHP driver
The simplest way is to go to your project folder and run the following command:
php composer.phar require brightzone/gremlin-php "*"
Or add this to your composer.json file:
Once this is done you can easily connect to the server and run queries in PHP. For example :
require_once('vendor/autoload.php'); // depending on your project this may not be necessary use \brightzone\rexpro\Connection; $db = new Connection([ 'host' => 'localhost', 'graph' => 'graph' ]); $db->open(); $result = $db->send('5+5'); //result =  //do something with result $db->close();
Working with graphs
If you followed all the instructions in this post. Your graph database should load with the modern graph. I’ve included this graph bellow for reference and to use in examples:
So given this graph. Lets return the names of all the people:
use \brightzone\rexpro\Connection; $db = new Connection([ 'host' => 'localhost', 'graph' => 'graph' ]); $db->open(); $query = 'g.V().has(label, "person").values("name")'; $result = $db->send($query); print_r($result); $db->close();
This will return the following array:
Array (  => marko  => vadas  => josh  => peter )
If you wanted to get the full set of entries with all properties you could have used g.V().has(label, “person”) to get the following response.
Array (  => Array ( [id] => 1 [label] => person [type] => vertex [properties] => Array ( [name] => Array (  => Array ( [id] => 0 [value] => marko ) ) [age] => Array (  => Array ( [id] => 2 [value] => 29 ) ) ) ... 3 more entries ... )
This response is a little verbose but it contains all the information you need.
Lets run a second query. This time we would like to find all the software the person josh has created and return all properties:
use \brightzone\rexpro\Connection; $db = new Connection([ 'host' => 'localhost', 'graph' => 'graph' ]); $db->open(); $query = 'g.V().has(label, "person") .has("name", "josh") .out("created") .valueMap()'; $result = $db->send($query); print_r($result); $db->close();
The result of this query is:
Array (  => Array ( [name] => Array (  => ripple ) [lang] => Array (  => java ) )  => Array ( [name] => Array (  => lop ) [lang] => Array (  => java ) ) )
For more information on how to write gremlin queries you can check the tinkerpop graph traversal documentation.
You’ll be up and running in 5mn and a whole world of graph database manipulation will be at the tip of your hands.
There are many tips and tricks on for example how to install and use Neo4J and cypher via TP3. I’ll make further posts for this specific topic.
Have fun guys.