2014-03-25 48 views
0

我試圖編寫一個程序,將數據輸入到Neo4j數據庫中。該項目似乎運作良好 - 我這樣說是因爲它運行沒有問題和Neo4j數據沒有顯示在Web控制檯中

System.out.println(node.getProperty("Name")); 

線返回,我創建的每個節點的值。然而,當我轉到Web控制檯時,在數據庫中看不到任何這些節點。我很積極,我正在寫它到正確的目錄。做一個ls -l顯示每個我運行程序時的分貝越來越修改:

new-host-4:new_graph.db USER$ ls -l 
total 504 
-rw-r--r-- 1 USER wheel  11 Mar 25 00:55 active_tx_log 
drwxr-xr-x 8 USER wheel  272 Mar 25 01:21 index 
-rw-r--r-- 1 USER wheel 111766 Mar 25 01:21 messages.log 
-rw-r--r-- 1 USER wheel  78 Mar 25 01:21 neostore 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.id 
-rw-r--r-- 1 USER wheel  22 Mar 25 01:21 neostore.labeltokenstore.db 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.labeltokenstore.db.id 
-rw-r--r-- 1 USER wheel  64 Mar 25 01:21 neostore.labeltokenstore.db.names 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.labeltokenstore.db.names.id 
-rw-r--r-- 1 USER wheel  100 Mar 25 01:21 neostore.nodestore.db 
-rw-r--r-- 1 USER wheel  41 Mar 25 01:21 neostore.nodestore.db.id 
-rw-r--r-- 1 USER wheel  93 Mar 25 01:21 neostore.nodestore.db.labels 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.nodestore.db.labels.id 
-rw-r--r-- 1 USER wheel  635 Mar 25 01:21 neostore.propertystore.db 
-rw-r--r-- 1 USER wheel  153 Mar 25 01:21 neostore.propertystore.db.arrays 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.propertystore.db.arrays.id 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.propertystore.db.id 
-rw-r--r-- 1 USER wheel  43 Mar 25 01:21 neostore.propertystore.db.index 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.propertystore.db.index.id 
-rw-r--r-- 1 USER wheel  140 Mar 25 01:21 neostore.propertystore.db.index.keys 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.propertystore.db.index.keys.id 
-rw-r--r-- 1 USER wheel  154 Mar 25 01:21 neostore.propertystore.db.strings 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.propertystore.db.strings.id 
-rw-r--r-- 1 USER wheel  57 Mar 25 01:21 neostore.relationshipstore.db 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.relationshipstore.db.id 
-rw-r--r-- 1 USER wheel  33 Mar 25 01:21 neostore.relationshiptypestore.db 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.relationshiptypestore.db.id 
-rw-r--r-- 1 USER wheel  102 Mar 25 01:21 neostore.relationshiptypestore.db.names 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.relationshiptypestore.db.names.id 
-rw-r--r-- 1 USER wheel  82 Mar 25 01:21 neostore.schemastore.db 
-rw-r--r-- 1 USER wheel  9 Mar 25 01:21 neostore.schemastore.db.id 
-rw-r--r-- 1 USER wheel  4 Mar 25 01:21 nioneo_logical.log.active 
-rw-r--r-- 1 USER wheel  856 Mar 25 00:55 nioneo_logical.log.v0 
-rw-r--r-- 1 USER wheel  464 Mar 25 00:57 nioneo_logical.log.v1 
-rw-r--r-- 1 USER wheel  316 Mar 25 01:05 nioneo_logical.log.v2 
-rw-r--r-- 1 USER wheel  316 Mar 25 01:21 nioneo_logical.log.v3 
drwxr-xr-x 3 USER wheel  102 Mar 25 00:55 schema 
-rw-r--r-- 1 USER wheel  0 Mar 25 00:55 store_lock 
-rw-r--r-- 1 USER wheel  500 Mar 25 01:21 tm_tx_log.1 
new-host-4:new_graph.db USER$ pwd 
/usr/local/Cellar/neo4j/2.0.1/libexec/data/new_graph.db 

然而,當我進入 START a = node(*) RETURN a;

我沒有得到任何節點。如果我在這個數據庫上運行EmbeddedNeo4j.java示例,那些節點將顯示在數據庫中,但不是這些節點。控制檯配置設置顯示相同的目錄enter image description here

我錯過了什麼或Neo4J中是否存在錯誤?

僅供參考我使用的類如下。主呼叫測試者,打開一個流。流獲取數據庫的句柄,並在將規則應用於輸入(未顯示)後,使用NeoProcessor創建條目。這些屬性沒有問題地打印到控制檯,導致我相信它們已經寫入數據庫,但我從來沒有在Web控制檯中看到它們。

我的主:

public class NeoTest { 


