在 Neo4j 中存储二进制对象

发布于 2024-11-01 22:25:09 字数 73 浏览 0 评论 0原文

Neo4j 似乎不允许我存储二进制对象。这是否意味着我必须将 Neo4j 与其他数据存储(例如文件系统、Oracle 等)结合使用?

Neo4j doesn't seem to allow me to store binary objects. Does this mean I must used Neo4j in conjuntion with another data store, such as the filesystem., Oracle, etc?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧时光的容颜 2024-11-08 22:25:09

Daniel 已经回答说可以在 Neo4J 中存储二进制对象。

但我建议你不要这样做。您无法对数据库中的二进制对象做任何有趣的事情。你无法搜索它们。通过存储二进制对象,您将实现的唯一一件事是增加数据库的文件大小。请注意,Neo4J 不可水平扩展。它没有自动分片。因此,如果您的数据库变得太大,您就会遇到麻烦。通过将二进制文件存储在文件系统或外部分布式键值存储(如 riak、cassandra、hadoop 等)中,您可以保持数据库较小,这有利于性能、备份并避免水平扩展问题。

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.

离鸿 2024-11-08 22:25:09

如果您查看此处的 API: http ://api.neo4j.org/1.2/org/neo4j/graphdb/PropertyContainer.html#setProperty(java.lang.String, java.lang.Object),您看到允许

使用 字节数组。 -数组,您可以存储二进制对象。当您在 Oracle 中存储二进制对象(使用 Java)时,您也可以将数据加载为 byte[]。

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.

您可以将二进制对象存储为 byte[] 或编码为字符串,但我建议将较大(例如 > 1,000 字节)的 blob 存储为单独的文件,并且仅在数据库中保留对该文件的引用。

我们也在 Structr (http://structr.org) 中执行此操作。

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.

夕嗳→ 2024-11-08 22:25:09

如前所述,这样做是非常不利的。

但是,如果您决定这样做,可以在 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;
            }
        }
    }    
}

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;
            }
        }
    }    
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文