2012-05-17 4 views
0

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

외부 트랜잭션을 수행 하시겠습니까? 왜 포장 방법을 보여주지 않습니까? – Andres

+0

예, 외부 트랜잭션을 커밋하려고합니다. 랩핑 방법을 보여주지 않았는데, 그 이유는 Neo4j가 아닌 것들을 설명해야하고 이미 복잡한 질문을 나눌 위험이 있기 때문입니다. – blue

+0

어쨌든 배치 방법을 표시 할 수 있습니까? 이 코드는 지금까지는 좋아 보인다. –

답변

0

코드는 괜찮아 보이는 : 감사

다음은 코드의 일부입니다. 검색어가 어떻게 생겼는지, 즉 데이터가 지속되지 않는다는 결론에 도달 한 이유는 무엇입니까?

+0

0을 반환 한 내 property1 인덱스의 모든 인덱스 노드를 계산하기 위해 쿼리를 실행했습니다. 또한 특정 정수 (term)를 검색하는 쿼리와 nullpointerexception을 반환했습니다. 긴 이야기를 짧게하기 위해 Gephi를 사용하여 노드/관계가 있는지 확인했습니다. 그리고 나는 아무것도 얻지 못했다. 그래서 나는 다른 neo4j 데이터베이스와 함께 Gephi를 확인하고 노드/관계가 거기에 있었다. – blue

+0

index.get ("property1", "*")을 수행 할 수 없으므로 query (...)를 사용하십시오. 어떤 쿼리 문자열로 인해 NPE가 생성 되었습니까? –

+0

감사합니다. 쿼리로 변경하겠습니다. 쿼리는 6 자리의 정수, 583392이며 속성은 정수 배열입니다. – blue

관련 문제