Your Next Neo4j App is Just a Few Lines of Code Away

DZone 's Guide to

Your Next Neo4j App is Just a Few Lines of Code Away

· Java Zone ·
Free Resource

The transactional http endpoint that was added to Neo4j 2.0 is really easy to use.

You can stream batches of cypher statements with their parameters to the server and receive the answers in a streaming fashion too.

The basic usage, of one transaction per batched request can be used with 3 lines of javascript.

var r=require("request")
function cypher(query,params,cb) {
        function(err,res) { cb(err,res.body)})

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)) }



It also supports transactions over the write. Meaning, you can across multiple http requests continue to read and write in your own isolated view of the world and then finally decide to keep your work (commit) or to throw it all away (rollback).

When you first post to /db/data/transaction the answer will contain the transaction-URL in the Location-Header. It also then provides a commit URL field in the JSON response and a transaction timeout date and time (by default 30s).

Here is a sample session:

POST /db/data/transaction
 {"statement": "CREATE (u:Person {login:{name}}) RETURN u",

Result: ==> 201 Created

{"commit": "http://localhost:7474/db/data/transaction/4/commit",
 "transaction":{"expires":"Wed, 18 Sep 2013 14:36:26 +0000"},

Read-Request: POST /db/data/transaction/4
 {"statements": [{"statement":"MATCH (u:User) RETURN u"}]}
Result: ==> 200 OK
   "transaction":{"expires":"Wed, 18 Sep 2013 14:39:05.."},

Commit-Request: POST /db/data/transaction/4/commit
Result: ==> 200 OK

Adding transactional support to the javascript snippet shown initially should be easy and is left as exercise for you :)

java ,nosql ,javascript ,tips and tricks ,neo4j

Published at DZone with permission of Michael Hunger , DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}