Chapter 7. Administering Neo4j

In the course of time that Neo4j has been around, it has become a widely accepted tool for storage and processing of large scale, highly interconnected data. From major corporations such as eBay and Walmart, to research students, the utilities of Neo4j have spread across the world. What makes Neo4j a great tool, is the fact that it is open source, and community contributions are useful and valuable. Today, Neo4j can be interfaced with several frameworks and a plethora of tools have been developed to make operations like data loading, traversals, and testing, simpler and more effective. It is not always about having an effective place for your application; it is also about maintaining and administering it to obtain optimum performance from the application. In this chapter, we will first look at how to use the Neo4j adapters for some of the most popular languages and frameworks that are used today. In the latter part of the chapter, we will see how admins can tweak and change the configurations of a standard Neo4j system, so that an optimum performance system can be obtained. The topics to be covered in this chapter can be summarized as follows:

  • Interfacing Neo4j with PHP, JavaScript, and Python
  • Configuring Neo4j Server, JVM, and caches
  • Memory mapped I/O settings
  • Logging configurations
  • Security of Neo4j Server

Interfacing with the tools and frameworks

Neo4j as a core data storage solution is quite powerful but, with the increasing sophistication of use and rapid development practices, several tools were developed as an effort to increase the efficiency and compatibility of a Neo4j system. Adapters for various well known languages for server-side and client-side development have sprung up, making the development process for Neo4j based web applications simpler. Let us take a look at the use of some of the Neo4j adapters for the most common web platform technologies.

Using Neo4j for PHP developers

PHP is one of the most widely popular web development languages for creating dynamic content based application. Some common frameworks such as Wordpress and CakePHP are developed with PHP. Now, if you are trying to develop a PHP application, and want Neo4j at the backend for data storage and processing, you can use the Neo4jPHP adapter very well. It is written by Josh Adell, and it exposes an API that encourages intuitiveness and flexibility of use. Neo4jPH also incorporates some advantageous performance enhancements of its own, in the form of lazy-loading and caching. Let us take a look at some basic usage of this tool.

Use an editor to create a script and name it connection_test.php. Add the following lines to it:

<?php
    require('vendor/autoload.php'),

    $client = new EverymanNeo4jClient('localhost', 7474);
    print_r($client->getServerInfo());

If you have a database instance running on a different machine or port, you need to accordingly change localhost to the complete address of the database machine, and 7474 to the appropriate port number.

You can now execute the script from a server instance or in a standalone manner to view the result:

> php connection_test.php

If your server information displays successfully, then you have set up a basic Neo4jPHP code. You can view the source of the project at https://github.com/jadell/neo4jphp along with the detailed documentation at https://github.com/jadell/neo4jphp/wiki/Introduction.

The JavaScript Neo4j adapter

JavaScript is one of the most widely used client-side scripting languages. You can now communicate with your client-side application along with the Neo4j server with the use of the request module of the Node.js framework. Any other JavaScript module which can interact with the Neo4j server by sending and receiving queries can also be used. You can get a detailed description of the formats and protocols of the Neo4j REST API endpoints from the official documentation at http://neo4j.com/docs/stable/rest-api-transactional.html. You can also perform operations like sending multiple statements in a request, as well as sharing a transaction between several requests. The following basic function can be used to access the remote REST endpoint with the help of JavaScript:

var req=require("request");
var traxUrl = "http://localhost:7474/db/data/transaction/commit";
function cypher(query,params,cb) {
  req.post({uri:traxUrl,
          json:{statements:[{statement:query,parameters:params}]}},
         function(err,res) { cb(err,res.body)})
}

The cypher() function can send a cypher request from the JavaScript code to the Neo4j server and receive a response in JSON format. To use this function in your application, you can use the following format:

var query="MATCH (n:User) RETURN n, labels(n) as l LIMIT {limit}"
var params={limit: 10}
var cb=function(err,data) { console.log(JSON.stringify(data)) }

cypher(query,params,cb)

{"results":[
  {"columns":["n","l"],
   "data":[
     {"row":[{"name":"Aran"},["User"]]}
    ]
  }],
 "errors":[]}

The JSON form of the results is displayed upon execution. For applications built on top of the Node.js framework, you can use the Node.js-specific driver for Neo4j as well. You can learn more about it at https://github.com/thingdom/node-neo4j.

Neo4j with Python

Neo4j provides extensive support for Python and its web framework, Django. Py2neo is a simple library written by Nigel Small, providing access to the Neo4j REST API, and even supports Cypher queries. It has no external dependencies and is actively maintained on Github. You can follow the project at https://github.com/nigelsmall/py2neo.

For the Django framework, neo4django is an Object Graph Mapper (OGM) with which you can create the model definitions in Django, along with queries for Neo4j. It is also community supported and is useful for graph web applications running Django at the backend. The project can be found at https://github.com/scholrly/neo4django.

There are several other tools and frameworks to make the task of interfacing Python applications with Neo4j painless. The most popular ones are the BulbFlow framework (http://bulbflow.com/) which is an ORM for graph traversals using Gremlin at the backend. NeoModel (https://github.com/robinedwards/neomodel) is another tool with support for Django. However, a detailed discussion of these frameworks is beyond the scope of this book.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset