简体   繁体   English

在Java EE 6应用程序(数据库/文件系统)中存储大量文件的最佳方法

[英]Best way to store large amount of files in Java EE 6 Application (Database/Filesystem)

I'm developing an application that needs to deal with several document files (PDFs, DOCs, etc.). 我正在开发一个需要处理多个文档文件(PDF,DOC等)的应用程序。

I'm using PostgresSQL 9.2 to store these files as large objects and Hibernate Blob to write and read them as stream in a way to not overload the memory. 我正在使用PostgresSQL 9.2将这些文件存储为大对象,并使用Hibernate Blob以流的形式写入和读取它们,以免使内存过载。

My question is if there is a best way to deal with it? 我的问题是,是否有最佳的应对方法? Or store a large ammount of binary files in database is a good approach? 还是在数据库中存储大量的二进制文件是一个好方法? Maybe store the files in the filesystem is faster, but that not guarantee the integrity of the data neither the scalability. 也许将文件存储在文件系统中的速度更快,但这不能保证数据的完整性,也不能保证可伸缩性。

It all depends on your need and use cases. 这完全取决于您的需求和用例。 At least there is nothing wrong to store them as blobs in the db. 至少没有任何错误将它们存储为db。 If you already have your database and store other relevant data with it and also need them in an consistent way, then your approach may be the only good one. 如果您已经拥有数据库并存储其他相关数据,并且还需要以一致的方式使用它们,那么您的方法可能是唯一的好方法。

Of course, if you have some requirements which the database cannot fulfill or when you think it's overkill to use a db because you have no requirements which a database normally fulfills, then you may think of other approaches. 当然,如果您有一些数据库无法满足的要求,或者由于没有数据库通常可以满足的要求而认为使用db是过分的,那么您可能会想到其他方法。

Storing in the filesystem has some disadvantages as you mentioned. 如前所述,在文件系统中存储有一些缺点。 But if you have some restrictions, like the same name is not used twice, and only one instance is writing the same file, no need for transaction/rollback etc. the filesystem approach wins on the aspect of simplicity. 但是,如果您有一些限制,例如相同的名称不会被使用两次,并且只有一个实例正在写入相同的文件,则无需进行事务/回滚等操作。文件系统方法在简单性方面胜出。

For example if you need to scale, because you store so much data that one machine is not enough to store them, you may want to consider NOSQL technologies to scale the storage. 例如,如果您需要扩展,因为存储的数据太多,一台机器不足以存储它们,则可能需要考虑使用NOSQL技术来扩展存储。

Your question is so open, that I could write forever of several options. 您的问题是如此开放,我可以永远写几个选择。

Depending on what type of data you are trying to store sometimes hibernate isn't really that useful, to me this sounds to me like it may be one of the cases. 根据您尝试存储的数据类型的不同,有时休眠实际上并没有那么有用,对我来说,这听起来像是其中一种情况。 Pulling a flat file into a Java object probably isn't going to buy you that much in the end. 最后,将平面文件拉入Java对象可能不会给您带来太多好处。 You may want to think about storing your files in a document-oriented database like MongoDB instead. 您可能需要考虑将文件存储在面向文档的数据库(如MongoDB)中。 What you are doing is just what these type of databases were made for. 您正在做的正是这些类型的数据库的用途。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM