2011-04-16 71 views

回答

35

丹尼爾已經回答了,這是可能的二進制對象存儲在Neo4j的。

但我建議你不要這樣做。您可以對數據庫中的二進制對象做任何事情。你不能搜索它們。您將通過存儲二進制對象來實現唯一的事情 - 增加數據庫的文件大小。請注意,Neo4J不能水平擴展。它沒有自動分片。所以如果你的分貝變得太大,你就有麻煩了。通過在文件系統或外部分佈式鍵值存儲(如riak,cassandra,hadoop等)中存儲二進制文件,您可以保持數據庫的小型化,這對性能,備份和避免水平擴展問題都很有幫助。

+0

很好的啓示。謝謝你的提醒。 – 2013-09-05 19:35:14

6

您可以存儲二進制對象的字節[]或字符串編碼,但我會建議存放較大(例如> 1000個字節)的斑點作爲單獨的文件,並只保留在你的數據庫文件的引用。

我們Structr(http://structr.org)做到這一點。

1

如上所述,這樣做是非常不利的。

但是,如果你決定這樣做,你可以像下面這樣做在C#:

using Neo4jClient; 
using Neo4jClient.Cypher; 
using System; 
using System.Collections.Generic; 
using System.IO; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace Neo4JBlob 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      try 
      { 
       GraphClient client = new GraphClient(new Uri("http://localhost:7474/db/data")); 
       client.Connect(); 

       byte[] image = File.ReadAllBytes("image.jpg"); 
       BlobNode blob = new BlobNode(){Blob = image, name = "An image: " + DateTime.Now.ToShortDateString()}; 

       client.Cypher.Create("(blob:Blob {category})").WithParam("category", blob).ExecuteWithoutResults(); 

       var res = client.Cypher.Match("(b:Blob)").Return<BlobNode>(b => b.As<BlobNode>()).Limit(1).Results; 
       BlobNode BlobReturned = res.First(); 
       File.WriteAllBytes("image_returned.jpg", BlobReturned.Blob); 

      } 
      catch (Exception ex) 
      { 
       Console.WriteLine(ex.Message); 
       Console.WriteLine(ex.StackTrace); 
      } 
      Console.ReadKey(); 
     } 

     class BlobNode 
     { 
      public byte[] Blob 
      { 
       get; 
       set; 
      } 
      public string name 
      { 
       get; 
       set; 
      } 
     } 
    }  
}