在 Neo4J 中,如何将标签设置为 Java 密码查询中的参数? [英] In Neo4J, how to set the label as a parameter in a cypher query from Java?

查看:19
本文介绍了在 Neo4J 中,如何将标签设置为 Java 密码查询中的参数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在来自 Java 的 Neo4J 中的密码中遇到参数问题.我运行嵌入的数据库.

I have problems with parameter in cypher in Neo4J from Java. I run the the database embedded.

代码应该是这样的(GraphDB.cypher直接进入ExecutionEngine)

The code should be like this (GraphDB.cypher goes directly to the ExecutionEngine)

HashMap<String, Object> parameter = new HashMap<>();
parameter.put("theLabel1", "Group");
parameter.put("theRelation", "isMemberOf");
parameter.put("theLabel2", "Person");
GraphDB.cypher("MATCH (n1:{theLabel1})-[r:{theRelation}]->(n2:{theLabel2}) RETURN n1, r, n2", parameter);

但它以这个异常结束

Exception in thread "main" Invalid input '{': expected whitespace or a label name (line 1, column 11)
"MATCH (n1:{theLabel1})-[r:{theRelation}]->(n2:{theLabel2}) RETURN n1, r, n2"

文档(和教程)告诉使用 { } 来覆盖参数,但这也用作属性的 cypher json 表示法.@参见 http://docs.neo4j.org/chunked/milestone/tutorials-cypher-parameters-java.html

The documentation (and tutorial) tells to use the { } to cover the parameters, BUT this is also used as the cypher json notation for properties. @See http://docs.neo4j.org/chunked/milestone/tutorials-cypher-parameters-java.html

是否有另一种方法来解决这个问题,而不是像这样(或使用其他模板方法)构建查询字符串

Is there another way to solve this issue rather than building the query string like this (or with other template methods)

GraphDB.cypher("MATCH (n:" + labelName + ")-[r:" + relationName + "]->...

这是必需的,因为目标标签可以改变,我想完全重用代码.

This is needed because the target label can change and I want to reuse the code completly.

提前致谢.

[[在得到(叹气)没有答案后编辑]]

由于目前(2014.6)不支持这种形式的参数,我会在发送查询之前运行一个小的替换器.

Since this form of parameter is currently (2014.6) not supported, I will run a little replacer right before sending the query.

HashMap<String, Object> parameter = new HashMap<>();
parameter.put("theLabel1", "Group");
parameter.put("theRelation", "isMemberOf");
parameter.put("theLabel2", "Person");

parameter.put("aName", "Donald Duck");

GraphDB.cypher("MATCH (n1:#theLabel1#)-[r:#theRelation#]->(n2:#theLabel2#) WHERE n2.Name = {aName} RETURN n1, r, n2", parameter);

... with ...

public static ExecutionResult cypher(String query, Map<String, Object> params) {
    for (String key : params.keySet()) {
        query = query.replaceAll("#" + key + "#", String.valueOf(params.get(key)));
    }
    return params == null ? cypherEngine.execute(query) : cypherEngine.execute(query, params);
}

可以有更多的可读性

推荐答案

恐怕暂时不支持.

这可能与本期中解释的原因完全相同:https://github.com/neo4j/neo4j/pull/1542.

And it might for the very same reason than the one explained in this issue: https://github.com/neo4j/neo4j/pull/1542.

参数化查询背后的想法是重用(缓存)执行计划.如果节点标签或关系类型不同,则执行计划将完全不同,从而破坏执行计划缓存的有用性.

The idea behind parametrized queries is to re-use (cache) execution plans. If a node label or a relationship type varies, the execution plan wouldn't be the same at all, thus ruining the usefulness of execution plan caching.

这篇关于在 Neo4J 中,如何将标签设置为 Java 密码查询中的参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