繁体   English   中英

Neo4j通过Rest执行密码查询

[英]Neo4j Executing cypher query via rest

早上好,我建立了本地Neo4j数据库,并希望对Maven依赖关系图进行建模。 当我通过网络控制台执行以下语句时,一切正常:

start root = node(1)
create unique root -[:ROOT]-> (n{groupId:'fancyStuff',artifactId:'somewhat', version:'1.4'})
return n

(注意:rootnode用于调试目的,稍后将被实际结构替换)因此,无论我占用多少空格还是将'替换为“

在我的Java应用程序中,我具有以下功能:

private static URI getOrCreate(Artifact artifact){
        String cypherUri = SERVER_ROOT_URI + "cypher";

        String cypherStatement="{\"query\" : \"start x  = node(1) " +
                "create unique x -[:ROOT]-> (artifact{groupId:\"" + artifact.getGroupID() +
                "\", artifactId:\"" + artifact.getArtifactID() +
                "\", version: \"" + artifact.getVersion() +
                "\"}) return artifact ,\"params\" : {}}";

        WebResource resource = Client.create()
                .resource( cypherUri );
        ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
                .type( MediaType.APPLICATION_JSON_TYPE )
                .entity( cypherStatement )
                .post( ClientResponse.class );

        System.out.println( String.format( "POST to [%s], status code [%d]",
                cypherUri, response.getStatus() ) );

        response.close();
        return response.getLocation();
    }

所以基本上我发布了一个json文件,看起来像

{"query" : "start root = node(1) create unique root-[:ROOT]->(artifact{groupId:'{"query" : "start root = node(1) create unique root-[:ROOT]->(artifact{groupId:'lol',artifactId:'somewhat',version:'1.4'}) return artifact","params" : {}}

同样,无论使用什么空格或“ /”,我都会看到一个HTTP 500错误,即关系的第一个-[:ROOT]->无效。

通过直接发布新节点

final String nodeEntryPointUri = SERVER_ROOT_URI + "node";
WebResource resource = Client.create().resource( nodeEntryPointUri );
ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
         .type( MediaType.APPLICATION_JSON_TYPE )
         .entity( /*some json*/)
         .post(ClientResponse.class);

(免责声明:在此版本有效的情况下,我将把参数移到正确的位置;))

我敢打赌这完全是个小错误,但是我现在盯着这个工作了半个多天,而且我的所有版本都不想工作。 如果有人知道答案,那将会很棒。

弗洛里安·罗门(Florian Rohm)的问候

问题是您的JSON无效。 您重复query两次。 如果删除星号之间的部分,是否有效?

**{"query" : 
    "start root = node(1) 
    create unique root-[:ROOT]->(artifact{groupId:'**
{"query" : "start root = node(1) 
  create unique root-[:ROOT]->(artifact{groupId:'lol',artifactId:'somewhat',version:'1.4'}) 
  return artifact",
 "params" : {}
}

好的,我不知道这条语句有什么不同,但这行得通(我也尝试了上面代码中的param split):

String cypherUri = SERVER_ROOT_URI + "cypher";        
JSONObject jObject = new JSONObject();
        try {
            Map<String, String> params = new HashMap<String, String>();
            params.put("groupId", artifact.getGroupID());
            params.put("artifactId", artifact.getArtifactID());
            params.put("version", artifact.getVersion());
            String query = "start x  = node(1) create unique x-[:ROOT]->(n{groupId:{groupId},artifactId:{artifactId},version:{version} }) return n";
            jObject.put("query", query);
            jObject.put("params", params);
        } catch (Exception e) {
            e.printStackTrace();
        }

        WebResource resource = Client.create()
                .resource( cypherUri );
        ClientResponse response = resource.accept( MediaType.APPLICATION_JSON_TYPE )
                .type(MediaType.APPLICATION_JSON_TYPE)
                .entity(jObject.toString())
                .post(ClientResponse.class);

但是无论如何,第二次尝试更好,而且我不会抱怨:D它使我不知道那里发生了什么……

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM