类似地,代码用于创建索引和数百万个节点的相应方法。这是用于从JSON文件创建新数据库。
我遇到以下错误:
for (int command = 4; command < inputNeo4jCommands.size(); command++) {
log.info(inputNeo4jCommands.get(command));
NEO4JCOMMANDS cmnd = NEO4JCOMMANDS.valueOf(inputNeo4jCommands.get(command).toUpperCase());
log.info(NEO4JCOMMANDS.valueOf(inputNeo4jCommands.get(command).toUpperCase()).toString());
if (NEO4JCOMMANDS.CONSTRAINT.equals(cmnd)) {
CreateConstraints1();
}
if (NEO4JCOMMANDS.INDEX.equals(cmnd)) {
CreateIndexes();
}
if (NEO4JCOMMANDS.MERGE.equals(cmnd)) {
log.info("started creating nodes........");
CreateNodes();
}
}
private void CreateIndexes1() {
log.info("Adding indexes.....");
log.info("into started adding index ......");
try (Transaction tx = db.beginTx()) {
log.info("got a transaction .....hence started adding index ......");
Iterator<Indx> itIndex = json2neo4j.getIndexes().iterator();
while (itIndex.hasNext()) {
Indx indx = itIndex.next();
Label lbl = Label.label(indx.getLabelname());
Iterable<IndexDefinition> indexes = db.schema().getIndexes(lbl);
if (indexes.iterator().hasNext()) {
for (IndexDefinition index : indexes) {
for (String key : index.getPropertyKeys()) {
if (!key.equals(indx.getColName())) {
db.schema().indexFor(lbl).on(indx.getColName());
}
}
}
} else {
db.schema().indexFor(lbl).on(indx.getColName());
}
tx.success();
tx.close();
}
log.info("\nIndexes Created..................Retured the method call ");
}
}
答案 0 :(得分:0)
您的问题和代码示例中缺少许多上下文,因此很难给出明确的答案。代码示例中抛出的异常在哪里?没有CreateNodes()
方法,所以我们无法找出它失败的原因(由于交易太大导致内存不足错误?)。
但是,CreateIndexes1()
方法中的事务管理存在问题(顺便说一下,不遵循Java命名约定):
try (Transaction tx = db.beginTx()) {
// ...
while (/* ... */) {
// ...
tx.success();
tx.close();
}
}
您已多次关闭该交易,当它实际处于资源试用模块时,您根本不需要自己关闭该交易 :
try (Transaction tx = db.beginTx()) {
// ...
while (/* ... */) {
// ...
}
tx.success();
}
我猜json2neo4j
是JSON的反序列化,描述了要在标签上创建的索引。逻辑是有缺陷的:当你找到另一个属性的索引时,你会尝试为属性创建一个索引,当你应该知道当前属性的索引是否存在时,只有当它丢失时才创建索引然后
for (Indx indx : json2neo4j.getIndexes()) {
Label lbl = Label.label(indx.getLabelname());
boolean indexExists = false;
for (IndexDefinition index : db.schema().getIndexes(lbl)) {
for (String property : index.getPropertyKeys()) {
if (property.equals(indx.getColName())) {
indexExists = true;
break;
}
}
if (indexExists) {
break;
}
}
if (!indexExists) {
db.schema().indexFor(lbl).on(indx.getColName());
}
}