2011-07-05 76 views
0

我在嵌入式Java版Neo4j中訪問以前創建的數據庫時遇到問題。我想要做的是打開一個GraphDatabaseService,添加幾百萬個關係(不使用BatchInserter,只有事務),然後關閉最後一個事務和連接。這看起來是這樣的:Neo4j在會話中查詢數據

public class startNeo4j{ … 
    public static void main (String[] args) { 
    GraphDatabaseService graphDb = new EmbeddedGraphDatabase("data/test/base"); 
    Transaction tx = graphDb.beginTx(); 
    IndexManager index = graphDb.index(); 
    Index<Node> userIds = index.forNodes("userIds"); 
    RelationshipIndex follows = index.forRelationships("follows"); 

[我在這裏輸入一個非常大的CSV(幾百萬關係),還贈送關係,用戶id指數]

tx.finish(); 
    graphDb.shutdown(); }} 

我則需要能夠做到打開一個新的GraphDatabaseService並訪問我剛剛插入的所有數據。我檢查了Neo4j列表,他們證實這是可能的,但沒有提供任何細節。

我不想重新創建索引,但是當我嘗試重新打開它時,出現索引(userIds from above)「無法解析」的錯誤。理想情況下,如果有人對第二套代碼看起來會是什麼樣子有一個大概的瞭解。我的非功能性的一個樣子:

public class examineNeo4j{ 
    public static void main (String[] args){ 
    GraphDatabaseService graphDb = new EmbeddedGraphDatabase("data/test/base"); 
    Transaction tx = graphDb.beginTx(); 
    IndexHits<Node> hits_final = userIds.get("userId","12"); 
    Node testthis = hits_final.getSingle(); 

[或者說我想運行其他一些查詢]

tx.finish(); 
    graphDb.shutdown();}} 

任何幫助將不勝感激!

回答

1

你也必須做tx.success();默認情況下tx處於「回滾」狀態。

Transaction tx = graphDb.beginTx(); 
try { 
    // do your work here 
    tx.success(); 
} finally { 
    tx.finish(); 
} 
graphdb.shutdown(); 

請記住,您的tx大小不應超過10k左右的操作。所以請批量處理這樣的塊大小。