Hazelcast and MongoDB Integration

Source: Internet
Author: User
Tags imap install mongodb mongoclient hazelcast

Hazelcast and MongoDB integrated Chszs, not allowed to reprint without the Bo master. Permitted reprint should be marked by author and blog homepage: Http://blog.csdn.net/chszs, Hazelcast and MongoDB

Hazelcast's distributed data structure and computational performance make it a "MongoDB" for application backend.

MongoDB is an open-source, document-oriented database with great extensibility and flexibility. MongoDB is not storing data in tables and rows (relational databases), but in a way that resembles JSON documents and is stored in dynamic mode. In short, MongoDB is a NoSQL data store that mainly involves the storage and persistence of data and the retrieval of modeless data.

Hazelcast is an open source, distributed, highly available, extensible, memory data grid store that is published based on Apache V2 licenses and can be used as a cache, message broker, and distributed computing platform. Hazelcast emphasizes high-speed access to distributed data (usually distributed cache), distributed computing, and distributed messaging.

You can view this document: Https://hazelcast.com/use-cases/nosql/nosql-data-store/

Hazelcast can be used as a nosql storage solution. MongoDB has some functions of data grid and grid computing, but MongoDB is not optimized in this respect. As a result, hazelcast and MongoDB do feature comparisons in this regard, somewhat similar to apples and oranges.

Hazelcast and MongoDB can be combined to work together rather than compete with each other. Hazelcast supports the use of MongoDB as a back-end data store. It is easy to map hazelcast data to MongoDB, whether it is data direct or deferred write support.

Let's review the features of Hazelcast and MongoDB to see how they work with each other.

Ii. characteristics 1, Simplicity

Both the Hazelcast and MongoDB technologies are simple to run, allowing MongoDB to be fully booted within minutes. For example, on Mac OS x systems, you can use brew to install MongoDB.

brew mongo install

Hazelcast is also easy to start.

2. Advantages to Java Developers

Hazelcast and MongoDB are ideal for developing Java applications. For Hazelcast, it is possible to use Java objects directly in the cluster without worrying about the data transfer layer, so the development work is much simpler. Using MONGODB requires both the use of MONGODB data structures and the need to write and configure data transfer layers.

The Bson library fully supports the Bson data format, data storage format, and network Transport layer format, and MongoDB uses Bson as the "document" for storage. Bson is the abbreviation for binary JSON, which is a binary encoding of JSON data serialization.

The MongoDB official web has a Java driver package, and this driver package is a Java Object Document mapping framework that allows you to map MongoDB documents to and from Java objects in two-way.

In terms of deploying and integrating Java applications, Hazelcast can bring low-latency data access features (through various mechanisms) to applications, particularly the Hazelcast client's proximity caches and hazelcast members of embedded deployments. For MongoDB, network latency is present because it does not have a local memory cache.

3. Distributed computing

Hazelcast's distributed computing framework is extremely powerful, allowing arbitrary business logic to perform location references and supporting distributed extensions across clusters. MongoDB supports a single-threaded mapreduce framework, but does not support arbitrary user code execution.

Hazelcast There are many features that MongoDB does not have in distributed computing, such as distributed concurrency tools: locks, semaphores, queues, etc., which can be distributed to multiple nodes in parallel, which is difficult to achieve locally. I know that many people use MongoDB as their own implementation of the message agent, but it is hard to imagine how to use MongoDB only to achieve real parallelism.

4. Persistence

Hazelcast is primarily to address the low latency of accessing distributed data and for distributed computing. By default, Hazelcast does not involve disks or other persisted storage. Hazelcast is not a database. And MongoDB is a real persistent database (of course, MongoDB persistence is a bit problematic, because it writes memory, not synchronous to disk.) )

Let's take a look at the benefits of persisting Hazelcast data to MongoDB:

1) IMAP and Mapstore

The hazelcast read/write function is two interfaces: Maploader and Mapstore. If you simply read the data from the database, the developer only needs to implement the Maploader interface.

Maploader interface

public interface MapLoader<K, V> {
    V load(K key); (1)
    Map<K, V> loadAll(Collection<K> keys); (2)
    Iterable<K> loadAllKeys(); (3)
    • 1) method One is to get the value of the given key name. If the distributed map does not contain the value of the given key name, then Hazelcast calls the implementation of the load (key) method to get the value.
    • 2) method Two is to get all the key values corresponding to the key name collection. This is a batch read operation that is optimized for multiple values that read a given key name.
    • 3) method Three is to get all the key names of the store.

The Mapstore interface inherits the Maploader interface and allows the storage of IMAP entries to the database.

Mapstore interface

public interface MapStore<K, V> extends MapLoader<K, V> {
    void store(K key, V value); (1)
    void storeAll(Map<K, V> map); (2)
    void delete(K key); (3)
    void deleteAll(Collection<K> keys); (4)
    • 1) Store key value pairs
    • 2) store multiple entries. The implementation of this method optimizes the operation of multiple storage key-value pairs
    • 3) Delete the entry for the given key name
    • 4) Delete multiple entries for a given set of key names

To learn more about the Maploader and Mapstore interfaces, view the documentation: Http://docs.hazelcast.org/docs/3.5/manual/html-single/index.html#map-persistence

To interact with MongoDB, use the Mongo-java-driver driver package.

The Maven dependency configuration is as follows:


MongoClient mongoClient = new MongoClient(new MongoClientURI(mongoUrl)); (1)
MongoCollection collection = mongoClient.getDatabase(dbName).getCollection(collectionName); (2)
final Document document = (Document) collection.find(eq("_id", key)).first(); (3)
collection.insertOne(document); (3)
    • 1) Establish a connection to a MongoDB instance using a given URI (such as mongodb://localhost:27017)
    • 2) The Mongoclient class provides a way to connect to a MongoDB instance, access a database, access a collection, access a document
    • 3) The Mongocollection class allows for the deletion of documents in the collection

MongoDB combines hazelcast to provide low-latency access to distributed, modeless data solutions. If you're looking for a NoSQL data storage solution, MongoDB is a great fit. Hazelcast's FENBUSHII data structure and distributed computing power are not available in MongoDB.

Hazelcast and MongoDB Integration

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.