Neo4j Java REST Binding – Part 2 (Batching)

In Part 1, we talked about setting up a connection to the Neo4j Server using the Java REST Binding. Let’s now go into some details about transactions, batching, and what the REST requests really look like.Make sure you turn on logging (set system property org.neo4j.rest.logging_filter to true) as described in Part 1.

We will change our code to  execute these Neo4j API calls.

Example 1:

 
 

Transaction tx = graphDb.beginTx();
    Map props=new HashMap();
    props.put("id", 100);
    props.put("name","firstNode");
    Node node=graphDb.createNode(props);

    props.put("id",200);
    props.put("name","secondNode");
    Node node2=graphDb.createNode(props);

    node.createRelationshipTo(node2, DynamicRelationshipType.withName("KNOWS"));

    tx.success();
    tx.finish();
    
    result=engine.query("start n=node(*) return count(n) as total", Collections.EMPTY_MAP);
    Iterator iterator=result.iterator();
    if(iterator.hasNext()) {
        Map row= iterator.next();
        out.print("Total nodes: " + row.get("total"));
    }

Check the logs (for me, they appeared by default on the Tomcat console) and look for the REST calls. The code above produced:

INFO: 1 * Client out-bound request
1 >  POST http://localhost:7474/db/data/node
1 >  Accept: application/json; stream=true
1 >  X-Stream: true
1 >  Content-Type: application/json
1 >
{"id":100,"name":"firstNode"}

INFO: 1 * Client in-bound response
1 < 201
1 < Access-Control-Allow-Origin: *
1 < Transfer-Encoding: chunked
1 < Content-Encoding: UTF-8
1 < Location: http://localhost:7474/db/data/node/1
1 < Content-Type: application/json; stream=true
1 < Server: Jetty(6.1.25)
1 < 
{"extensions":{},"paged_traverse":"http://localhost:7474/db/data/node/1/paged/traverse/{returnType}{?pageSize,leaseTime}","outgoing_relationships":"http://localhost:7474/db/data/node/1/relationships/out","traverse":"http://localhost:7474/db/data/node/1/traverse/{returnType}","all_typed_relationships":"http://localhost:7474/db/data/node/1/relationships/all/{-list|&|types}","property":"http://localhost:7474/db/data/node/1/properties/{key}","all_relationships":"http://localhost:7474/db/data/node/1/relationships/all","self":"http://localhost:7474/db/data/node/1","properties":"http://localhost:7474/db/data/node/1/properties","outgoing_typed_relationships":"http://localhost:7474/db/data/node/1/relationships/out/{-list|&|types}","incoming_relationships":"http://localhost:7474/db/data/node/1/relationships/in","incoming_typed_relationships":"http://localhost:7474/db/data/node/1/relationships/in/{-list|&|types}","create_relationship":"http://localhost:7474/db/data/node/1/relationships","data":{"name":"firstNode","id":100}}

INFO: 2 * Client out-bound request
2 > POST http://localhost:7474/db/data/node
2 > Accept: application/json; stream=true
2 > X-Stream: true
2 > Content-Type: application/json
2 > 
{"id":200,"name":"secondNode"}

INFO: 2 * Client in-bound response
2 < 201
2 < Access-Control-Allow-Origin: *
2 < Transfer-Encoding: chunked
2 < Content-Encoding: UTF-8
2 < Location: http://localhost:7474/db/data/node/2
2 < Content-Type: application/json; stream=true
2 < Server: Jetty(6.1.25)
2 < 
{"extensions":{},"paged_traverse":"http://localhost:7474/db/data/node/2/paged/traverse/{returnType}{?pageSize,leaseTime}","outgoing_relationships":"http://localhost:7474/db/data/node/2/relationships/out","traverse":"http://localhost:7474/db/data/node/2/traverse/{returnType}","all_typed_relationships":"http://localhost:7474/db/data/node/2/relationships/all/{-list|&|types}","property":"http://localhost:7474/db/data/node/2/properties/{key}","all_relationships":"http://localhost:7474/db/data/node/2/relationships/all","self":"http://localhost:7474/db/data/node/2","properties":"http://localhost:7474/db/data/node/2/properties","outgoing_typed_relationships":"http://localhost:7474/db/data/node/2/relationships/out/{-list|&|types}","incoming_relationships":"http://localhost:7474/db/data/node/2/relationships/in","incoming_typed_relationships":"http://localhost:7474/db/data/node/2/relationships/in/{-list|&|types}","create_relationship":"http://localhost:7474/db/data/node/2/relationships","data":{"name":"secondNode","id":200}}

INFO: 3 * Client out-bound request
3 > POST http://localhost:7474/db/data/node/1/relationships
3 > Accept: application/json; stream=true
3 > X-Stream: true
3 > Content-Type: application/json
3 > 
{"to":"http://localhost:7474/db/data/node/2","type":"KNOWS"}

INFO: 3 * Client in-bound response
3 < 201
3 < Access-Control-Allow-Origin: *
3 < Transfer-Encoding: chunked
3 < Content-Encoding: UTF-8
3 < Location: http://localhost:7474/db/data/relationship/0
3 < Content-Type: application/json; stream=true
3 < Server: Jetty(6.1.25)
3 < 
{"extensions":{},"start":"http://localhost:7474/db/data/node/1","property":"http://localhost:7474/db/data/relationship/0/properties/{key}","self":"http://localhost:7474/db/data/relationship/0","properties":"http://localhost:7474/db/data/relationship/0/properties","type":"KNOWS","end":"http://localhost:7474/db/data/node/2","data":{}}

