NoSQL Zone is brought to you in partnership with:

I am a software developer who specializes in web applications. My languages of choice are PHP and Javascript, but I dabble in a little bit of everything. I am the author of the Neo4jPHP binding for the Neo4j graph database. Every part of the software development process fascinates me. Josh is a DZone MVB and is not an employee of DZone and has posted 14 posts at DZone. You can read more from them at their website. View Full User Profile

Development Setup for Neo4j and PHP: Part 1

07.27.2012
| 6669 views |
  • submit to reddit

I would really love to see more of my fellow PHP developers talking about, playing with, and building awesome applications on top of graph databases. They really are a powerful storage solution that fits well into a wide variety of domains.

In this two part series, I'll detail how to set up a development environment for building a project with a graph database (specifically Neo4j). Part 1 will show how to set up the development and unit testing databases. In Part 2, we'll create a basic application that talks to the database, including unit tests.

All the steps below were performed on Ubuntu 10.10 Maverick, but should be easy to translate to any other OS.

Grab the Components

There are a few libraries and software tools needed for the project. First off, we'll need to install our programming environment, specifically PHP, PHPUnit and Java. How to do this is dependent on your operating system. My setup is PHP 5.3, PHPUnit 3.6 and Sun Java 6.

Next, we need to get Neo4j. Download the latest tarball from http://neo4j.org/download. I usually go with the latest milestone release (1.5.M02 at this time).

> cd ~/Downloads
> wget http://dist.neo4j.org/neo4j-community-1.5.M02-unix.tar.gz

It's good practice to reset your unit testing databse before each test run. In SQL, it's easy to drop all the tables, or delete all the data. This capability is not built in to Neo4j, so we'll need to get a custom server plugin that does this:

> wget http://github.com/downloads/jexp/neo4j-clean-remote-db-addon/test-delete-db-extension-1.4.jar

Finally, we'll need a Neo4j PHP client library. I recommend Neo4jPHP (disclaimer: I'm the author) and all the code samples in Part 2 use it. Feel free to find another one, or roll your own.

>wget http://github.com/downloads/jadell/Neo4jPHP/neo4jphp.phar

 

Set Up the Development Instance

It is possible to put test and development data in the same database instance, but I prefer a two database solution, even when using a SQL database. I do this because 1) I don't have to worry about accidentally blowing away any development data I care about, and 2) I don't have to worry about queries and traversals in my unit tests accidentally pulling in development data, and vice versa.

Unlike SQL or many NOSQL database servers, Neo4j cannot have more than one database in a single instance. The only way to get two databases is to run two separate instances on two separate ports or hosts. The instructions below describe how to set up our development and unit testing databases on the same host but listening on different ports.

First, create a point to hold both neo4j instances, and unpack the development instance:
> mkdir -p ~/neo4j
> cd ~/neo4j
> tar -xvzf ~/Downloads/neo4j-community-1.5.M02-unix.tar.gz
> mv neo4j-community-1.5.M02 dev

Next, configure the server by editing the file "~/neo4j/dev/conf/neo4j-server.properties". Make sure that the server is on port 7474

org.neo4j.server.webserver.port=7474

If you will be accessing the database instance from a host other than localhost, uncomment the following line:

org.neo4j.server.webserver.address=0.0.0.0

Save the config and exit. Now it's time to start the instance.

> ~/neo4j/dev/bin/neo4j start

You should be able to browse to the Neo4j Webadmin panel: http://localhost:7474/webadmin/#

Set Up the Testing Instance

 

Unpack the test instance:

> cd ~/neo4j
> tar -xvzf ~/Downloads/neo4j-community-1.5.M02-unix.tar.gz
> mv neo4j-community-1.5.M02 test

Next, configure the server by editing the file "~/neo4j/test/conf/neo4j-server.properties". Make sure that the server is on port 7475 (note that this is a different port from the development instance!)

org.neo4j.server.webserver.port=7475

If you will be accessing the database instance from a host other than localhost, uncomment the following line:

org.neo4j.server.webserver.address=0.0.0.0

Add the following lines in the JAXRS section at the bottom of the file:

org.neo4j.server.thirdparty_jaxrs_classes=org.neo4j.server.extension.test.delete=/db/data/cleandb
org.neo4j.server.thirdparty.delete.key=secret-key

You can change "secret-key" to be whatever you wish. Save the config and exit.

We need one more change to differentiate the testing from the development instance. Edit the file "~/neo4j/test/conf/neo4j-wrapper.properties" and change the instance name line:

wrapper.name=neo4j-test

Save and exit. Now we have to install the plugin that will allow us to reset the database via the REST client.

> cp ~/Downloads/test-delete-db-extension-1.4.jar ~/neo4j/test/plugins

Start the instance.

> ~/neo4j/test/bin/neo4j start

You should be able to browse to the Neo4j Webadmin panel: http://localhost:7475/webadmin/#

Test the remote clean extension by switching to the Webadmin "Console" tab, clicking "HTTP" and entering the following:

DELETE /db/data/cleandb/secret-key

The "secret-key" should be whatever you entered in the server config file. If everything works, you should see a "200 OK" response.

Congratulations! Everything is set up and ready for us to build our application. In Part 2 of this series, I'll talk about setting up unit tests and building a basic application using a graph database as a backend. 

 

 

 

Published at DZone with permission of Josh Adell, author and DZone MVB. (source)

(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)