1
votes

Background:

I am using a locally run Neo4J instance, (at localhost:7474), and accessing it through a Java adaptor which uses Cypher via the REST API (with Jersey), and makes data accessible to my Grails app running on the same server.

Question:

Is it possible to query a Neo4J db using Cypher, via the REST API, and return the URI of a node? Right now, I can check Neo4J server status, create nodes, populate node properties, query, and create relationships. My problem is that my "add relationship" and traversal code requires a node URIs as input. I can query for nodes and obtain the correct JSON describing the results, but I cannot seem to get the URI locations.

Here is a simplified version of my getUserByEmail code:

public URI getUserByEmail( String email )
{
    System.out.println( "GETTING USER BY EMAIL [" + email + "]..." );

    String queryStr = "MATCH (user) WHERE user.nodetype=\'user\' and user.email=\'" + email + "\' RETURN user";

    WebResource webResource = client.resource( ROOT_URI + "/transaction/commit" );

    String payload = "{\"statements\" : [ {\"statement\" : \"" + queryStr + "\"} ]}";

    ClientResponse response = webResource
        .accept( MediaType.APPLICATION_JSON )
        .type( MediaType.APPLICATION_JSON )
        .entity( payload )
        .post( ClientResponse.class );

    String responseStr = response.getEntity( String.class );
    URI responseLocation = response.getLocation();

    System.out.println( "RESPONSE STRING: " + responseStr );
    System.out.println( "GOT USER AT: [" + responseLocation + "]" );

    return responseLocation;
}

The JSON results come back fine and reflect what is in the graph db. The location, however, is always null.

The "add relationship" code that I am using works, as long as I have the URI to the start node. The code I have is based on the addRelationship() code that lives here:

https://github.com/neo4j/neo4j/blob/2.1.6/community/server-examples/src/main/java/org/neo4j/examples/server/CreateSimpleGraph.java

2

2 Answers

2
votes

In your JSON results, the self property value for each "user" will be its URI.

In this example, the response has 2 "n" nodes, and the self property value of each is its URI.

Here is an example of how to get the transactional endpoint (which is normally less verbose than the legacy endpoint) to also return the self property.

2
votes

You can in your case create the node uri yourself by just appending the node internal id to the following url :

http://localhost:7474/db/data/node/your-123-id

maybe you want to set the scheme, host and database port in a configuration file to not do hardcode changes when changing the database location.