Neo4j doesn't seem to allow me to store binary objects. Does this mean I must used Neo4j in conjuntion 开发者_JAVA百科with another data store, such as the filesystem., Oracle, etc?
Daniel already answered that it's possible to store binary objects in Neo4J.
But i would suggest you not to do so. You can do nothing interesting with binary objects in database. You cannot search them. The only thing you will achieve by storing binary objects - grow the file size of your database. Mind you, Neo4J is not scalable horizontally. It does not have automatic sharding. So if your db grows too big, you are in trouble. By storing binaries in a file system or external distributed key-value store like riak, cassandra, hadoop etc, you are keeping your database small, which is good for performance, backups and avoiding horizontal scaling problems.
If you look in the API here: http://api.neo4j.org/1.2/org/neo4j/graphdb/PropertyContainer.html#setProperty(java.lang.String, java.lang.Object), you see that byte arrays are allowed.
Using byte-arrays you can store your binary objects. When you store binary objects (using Java) in Oracle, you load in the data as byte[] as well.
You can store binary objects as byte[] or encoded in a String, but I would recommend to store larger (e.g. > 1,000 bytes) blobs as separate files, and only keep a reference to the file in your database.
We do this in Structr (http://structr.org) as well.
As mentioned doing this is highly disadvantageous.
However if you decide to do so, you could do it like this in 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;
}
}
}
}
精彩评论