在 Oracle 中将对象序列化为 BLOB

发布于 2024-07-11 04:00:55 字数 293 浏览 4 评论 0原文

我有一个 HashMap,正在将其序列化和反序列化到 Oracle 数据库的 BLOB 数据类型字段中。 我想使用该字段执行查询。 例如,应用程序将创建一个新的 HashMap,并具有一些键值对。 我想查询数据库以查看数据库中是否已存在包含此数据的 HashMap。 我不知道该怎么做,如果我必须转到数据库中的每条记录,将其反序列化,然后进行比较,这似乎很奇怪,SQL 是否处理比较 BLOB,所以我可以...从 PROCESSES where foo = 中选择 * ?....并且 foo 是 BLOB 类型,而 ? 是新的HashMap的实例吗? 谢谢

I have a HashMap that I am serializing and deserializing to an Oracle db, in a BLOB data type field.
I want to perform a query, using this field.
Example, the application will make a new HashMap, and have some key-value pairs.
I want to query the db to see if a HashMap with this data already exists in the db.
I do not know how to do this, it seems strange if i have to go to every record in the db, deserialize it, then compare, Does SQL handle comparing BLOBs, so i could have...select * from PROCESSES where foo = ?....and foo is a BLOB type, and the ? is an instance of the new HashMap?
Thanks

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

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

发布评论

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

评论(7

回眸一笑 2024-07-18 04:00:55

这里有一篇文章供您阅读: 敲钉子:旧鞋或 Glass Bottle

我对您的应用程序的底层架构了解不多,但我可以立即告诉您,您没有理由需要以这种方式使用 HashMap。 这是一种糟糕的技术,简单明了。

您问题的答案不是聪明的 Oracle 查询,而是对应用程序架构的重新设计。

首先,您不应该将 HashMap 序列化到数据库(更一般地说,您不应该序列化需要查询的任何内容)。 创建一个表来表示应用程序中的哈希图要容易得多,如下所示:

HashMaps
--------
MapID (pk int)
Key   (pk varchar)
Value

一旦数据库中包含哈希图的内容,查询数据库以查看数据是否已存在或生成任何其他类型的聚合数据就很简单:

SELECT Count(*) FROM HashMaps where MapID = ? AND Key = ?

Here's an article for you to read: Pounding a Nail: Old Shoe or Glass Bottle

I haven't heard much about your application's underlying architecture, but I can tell you immediately that there is never a reason why you should need to use a HashMap in this way. Its a bad technique, plain and simple.

The answer to your question is not a clever Oracle query, its a redesign of your application's architecture.

For a start, you should not serialize a HashMap to a database (more generally, you shouldn't serialize anything that you need to query against). Its much easier to create a table to represent hashmaps in your application as follows:

HashMaps
--------
MapID (pk int)
Key   (pk varchar)
Value

Once you have the content of your hashmaps in your database, its trivial to query the database to see if the data already exists or produce any other kind of aggregate data:

SELECT Count(*) FROM HashMaps where MapID = ? AND Key = ?
时光倒影 2024-07-18 04:00:55

在数据库中存储序列化对象几乎总是一个坏主意,除非您提前知道不需要查询它们。

你如何序列化 HashMap? 有很多方法可以序列化数据和对象(例如 HashMap)。 比较两个映射(尤其是序列化形式的映射)并非易事,除非您的序列化技术保证两个等效映射始终以相同的方式序列化。

解决这个混乱问题的一种方法是对一些很少需要查询的对象使用 XML 序列化。 例如,我工作的地方有一个日志表,其中某个日志消息作为 XML 文件存储在 CLOB 字段中。 该 xml 数据表示序列化的 Java 对象。 通常,我们查询记录中的其他列,并且仅以单个原子步骤读取/写入 blob。 然而,有一两次需要对 blob 进行一些深度检查,而使用 XML 可以实现这种情况(Oracle 支持在 varchar2 或 CLOB 字段以及本机 XML 对象中查询 XML)。 如果谨慎使用,这是一种有用的技术。

Storing serialized objects in a database is almost always a bad idea, unless you know ahead of time that you don't need to query against them.

How are you serializing the HashMap? There are lots of ways to serialize data and an object like a HashMap. Comparing two maps, especially in serialized form, is not trivial, unless your serialization technique guarantees that two equivalent maps always serialize the same way.

One way you can get around this mess is to use XML serialization for some objects that rarely need to be queried. For example, where I work we have a log table where a certain log message is stored as an XML file in a CLOB field. This xml data represents a serialized Java object. Normally we query against other columns in the record, and only read/write the blob in single atomic steps. However once or twice it was necessary to do some deep inspection of the blob, and using XML allowed this to happen (Oracle supports querying XML in varchar2 or CLOB fields as well as native XML objects). It's a useful technique if used sparingly.

魔法少女 2024-07-18 04:00:55

查看 dbms_crypto.hash 以生成 blob 的哈希值。 将哈希值与 blob 一起存储,它会给您一些东西来将搜索范围缩小到易于管理的范围。 我不建议存储哈希映射,但这是搜索 blob 之间精确匹配的通用技术。
另请参阅SQL - 如何比较 CLOB

Look into dbms_crypto.hash to make a hash of your blob. Store the hash alongside the blob and it will give you something to narrow down the search to something manageable. I'm not recommending storing the hash map, but this is a general technique for searching for an exact match between blobs.
See also SQL - How do you compare a CLOB

深海少女心 2024-07-18 04:00:55

我不能不同意,但我被告知要这样做。
我很欣赏你的解决方案,这就是我以前的解决方案。
谢谢

i cannot disagree, but i'm being told to do so.
i appreciate your solution, and that's sort of what i had previously.
thanks

拥抱没勇气 2024-07-18 04:00:55

我不需要比较 BLOB,但它似乎是通过 dbms_lob 包支持的。

请参阅 http://www.psoug 处的 dbms_lob.compare()。 org/reference/dbms_lob.html

I haven't had the need to compare BLOBs, but it appears that it's supported through the dbms_lob package.

See dbms_lob.compare() at http://www.psoug.org/reference/dbms_lob.html

狼亦尘 2024-07-18 04:00:55

Oracle 可以使用 Java(或 Windows 上的 .net)定义新的数据类型,您可以为序列化对象定义数据类型并定义查询如何处理它。

很好的缺乏,如果你尝试这个......

Oracle can have new data types defined with Java (or .net on windows) you could define a data type for your serialized object and define how queries work on it.

Good lack if you try this...

帅冕 2024-07-18 04:00:55

如果将数据序列化为 xml,并将数据存储在 xml 中,则可以在 sql 查询中使用 xpath。 (抱歉,因为我更喜欢 SqlServer,所以我不知道如何在 Oracle 中执行此操作的详细信息。)

  • 如果您每次只需要更新部分序列化数据,请不要这样做。
  • 同样,如果任何数据被其他数据指向或指向其他数据,则不要执行此操作。

If you serialize your data to xml, and store the data in an xml you can then use xpaths within your sql query. (Sorry as I am more of a SqlServer person, I don’t know the details of how to do this in Oracle.)

  • If you EVERY need to update only part of the serialized data don’t do this.
  • Likewise if any of the data is pointed to by other data or points to other data don’t do this.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文