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;
}
외부 트랜잭션을 수행 하시겠습니까? 왜 포장 방법을 보여주지 않습니까? – Andres
예, 외부 트랜잭션을 커밋하려고합니다. 랩핑 방법을 보여주지 않았는데, 그 이유는 Neo4j가 아닌 것들을 설명해야하고 이미 복잡한 질문을 나눌 위험이 있기 때문입니다. – blue
어쨌든 배치 방법을 표시 할 수 있습니까? 이 코드는 지금까지는 좋아 보인다. –