【问题标题】:getOrCreate for java-rest-api neo4j failsjava-rest-api neo4j 的 getOrCreate 失败
【发布时间】:2013-05-21 22:51:40
【问题描述】:

我有一个简单的关系测试,我正在尝试运行它以使用 Rest API (java-rest-binding) https://github.com/neo4j/java-rest-binding 创建一个唯一节点,但不幸的是我遇到了一些问题,这里是详细信息:(非独特的节点和关系工作得很好,它没有,很可能我在做一些幼稚的事情(请原谅我缺乏对neo4j的了解)。

final UserModel userModel = new UserModel();
        final HashMap<String, Object> uModelAttributes = new HashMap<String, Object>(0);
        uModelAttributes.put("name", "AnirudhVyas");
        userModel.setAttributes(uModelAttributes);
        final HashSet<Action> buyHistory = new HashSet<Action>();
        final Action buyAction = new Action();
        final ProductModel productModel = new ProductModel();
        final HashMap<String, Object> attributes = new HashMap<String, Object>(0);
        attributes.put("name", "mercedes benz ");
        attributes.put("make", "mercedes benz");
        attributes.put("model", "sls 550");
        attributes.put("year", "2014");
        productModel.setAttributes(attributes);
        buyAction.setProduct(productModel);
        buyHistory.add(buyAction);
        userModel.setBuyHistory(buyHistory);
        System.out.println("Before");
        new UserModelDAO().createCompleteTree(userModel);
        System.out.println("Completed >>>

如果我在 dao 上使用它:

final RestNode root = api.getOrCreateNode(api.index().forNodes("users", MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "fulltext")), "name", m
                    .getAttributes().get("name"), m.getAttributes());

api.getOrCreateNode(api.index().forNodes("products", MapUtil.stringMap(IndexManager.PROVIDER, "lucene", "type", "fulltext")), "name", buyAction.getProduct().getAttributes().get("name"), buyAction.getProduct().getAttributes()), RelationshipTypes.BOUGHT);

这基本上失败了:

   java.lang.RuntimeException: Error retrieving or creating node for key name and value AnirudhVyas with index users
        at org.neo4j.rest.graphdb.ExecutingRestAPI.getOrCreateNode(ExecutingRestAPI.java:448)
        at org.neo4j.rest.graphdb.RestAPIFacade.getOrCreateNode(RestAPIFacade.java:223)
        at xxxx.xxxx.xxxx.graph.UserModelCreateTasteKeyNeo4JBatchCallback.recordBatch(UserModelCreateTasteKeyNeo4JBatchCallback.java:61)

【问题讨论】:

    标签: neo4j


    【解决方案1】:

    有几种方法可以做到这一点,其中一种是使用 Cypher:

    MATCH a
    WHERE a.name! = 'nameTofound'
    CREATE UNIQUE a-[:Relationship]-c:LABEL
    RETURN c
    

    在查询引擎中使用它。 它就像一个魅力,请查看以下链接以获取更多详细信息: http://docs.neo4j.org/chunked/milestone/query-create-unique.html

    Java 代码在这里:

    protected static RestNode createOrGetExistingNode(String key, String valueFor, String Rel, String label){
        RestNode node = null;
        final QueryResult<Map<String, Object>> result = GraphRestService.queryEngine.query(String.format("MATCH node WHERE node.%s! ='%s' " +
                "CREATE UNIQUE node-[:%s]-c:%s RETURN c" , key, valueFor, Rel, label), MapUtil.map("reference", 0));
        for (Map<String, Object> column : result) {
            node = (RestNode) column.get("c");
        }
        return node;
    }
    

    【讨论】:

      【解决方案2】:

      我通过不使用批处理休息回调解决了这个问题 - 当我简单地使用 - getOrCreateXXX( ) for RestAPI 时,它就像一个魅力 - 需要进一步调查为什么 BatchCallback#recordBatch( ) 上的 getOrCreate 会表现不同。

      【讨论】:

      • 批处理操作只是记录您的调用,然后在执行块时重放它们,因此任何类型的读取都只会延迟到块实际执行。
      • 想提供更多细节吗?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-07-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多