您需要使用 Cypher 吗?如果不是,由于您有 6000 万个节点,因此列出的密码查询tstorms
看起来不错并且工作正常,但它可能会很困难,因为它会围绕所有这些进行事务,这可能会导致大量内存使用。
您可以使用 Java API(我假设您正在使用 Java)手动执行此操作。
RelationshipType readsRelationshipType = DynamicRelationshipType.withName("reads");
RelationshipType writtenByRelationshipType = DynamicRelationshipType.withName("writtenBy");
RelationshipType isAFanOfRelationshipType = DynamicRelationshipType.withName("isAFanOf");
int counter = 0;
Transaction tx = db.beginTx();
try {
for (Node reader : GlobalGraphOperations.at(db).getAllNodes()) {
for (Relationship reads : reader.getRelationships(Direction.OUTGOING, readsRelationshipType)) {
Node book = reads.getOtherNode(reader);
for (Relationship writtenBy : book.getRelationships(Direction.OUTGOING, writtenByRelationshipType)) {
Node author = reads.getOtherNode(book);
try {
reader.createRelationshipTo(author, isAFanOfRelationshipType);
} catch (Exception e) {
// TODO: Something for exception
}
}
}
counter++;
if (counter % 100000 == 0) {
tx.success();
tx.finish();
tx = db.beginTx();
}
}
tx.success();
} catch (Exception e) {
tx.failure();
} finally {
tx.finish();
}
}
此代码假定错误处理和事务数,但您可以根据需要进行调整。