2013-08-23 80 views
0

我通過talend將約60k個節點加載到neo4j中。我想通過java訪問這些節點。 Java代碼只能夠獲取neo4j本身附帶的單個節點,即第0個節點。通過java檢索neo4j節點數據時出錯

我的Java代碼是:

package com.Neo4J; 

import java.io.File; 
import java.io.IOException; 
import java.util.Iterator; 
import java.util.List; 
import java.util.Map; 
import java.util.Map.Entry; 

import javax.naming.spi.DirStateFactory.Result; 

import org.neo4j.cypher.javacompat.ExecutionEngine; 
import org.neo4j.cypher.javacompat.ExecutionResult; 
import org.neo4j.graphdb.GraphDatabaseService; 
import org.neo4j.graphdb.Node; 
import org.neo4j.graphdb.Transaction; 
import org.neo4j.graphdb.factory.GraphDatabaseFactory; 
import org.neo4j.helpers.collection.IteratorUtil; 
import org.neo4j.kernel.impl.util.FileUtils; 
import org.neo4j.tooling.GlobalGraphOperations; 

public class CaseNeo4J { 

private static final String DB_PATH = "data/graph.db"; 
GraphDatabaseService graphDataService; 
String nodeResult, resultString, columnsString; 
String rows = ""; 

public static void main(String args[]) { 
    CaseNeo4J neoobj = new CaseNeo4J(); 
    neoobj.connect(); 

} 

void connect() { 

    graphDataService = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); 
    Transaction transaction = graphDataService.beginTx(); 

    try { 
     ExecutionEngine engine = new ExecutionEngine(graphDataService); 
     ExecutionResult result = engine.execute("START n = node(0) return  n"); 
     System.out.println(result.dumpToString()); 
    } 

    finally { 
     transaction.finish(); 
    } 
} 
} 

正如你可以定製列表看到我試圖retireve節點(0),這是給我正確的結果

+-----------+ 
| n   | 
+-----------+ 
| Node[0]{} | 
+-----------+ 
1 row 

但只要我給查詢像START N =節點(1)返回否它引發錯誤原樣

Exception in thread "main" org.neo4j.cypher.EntityNotFoundException: Node 1 not found 
    at  org.neo4j.cypher.internal.spi.gdsimpl.GDSBackedQueryContext$$anon$1.getById(GDSBackedQueryC  ontext.scala:80) 
at org.neo4j.cypher.internal.spi.gdsimpl.GDSBackedQueryContext$$anon$1.getById(GDSBackedQueryC  ontext.scala:48) 
at org.neo4j.cypher.internal.executionplan.builders.NodeByIdBuilder$$anonfun$org$neo4j$cypher$  internal$executionplan$builders$NodeByIdBuilder$$f$1$1.apply(NodeByIdBuilder.scala:41) 
at org.neo4j.cypher.internal.executionplan.builders.NodeByIdBuilder$$anonfun$org$neo4j$cypher$  internal$executionplan$builders$NodeByIdBuilder$$f$1$1.apply(NodeByIdBuilder.scala:41) 
at org.neo4j.cypher.internal.executionplan.builders.GetGraphElements$.org$neo4j$cypher$interna  l$executionplan$builders$GetGraphElements$$castElement$1(GetGraphElements.scala:30) 
at org.neo4j.cypher.internal.executionplan.builders.GetGraphElements$$anonfun$getElements$3.ap  ply(GetGraphElements.scala:40) 
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328) 
at scala.collection.Iterator$$anon$11.next(Iterator.scala:328) 
at scala.collection.Iterator$$anon$13.next(Iterator.scala:372) 
at org.neo4j.cypher.internal.ClosingIterator$$anonfun$next$1.apply(ClosingIterator.scala:44) 
at org.neo4j.cypher.internal.ClosingIterator.failIfThrows(ClosingIterator.scala:86) 
at org.neo4j.cypher.internal.ClosingIterator.next(ClosingIterator.scala:43) 
at scala.collection.Iterator$class.foreach(Iterator.scala:727) 
at org.neo4j.cypher.internal.ClosingIterator.foreach(ClosingIterator.scala:31) 
at scala.collection.generic.Growable$class.$plus$plus$eq(Growable.scala:48) 
at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:178) 
at scala.collection.mutable.ListBuffer.$plus$plus$eq(ListBuffer.scala:45) 
at scala.collection.TraversableOnce$class.to(TraversableOnce.scala:259) 
at org.neo4j.cypher.internal.ClosingIterator.to(ClosingIterator.scala:31) 
at scala.collection.TraversableOnce$class.toList(TraversableOnce.scala:243) 
at org.neo4j.cypher.internal.ClosingIterator.toList(ClosingIterator.scala:31) 
at org.neo4j.cypher.PipeExecutionResult.eagerResult(PipeExecutionResult.scala:100) 
at org.neo4j.cypher.PipeExecutionResult.dumpToString(PipeExecutionResult.scala:103) 
at org.neo4j.cypher.PipeExecutionResult.dumpToString$lzycompute(PipeExecutionResult.scala:143) 
at org.neo4j.cypher.PipeExecutionResult.dumpToString(PipeExecutionResult.scala:140) 
at org.neo4j.cypher.javacompat.ExecutionResult.dumpToString(ExecutionResult.java:102) 
at com.Neo4J.CaseNeo4J.connect(CaseNeo4J.java:45) 
at com.Neo4J.CaseNeo4J.main(CaseNeo4J.java:32) 

但是如果我運行了START n =節點(1)返回n這個查詢在neo4j shell的web UI中它給了我正確的結果。

對此有任何想法嗎?如果我在這裏得到一些解決方案,我將非常感激。謝謝!

回答

2

您確定您指向與neo4j服務器實例和talend所使用的DB_PATH相同的數據位置嗎?你能檢查絕對路徑嗎? AFAIK當您在沒有數據存在的路徑上啓動嵌入式數據庫實例時,它會創建一個空白數據集,其中只包含ID爲0的根節點。

0

能夠解決此挑戰。提及完整的DB_PATH。感謝所有爲此付出時間的人。