为现有节点创建唯一索引

时间:2013-10-01 18:29:18

标签: java neo4j

我正在使用Neo4j批量插入器从RDBMS导入一些数据。处理,插入该数据并缓存一些节点以便稍后进行唯一索引。问题在于,当使用UniqueFactory时,会再次创建节点,从而导致重复。我确定我错过了什么。

有没有办法为尚未编入索引的现有节点创建唯一索引?最好使用Java客户端调用?

编辑:这是简化代码

BatchInserter inserter = BatchInserters.inserter(DB_PATH);
try {
    for (Person p : rdbms.getPersons()) {
        // Process data, modeling in graph form
        inserter.createNode(personProperties);
        Long cityId = inserter.createNode(cityProperties);
        cache.put(cityId, Type.CITY);
        // More nodes
        // Relationships (they aren't cached)
    }
} finally {
    inserter.shutdown();
}

GraphDatabaseService db = new GraphDatabaseFactory()
                .newEmbeddedDatabase(DB_PATH);
try {
    for (Entry<Long, Type> entry : cache.entrySet()) {
        Long nodeId = entry.getKey();
        Type nodeType = entry.getValue();
        UniqueFactory<Node> nodeFactory = new UniqueFactory.UniqueNodeFactory(
            db, nodeType.name().toLowerCase()) {
                @Override protected void initialize(
                        Node created, Map<String, Object> properties) {
                    for (Entry<String, Object> entry : properties.entrySet()) {
                        created.setProperty(entry.getKey(), entry.getValue());
                    }
                }
            };
        Node nodeToIndex = db.getNodeById(nodeId);
        Entry<String, Object> nameAndValue 
                = getIndexedPropertyNameAndValue(nodeType, node);
        nodeFactory.getOrCreate(nameAndValue.getKey(), nameAndValue.getValue());
    }
} finally {
    db.shutdown();
}

0 个答案:

没有答案