我正在尝试在Neo4j数据库中创建节点和关系。创建节点,因为我可以打印出节点ID,但它们不会持久存在。我的程序的下一个阶段是计算索引中的节点数,并给出NullPointerExpection错误。我正在使用Gephi来可视化图形,它既不显示节点也不显示关系。如何让节点保持不变?这可能是因为我有一个包含在事务中的另一个方法(由于长度而未显示),它调用这些节点创建方法吗?感谢
以下是代码的一部分:
public class Neo4JGraphDbClass {
private String graphLocation = "D:\\testdb" ;
private GraphDatabaseService graphDb ;
private IndexManager graphIndex ;
private Index<Node> property1;
private Index<Node> special_nodes ;
public Neo4JGraphDbClass() {}
public void createGraphDb() {
graphDb = new EmbeddedGraphDatabase(graphLocation) ;
graphIndex = graphDb.index();
property1 = graphIndex.forNodes("PROPERTY1");
special_nodes = graphIndex.forNodes("SPECIAL_NODES");
}
public Node createAndIndex(Integer[] property1) {
Transaction transaction0 = graphDb.beginTx();
try {
Node node = graphDb.createNode();
node.setProperty("PROPERTY1",property1) ;
property1.putIfAbsent(node, "PROPERTY1", property1);
System.out.println(node.getId());
transaction0.success();
return node;
} finally {
transaction0.finish();
}
}
public Node createSpecialNodes(Integer name) {
Transaction transaction1 = graphDb.beginTx();
try {
Node node = graphDb.createNode();
node.setProperty("SPECIAL_NODE", name);
special_nodes.add(node, "SPECIAL_NODE", name);
transaction1.success();
return node;
}
finally {
transaction1.finish();
}
}
public void addNodesToGraph(Integer preName, ArrayList<Integer[]> preProperty1) {
//stuff to change preName into name and preProperty into property
Transaction transaction = graphDb.beginTx();
try {
Node specialNode = createSpecialNodes(name) ;
Node previousNode = graphDb.getReferenceNode();
for (int i = 0; i < property1.size(); i++) {
Node currentNode = createAndIndex(property1.get(i));
previousNode.createRelationshipTo(currentNode, RelTypes.NEXT_MEASURE);
currentNode.createRelationshipTo(specialNode,RelTypes.SAMPLE);
previousNode = currentNode; }
transaction.success();
}
finally {
transaction.finish();
}
}
public Integer countIndex() {
Integer hitSize;
Transaction tx = graphDb.beginTx();
try {
IndexHits<Node> hits = graphIndex.forNodes("PROPERTY1").get("PROPERTY1", "*");
hitSize = hits.size();
tx.success();
}
finally {
tx.finish();
}
return hitSize;
}
public Integer indexSize(Integer lookup) throws NullPointerException{
Integer hitSize;
Transaction tx2 = graphDb.beginTx();
try {
IndexHits<Node> hits = graphIndex.forNodes("PROPERTY1").query(lookup);
hitSize = hits.size();
tx2.success();
}
finally {
tx2.finish();
}
return hitSize;
}
答案 0 :(得分:0)
代码看起来不错。您的查询是什么样的,即您如何得出数据未被持久化的结论?