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

问题描述

我在Java中使用Neo4J中的cypher参数时遇到问题。我运行嵌入的数据库。

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表示法。
@参见

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.

提前致谢。

[[获得后获得的信息] A(叹气)没有作为答案]]

由于这种形式的参数目前(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);
}

可以有更多的可读

推荐答案

我担心目前不支持这种情况。

I am afraid this is not supported at the moment.

这可能与原因完全相同本期中解释的一个:。

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的cypher查询中将标签设置为参数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-03 11:40