2016-07-27 157 views
0

我正在使用Neo4j作爲數據庫的項目。我有一個爲應用程序構建的遠程數據庫,並託管在Cloud中。還有另一個數據庫(本地數據庫),位於客戶位置。Neo4j - 如何同步本地數據庫與遠程neo4j數據庫服務器

問題是:有沒有辦法同步兩個數據庫?有沒有可用於此同步的直接COTS產品?

+0

我認爲這是一個有效的問題,但我相信你應該刪除粗體句子,因爲它直接違揹我們的一個偏離主題的原因:要求提供非現場資源或產品推薦。 –

+0

這就是我所知道的,這可能會對你有所幫助,它不會像你可能想要的那樣幫助你:http://stackoverflow.com/questions/18830686/export-whole-database-in-cypher-format -ascii-text –

回答

0

這取決於你的跑步版本。 如果您使用企業版,您可以同步開箱即用,但是如果您使用社區,則必須自己完成。

我通過記錄所有在本地數據庫上修改數據然後在遠程數據庫上重播的查詢來完成此操作。 下面的代碼是在C#這樣的例子

var query = _client.Cypher 
       .Match("(p:Person)") 
       .Where((Person p) => p.Id == id) 
       .Set("p = {person}") 
       .WithParam("person", person); 
      query.ExecuteWithoutResults(); 
      RecordQuery(query); 

private static void RecordQuery(dynamic query) 
     { 
      var syncStore = new SynchronisationStore(); 
      var replayQuery = syncStore.Create(); 
      replayQuery.TimeStamp = DateTime.Now; 
      var queryText = query.Query.QueryText.Replace("\r\n", " "); 
      replayQuery.CypherQueryText = queryText; 
      replayQuery.CypherQueryParameters = query.Query.QueryParameters; 
      syncStore.Store(replayQuery); 
     } 

然後,您可以使用您想使用回放服務器之間的字符串數據(我使用RESTful服務)和轉讓任何方法:

public void ReplayQuery(ReplayQuery replayQuery) 
     { 
       foreach (var pair in replayQuery.CypherQueryParameters) 
       { 
        replayQuery.CypherQueryText = replayQuery.CypherQueryText.Replace("{" + pair.Key + "}", SerializeWithoutQuote(pair.Value)); 
       } 
       var query = new CypherQuery(replayQuery.CypherQueryText, new Dictionary<string, object>(), CypherResultMode.Set, null); 

       _client.Connect(); 
       ((IRawGraphClient) _client).ExecuteCypher(query); 
      } 
     } 

注 - 您需要確保您只重播一次,並按順序重播查詢。

+0

有沒有更簡單的方法,或者有一些工具可以做到這一點?我有社區版本3 –

+0

我不知道任何工具可以爲你做到這一點。所以它要麼自己做,要麼分出Neo4j Enterprise的天價成本。通過上面的代碼和一些將重播查詢存儲爲字符串的機制,以及一個標誌來說明同步是否成功(我使用的是MongoDB),您應該在一天左右的時間內啓動並運行。 – joe

相關問題