INFO: 4 * Client out-bound request
4 > POST http://localhost:7474/db/data/cypher
4 > Accept: application/json; stream=true
4 > X-Stream: true
4 > Content-Type: application/json
4 > 
{"query":"start n=node(*) return count(n) as total","params":{}}

INFO: 4 * Client in-bound response
4 < 200
4 < Access-Control-Allow-Origin: *
4 < Transfer-Encoding: chunked
4 < Content-Encoding: UTF-8
4 < Content-Type: application/json; stream=true
4 < Server: Jetty(6.1.25)
4 < 
{"columns":["total"],"data":[[3]]}

A total of 4 REST calls over the wire for that tiny piece of code. You definitely want to avoid this as far as possible. Option 1 is to use Cypher as far as possible. We could convert the first three REST calls into one by not using the embedded style API and switching to Cypher.

Example 2:

Map<String,Object> props=new HashMap<String, Object>();
 props.put("id", 100);
 props.put("name","firstNode");
     
 Map<String,Object> props2=new HashMap<String, Object>();
 props2.put("id",200);
 props2.put("name","secondNode");
    
  Map<String,Object> params=new HashMap<String, Object>();
  params.put("props1",props);
  params.put("props2",props2);
        
 engine.query("create (n1 {props1})-[:KNOWS]->(n2 {props2})", params);

This produces:

1 > POST http://localhost:7474/db/data/cypher
{"query":"create (n1 {props1})-[:KNOWS]->(n2 {props2})","params":{"props1":{"id":100,"name":"firstNode"},"props2":{"id":100,"name":"firstNode"}}}

Jul 24, 2013 10:38:47 PM com.sun.jersey.api.client.filter.LoggingFilter log
INFO: 1 * Client in-bound response
1 < 200
1 < Access-Control-Allow-Origin: *
1 < Transfer-Encoding: chunked
1 < Content-Encoding: UTF-8
1 < Content-Type: application/json; stream=true
1 < Server: Jetty(6.1.25)
1 < 
{"columns":[],"data":[]}

Batching all operations within a transaction

The documentation at https://github.com/neo4j/java-rest-binding states:

“In 1.8 it tries to collect all operations within a tx as a batch-operation which will then be executed on the server. This has the implication that the results retrieved within that “tx” are not immediately available but only after you called tx.success and tx.finish“

However, note that this is NOT the default behavior as you can see from Example 1. To turn this on, you need to set the following system property:org.neo4j.rest.batch_transaction=true

Once the system property is set and Example 1 is re-run, the REST calls look like(requests only):

INFO: 1 * Client out-bound request
1 > POST http://localhost:7474/db/data/batch
1 > Accept: application/json; stream=true
1 > X-Stream: true
1 > Content-Type: application/json
1 > 
[{"id":1,"to":"node","body":{"id":200,"name":"secondNode"},"method":"POST"},{"id":2,"to":"node","body":{"id":200,"name":"secondNode"},"method":"POST"},{"id":3,"to":"{1}/relationships","body":{"to":"{2}","type":"KNOWS"},"method":"POST"}]

INFO: 2 * Client out-bound request
2 > POST http://localhost:7474/db/data/cypher
2 > Accept: application/json; stream=true
2 > X-Stream: true
2 > Content-Type: application/json
2 > 
{"query":"start n=node(*) return count(n) as total","params":{}}

You may also explicitly create batch operations like so:

List<Node> response =graphDb.executeBatch(new BatchCallback<List<Node>>() {

            @Override
            public List<Node> recordBatch(RestAPI batchRestApi) {
                List<Node> nodes=new ArrayList<Node>();

                Map props=new HashMap<String, Object>();
                props.put("id",600);
                nodes.add(batchRestApi.createNode(props));

                Map props2=new HashMap<String, Object>();
                props2.put("id",500);
                nodes.add(batchRestApi.createNode(props2));
                return nodes;
            }
        });

This translates into:

INFO: 1 * Client out-bound request
1 > POST http://localhost:7474/db/data/batch
1 > Accept: application/json; stream=true
1 > X-Stream: true
1 > Content-Type: application/json
1 > 
[{"id":1,"to":"node","body":{"id":600},"method":"POST"},{"id":2,"to":"node","body":{"id":500},"method":"POST"}]

Any of the Cypher/Batching approaches are highly recommended over the finer grained Neo4j Java API. In the final post, we will take a look how transactions behave in the context of the REST binding.
 

Reference: Neo4j Java REST Binding – Part 2 (Batching) from our JCG partner Luanne Misquitta at the Thought Bytes blog.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

JPA Mini Book

Learn how to leverage the power of JPA in order to create robust and flexible Java applications. With this Mini Book, you will get introduced to JPA and smoothly transition to more advanced concepts.

JVM Troubleshooting Guide

The Java virtual machine is really the foundation of any Java EE platform. Learn how to master it with this advanced guide!

Given email address is already subscribed, thank you!
Oops. Something went wrong. Please try again later.
Please provide a valid email address.
Thank you, your sign-up request was successful! Please check your e-mail inbox.
Please complete the CAPTCHA.
Please fill in the required fields.

Leave a Reply


two × = 16



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.
Do you want to know how to develop your skillset and become a ...
Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you two of our best selling eBooks for FREE!

Get ready to Rock!
You can download the complementary eBooks using the links below:
Close