user1802481
user1802481

Reputation: 133

java example of running cypher queries in neo4j using rest

trying to build a java client for accessing the neo4j data , I don't want to use embedded mode of Neo4j please somebody give me the example code for the same am trying to run following code

    import org.neo4j.rest.graphdb.RestAPI;
    import org.neo4j.rest.graphdb.RestAPIFacade;
    import org.neo4j.rest.graphdb.RestGraphDatabase;
    import org.neo4j.rest.graphdb.query.RestCypherQueryEngine;
    import org.neo4j.rest.graphdb.util.QueryResult;
    import static org.neo4j.helpers.collection.MapUtil.map;
    import java.util.Map;


    public class CypherQuery {
         public static void main(String[] args) {
             try{
          System.out.println("starting test");
         final RestAPI api = new RestAPIFacade("http://localhost:7474/db/data/");
         System.out.println("API created");
         final RestCypherQueryEngine engine = new RestCypherQueryEngine(api);
         System.out.println("engine created");
         final QueryResult<Map<String,Object>> result = engine.query("start n=node(2) return n, n.name as name;", map("id", 0));

         System.out.println("query created");
         for (Map<String, Object> row : result) {
            long id=((Number)row.get("id")).longValue();
            System.out.println("id is " + id);
         }
         }
         catch(Exception e)
         {
            e.printStackTrace(); 

         }
         }
       }

But it is not showing any error or exception and it is not producing any output.

Upvotes: 1

Views: 4580

Answers (2)

Michael Hunger
Michael Hunger

Reputation: 41676

This works, you didn't have an id result column and were also not passing in a parameter (which is recommended, using parameters)

public class CypherQuery {
     public static void main(String[] args) {
         try{
      System.out.println("starting test");
     final RestAPI api = new RestAPIFacade("http://localhost:7474/db/data/");
     System.out.println("API created");
     final RestCypherQueryEngine engine = new RestCypherQueryEngine(api);
     System.out.println("engine created");
     final QueryResult<Map<String,Object>> result = engine.query("start n=node({id}) return id(n) as id, n.name? as name;", map("id", 2));

     System.out.println("query created");
     for (Map<String, Object> row : result) {
        long id=((Number)row.get("id")).longValue();
        System.out.println("id is " + id);
     }
     }
     catch(Exception e)
     {
        e.printStackTrace();

     }
     }
   }

Upvotes: 1

Stefan Armbruster
Stefan Armbruster

Reputation: 39905

Looks like a typo, you have three "t" in the URL htttp://localhost:7474/db/data

Upvotes: 1

Related Questions