在不使用关系数据库的情况下在 Java 中存储和检索对象的简单方法?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/443558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 12:25:06  来源:igfitidea点击:

Easy way to store and retrieve objects in Java without using a relational DB?

javapersistencerelational

提问by

Do you know of an "easy" way to store and retrieve objects in Java without using a relational DB / ORM like Hibernate?

您是否知道一种在 Java 中存储和检索对象而不使用像 Hibernate 这样的关系 DB/ORM的“简单”方法?

[Note that I am not considering serialization as-is for this purpose, as it won't allow to retrieve arbitrary objects in the middle of an object graph. Neither am I considering DB4O because of its restrictive license. Thanks.]

[请注意,我不会出于此目的考虑按原样进行序列化,因为它不允许检索对象图中间的任意对象。我也不考虑 DB4O 因为它的限制性许可。谢谢。]

"Easy" meaning: not having to handle low-level details such as key/value pairs to rebuild an object graph (as with BerkeleyDB or traditional caches). The same applies for rebuilding objects from a document- or column-oriented DB (CouchDB, HBase, ..., even Lucene).

“简单”的意思是:不必处理诸如键/值对之类的低级细节来重建对象图(如使用 BerkeleyDB 或传统缓存)。这同样适用于从面向文档或面向列的 DB(CouchDB、HBase、...,甚至 Lucene)重建对象。

Perhaps there are interesting projects out there that provide a layer of integration between the mentioned storage systems and the object model (like ORM would be for RDBMSs) that I am not aware of.

也许有一些有趣的项目在提到的存储系统和我不知道的对象模型(如 ORM 将用于 RDBMS)之间提供了一个集成层。

Anyone successfully using those in production, or experimenting with persistence strategies other than relational DBs? How about RDF stores?

有没有人成功地在生产中使用它们,或者尝试使用关系数据库以外的持久性策略?RDF 商店怎么样?

Update: I came across a very interesting article: A list of distributed key-value stores

更新:我遇到了一篇非常有趣的文章:分布式键值存储列表

采纳答案by Taylor Gautier

I guess I have found a sort of answer to my question.

我想我已经找到了我的问题的答案。

Getting the document-oriented paradigm mindset is no easy task when you have always thought your data in terms of relationships, normalization and joins.

当您一直从关系、规范化和连接的角度考虑数据时,获得面向文档的范式思维模式并非易事。

CouchDBseems to fit the bill. It still could act as a key-value store but its great querying capabilities (map/reduce, view collations), concurrency readiness and language-agnostic HTTP access makes it my choice.

CouchDB似乎符合要求。它仍然可以充当键值存储,但其强大的查询功能(映射/减少、视图排序规则)、并发就绪性和与语言无关的 HTTP 访问使其成为我的选择。

Only glitch is having to correclty define and map JSON structures to objects, but I'm confident I will come up with a simple solution for usage with relational models from Java and Scala (and worry about caching later on, as contention is moved away from the database). Terracotta could still be useful but certainly not as with an RDBMS scenario.

唯一的小问题是必须正确定义 JSON 结构并将其映射到对象,但我相信我会想出一个简单的解决方案,用于 Java 和 Scala 的关系模型(并且稍后担心缓存,因为争用已从数据库)。Terracotta 仍然有用,但肯定不像 RDBMS 场景那样有用。

Thank you all for your input.

谢谢大家的意见。

回答by Matt Campbell

  • 对象序列化(也就是将东西存储到文件中)
  • Hibernate(使用关系数据库,但它对开发人员相当透明)

I would suggest Hibernate because it will deal with most of the ugly details that bog developers down when using a database while still allowing for the optimizations that have been made to database software over the years.

我会推荐 Hibernate,因为它可以处理大多数让开发人员在使用数据库时陷入困境的丑陋细节,同时仍然允许多年来对数据库软件进行的优化。

回答by willcodejavaforfood

I would like to recommend XStreamwhich simply takes your POJOs and creates XML out of them so you can store it on disk. It is very easy to use and is also open source.

我想推荐XStream,它只是获取您的 POJO 并从中创建 XML,以便您可以将其存储在磁盘上。它非常易于使用,也是开源的。

回答by Steve K

NeoDatislooks interesting. It is licensed under the LGPL, so not quite as restrictive as the GLP proper.

NeoDatis看起来很有趣。它是根据 LGPL 获得许可的,因此不像 GLP 那样严格。

Check out their 1 minute tutorialto see if it will work for your needs.

查看他们的1 分钟教程,看看它是否适合您的需求。

回答by orip

I still think you should consider paying for db4o.

我仍然认为您应该考虑为db4o付费。

If you want something else, add "with an MIT-style license" to the title.

如果您想要其他内容,请在标题中添加“具有 MIT 风格的许可证”。

回答by Kai Huppmann

I'd recommend Hibernate (or, more general, OR-mapping) like Matt, but there is also a RDBMS at the backend and I'm not so sure about what you mean by

我会推荐像 Matt 一样的 Hibernate(或者更一般的 OR-mapping),但是后端也有一个 RDBMS,我不太确定你的意思

...without using a relational DB?...

...不使用关系数据库?...

It also would be interesting to know more about the application, because OR-mapping is not always a good idea (development performance vs. runtime performance).

了解更多关于应用程序的信息也会很有趣,因为 OR 映射并不总是一个好主意(开发性能与运行时性能)。

Edit: I shortly learned about terracotta and there is a good stackoverflow discussion hereabout replacing DBs with that tool. Still experimental, but worth reading.

编辑:我很快了解了兵马俑,有一个很好的计算器讨论在这里关于与刀具替换数据块。仍在实验中,但值得一读。

回答by Steve B.

Check out comments on Prevayler on this question. Prevayler is a transactional wrapper around object serialization - roughly, use objects in plain java and persist to disk through java API w/o sql, a bit neater than writing your own serialization.

查看 Prevayler对此问题的评论。Prevayler 是一个围绕对象序列化的事务包装器 - 粗略地说,使用纯 java 中的对象并通过 java API w/o sql 持久化到磁盘,比编写自己的序列化更简洁。

Caveats- with serialization as a persistance mechanism, you run the risk of invalidating your saved data when you update the class. Even with a wrapper library you'll probably want to customize the serialization/deserialization handling. It also helps to include the serialVersionUID in the class so you override the JVM's idea of when the class is updated (and therefore can't reload your saved serialized data).

警告 - 将序列化作为一种​​持久性机制,当您更新类时,您可能会冒着使保存的数据无效的风险。即使使用包装库,您也可能希望自定义序列化/反序列化处理。它还有助于在类中包含 serialVersionUID,以便您覆盖 JVM 的类更新时间的想法(因此无法重新加载保存的序列化数据)。

回答by Taylor Gautier

Terracotta provides a highly available, highly scalable persistent to disk object store. You can use it for just this feature alone - or you can use it's breadth of features to implement a fully clustered application - your choice.

Terracotta 提供了一个高度可用、高度可扩展的持久磁盘对象存储。您可以仅将它用于此功能 - 或者您可以使用它的广泛功能来实现一个完全集群的应用程序 - 您的选择。

Terracotta:

红陶:

  • does not break object identity giving you the most natural programming interface
  • does not require Serialization
  • clusters (and persists) nearly all Java classes (Maps, Locks, Queues, FutureTask, CyclicBarrier, and more)
  • persists objects to disk at memory speeds
  • moves only object deltas, giving very high performance
  • 不会破坏对象身份,为您提供最自然的编程界面
  • 不需要序列化
  • 集群(并持久化)几乎所有的 Java 类(Maps、Locks、Queues、FutureTask、CyclicBarrier 等)
  • 以内存速度将对象持久化到磁盘
  • 仅移动对象增量,提供非常高的性能

Here's a case study about how gnipuses Terracotta for in-memory persistence - no database. Gnip takes in all of the events on Facebook, Twitter, and the like and produces them for consumers in a normalized fashion. Their current solution is processing in excess of 50,000 messages / second.

这是一个关于 gnip 如何使用 Terracotta 进行内存持久性的案例研究- 没有数据库。Gnip 接收 Facebook、Twitter 等上的所有事件,并以规范化的方式为消费者制作它们。他们目前的解决方案是每秒处理超过 50,000 条消息。

It's OSS and has a high degree of integration with many other 3rd party frameworks including Spring and Hibernate.

它是 OSS,并且与许多其他 3rd 方框架(包括 Spring 和 Hibernate)高度集成。

回答by bethlakshmi

Hmm... without serialization, and without an ORM solution, I would fall back to some sort of XML based implementation? You'd still have to design it carefully if you want to pull out only some of the objects from the object graph - perhaps a different file for each object, where object relationships are referenced by a URI to another file?

嗯...没有序列化,没有 ORM 解决方案,我会退回到某种基于 XML 的实现?如果您只想从对象图中提取一些对象,您仍然必须仔细设计它 - 也许每个对象都有一个不同的文件,其中对象关系由 URI 引用到另一个文件?

I would have said that wasn't "easy" because I've always found designing the mapping of XML to objects to be somewhat time consuming, but I was really inspired by a conversation on Apache Betwixt that has me feeling hopeful that I'm just out of date, and easier solutions are now available.

我会说这并不“容易”,因为我一直发现设计 XML 到对象的映射有点耗时,但我真的受到 Apache Betwixt 上的一次谈话的启发,这让我感到充满希望刚刚过时,现在可以使用更简单的解决方案。