    public static void main(final String[] args) 
    { 


     //Our tester to test the database 
     Tester myTest = new Tester("Stream"); 

     try{ 
      myTest.fileTester("/Users/me/data/test"); 
      myTest.end(); 
     } 
     catch (IOException ex) 
     { 
      System.out.println("Cannot open file fool"); 
     } 

    } 


} 

我的測試:

public class Tester { 

    private Streamer myStream; 
    private String inputType; 

    public Tester(String inputType) 
    { 
     this.inputType = inputType; 
     if(inputType.toLowerCase().contains("Stream".toLowerCase())) 
     { 
      System.out.println("Using Stream"); 
      // instantiate streamer 
      myStream = new Streamer(); 
      //get DB going 
      myStream.openStream("calllog"); 
     } 
    } 

    public void fileTester(String fileName) throws IOException 
    { 
     System.out.println("hi"); 
     BufferedReader in = new BufferedReader(new FileReader(fileName)); 

     while(in.ready()) 
     { 
      String s = in.readLine(); 
      myStream.streamInput(s); 
     } 
     in.close(); 
    } 

    public void end() 
    { 
     if(inputType.toLowerCase().contains("Stream".toLowerCase())) 
       myStream.closeStream(); 
    } 

} 

我的處理程序是:

public class NeoHandle { 

    private static GraphDatabaseService graphDb; 
    private String DB_PATH = "/usr/local/Cellar/neo4j/2.0.1/libexec/data/new_graph.db"; 

    NeoHandle() 
    { 
     this.graphDb = new GraphDatabaseFactory().newEmbeddedDatabase(DB_PATH); 
    } 


    public GraphDatabaseService getNeoHandle() 
    { 
     return graphDb; 
    } 

    public void createDb() 
    { 
     // START SNIPPET: startDb 
     registerShutdownHook(graphDb); 
     // END SNIPPET: startDb 
    } 

    private static void registerShutdownHook(final GraphDatabaseService graphDb) 
    { 
     // Registers a shutdown hook for the Neo4j instance so that it 
     // shuts down nicely when the VM exits (even if you "Ctrl-C" the 
     // running application). 
     Runtime.getRuntime().addShutdownHook(new Thread() 
     { 
      @Override 
      public void run() 
      { 
       graphDb.shutdown(); 
      } 
     }); 
    } 

    public void shutDown() 
    { 
     System.out.println(); 
     System.out.println("Shutting down database ..."); 
     // START SNIPPET: shutdownServer 
     graphDb.shutdown(); 
     // END SNIPPET: shutdownServer 
    } 
} 

我的流光:

public class Streamer { 

    private NeoHandle myHandle; 

    private String contextType; 

    Streamer() 
    { 

    } 


    public void openStream(String contextType) 
    { 
     myHandle = new NeoHandle(); 
     myHandle.createDb(); 
    } 

    public void streamInput(String dataLine) 
    { 
     LinkedList context = new LinkedList(); 
     /* 
     * get database instance 
     * write to database 
     * check for errors 
     * report errors & success 
     */ 
     System.out.println(dataLine); 

     //apply rules to data (make ContextRules do this, send type and string of data) 
     ContextRules contextRules = new ContextRules(); 
     context = contextRules.processContextRules("Calls", dataLine); 

     //write data (using linked list from contextRules) 
     NeoProcessor processor = new NeoProcessor(myHandle); 
     processor.processContextData(context); 
    } 

    public void closeStream() 
    { 
     /* 
     * close database instance 
     */ 
     myHandle.shutDown(); 
    } 

} 

我的處理器是:

public class NeoProcessor { 

    private NeoHandle handle; 

    public NeoProcessor(NeoHandle handle) 
    { 
     this.handle = handle; 
    } 

    public void processContextData(LinkedList context) 
    { 
     for(Object c : context) 
     { 
      if(c instanceof Entity) 
      { 
       Node node = addNode((Entity)c); 
       ((Entity)c).setNode(node);   
      } 

     } 
    } 

    public Node addNode(Entity myNode) 
    { 
     //TODO - wrap in transaction 
     GraphDatabaseService graphDb = this.handle.getNeoHandle(); 

     Node node; 


     try (Transaction tx = graphDb.beginTx()) 
     { 
      node = graphDb.createNode(); 
      if(myNode.hasName()) 
       { 
       System.out.println("setting name"); 
       node.setProperty("Name", myNode.getName()); 
       System.out.println(node.getProperty("Name")); 
       } 

     }//try transaction 

     return node; 
    } 
} 
+0

UGGGG,在這裏輸入所有這些信息之後,我找出了錯誤。我遺漏了tx.success();在NeoProcessor類方法addNode中的事務結束處調用。我會回答這個問題,因爲我認爲在這裏保留這個問題比刪除它好,因爲它可能(?)作爲其他的例子嗎? – WildBill

回答

0

我離開了在NeoProcessor的tx.success()電話。當我在事務中包裝時,我必須提交事務。

相關問題