DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
Securing Your Software Supply Chain with JFrog and Azure
Register Today

Trending

  • How To Approach Java, Databases, and SQL [Video]
  • Writing a Vector Database in a Week in Rust
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Comparing Cloud Hosting vs. Self Hosting

Trending

  • How To Approach Java, Databases, and SQL [Video]
  • Writing a Vector Database in a Week in Rust
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Comparing Cloud Hosting vs. Self Hosting
  1. DZone
  2. Data Engineering
  3. Databases
  4. Save Large Files in MongoDB Using Kundera

Save Large Files in MongoDB Using Kundera

Kundera can save some time when it comes to saving big files in MongoDB. This tutorial shows you how it's done.

Devender Yadav user avatar by
Devender Yadav
·
Jul. 24, 16 · Tutorial
Like (2)
Save
Tweet
Share
8.97K Views

Join the DZone community and get the full member experience.

Join For Free

Kundera is a "Polyglot Object Mapper" with a JPA interface. Kundera currently supports Cassandra, MongoDB, HBase, Redis, OracleNoSQL, Neo4j, CouchDB, Kudu, Relational databases, and Apache Spark.

New folks should read Getting Started in 5 minutes and Working with MongoDB using Kundera.

Storing Large Files in MongoDB

For storing and retrieving files that exceed the BSON-document size limit of 16MB, MongoDB provides GridFS.

Instead of storing a file in a single document, GridFS divides a file into chunks and stores each of those chunks as a separate document. By default, GridFS limits chunk sizes to 255k. GridFS uses two collections to store files. One collection stores the file chunks, and the other stores file metadata.

GridFS with Kundera

Kundera allows the user to perform CRUD and Query operation on large files in MongoDB using GridFS. All a user needs to add is an @javax.persistence.Lob annotation in the field you want to insert using GridFS.

Sample Entity:

@Entity
public class Book
{
    @Id
    private String id;

    @Column
    private String title;

    @Lob
    private byte[] pdfFile;

    //setters and getters
}


Mapping

@Lob Field ==> GridFSInputFile.

Other Fields ==> GridFSInputFile.metadata.

Consider the Book Entity mentioned above. GridFSInputFile is created using pdfFile(@Lob field). Other fields (id, title) are saved in the metadata of GridFSInputFile.

CRUD and Querying Operation

Refer to this test case.

Limitations

  • CRUD on simple entities (without relationships, inheritance, embeddable, etc.) is possible.
  • Only select queries are allowed with WHERE and ORDER BY clauses.
  • Lob field can only be byte[].

Conclusion

Kundera, being JPA compliant, makes it easier to work with NoSQL databases. Irrespective of the database used, the user needs to write the same JPA query.  Also, if a user wants to switch databases (say from HBase to MongoDB), there is no need to rewrite the code — just change some configurations in the persistence.xml file. 

Database MongoDB Relational database

Opinions expressed by DZone contributors are their own.

Trending

  • How To Approach Java, Databases, and SQL [Video]
  • Writing a Vector Database in a Week in Rust
  • RBAC With API Gateway and Open Policy Agent (OPA)
  • Comparing Cloud Hosting vs. Self Hosting

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: