使用rest在neo4j中运行cypher查询的java示例

时间:2013-04-24 09:02:31

标签: java neo4j cypher

尝试构建一个用于访问neo4j数据的java客户端,我不想使用Neo4j的嵌入模式,请有人给我示例代码同样尝试运行以下代码

    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(); 

         }
         }
       }

但它没有显示任何错误或异常,也没有产生任何输出。

2 个答案:

答案 0 :(得分:1)

看起来像拼写错误,您在网址htttp://localhost:7474/db/data

中有三个“t”

答案 1 :(得分:1)

这样可行,你没有id结果列,也没有传入参数(建议使用参数)

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();

     }
     }
   }