Over a million developers have joined DZone.

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

· Java Zone

Microservices! They are everywhere, or at least, the term is. When should you use a microservice architecture? What factors should be considered when making that decision? Do the benefits outweigh the costs? Why is everyone so excited about them, anyway?  Brought to you in partnership with IBM.

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

Discover how the Watson team is further developing SDKs in Java, Node.js, Python, iOS, and Android to access these services and make programming easy. Brought to you in partnership with IBM.


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

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}