Integration of Hazelcast and MongoDB
I. Hazelcast and MongoDB
Hazelcast's Distributed Data Structure and computing performance enable it to serve as the "MongoDB" at the application backend ".
MongoDB is an open-source, document-oriented database with excellent scalability and flexibility. MongoDB does not store data into (Relational Database) tables and rows, but is similar to JSON files and is stored in dynamic mode. In short, MongoDB is a NoSQL data storage that mainly involves data storage and persistence and retrieval of Schema-less data.
Hazelcast is an open-source, distributed, highly available, and scalable grid storage of memory data. It is released based on the Apache v2 license and can be used as a cache, message proxy, and distributed computing platform. Hazelcast emphasizes high-speed access to distributed data (usually distributed cache), distributed computing, and distributed messages.
See this document: https://hazelcast.com/use-cases/nosql/nosql-data-store/
Hazelcast can be used as a NoSQL storage solution. MongoDB has some data grid and grid computing functions, but MongoDB has not been optimized in this respect. Therefore, the Functional Comparison Between Hazelcast and MongoDB is similar to Apple and orange.
Hazelcast and MongoDB can work together rather than compete with each other. Hazelcast supports using MongoDB as its back-end data storage. It is easy to map Hazelcast data to MongoDB. Both Data Writing and delayed writing are supported.
Next we will review the features of Hazelcast and MongoDB to see how they work together.
Ii. Features 1. Simplicity
Hazelcast and MongoDB are both easy to run. We can enable MongoDB to be fully started in just a few minutes. For example, on Mac OS X, you can use brew to install MongoDB.
brew mongo install
Hazelcast is also easy to start.
2. Advantages for Java developers
Hazelcast and MongoDB are very suitable for developing Java applications. For Hazelcast, Java objects can be directly used in clusters without worrying about the data transmission layer. This simplifies development. MongoDB requires both the data structure and the data transmission layer to be written and configured.
The BSON database fully supports the BSON data format, data storage format, and network transmission layer format. MongoDB uses BSON as the "document" for storage ". BSON is short for Binary JSON and is the binary encoding of JSON data serialization.
The MongoDB official website has a Java driver package, which is a Java object document ing framework that provides bidirectional ing between MongoDB documents and Java objects.
For the deployment and integration of Java applications, Hazelcast can provide applications with low-latency data access features (through various mechanisms), especially the nearest cache of Hazelcast clients and embedded deployment of Hazelcast members. For MongodB, network latency exists because it does not have local memory cache.
3. Distributed Computing
Hazelcast's distributed computing framework is extremely powerful. It allows reference of any business logic execution location and supports distributed extension across clusters. MongoDB supports a single-threaded MapReduce framework, but does not support arbitrary user code execution.
Hazelcast also has many features that are not available in MongoDB in distributed computing, such as Distributed Concurrency tools: locks, semaphores, and queues. It can coordinate and distribute tasks to multiple nodes for parallel work, however, it is difficult to implement these features locally. I know that many people use MongoDB as their own message proxy, but it is hard to imagine how to use MongoDB only to achieve real parallelism.
4. Persistence
Hazelcast mainly solves the problem of low latency when accessing distributed data and performing distributed computing. By default, Hazelcast does not involve disks or other persistent storage. Hazelcast is not a database. MongoDB is a real Persistent Database (of course, there is also a problem with MongoDB's persistence, because its write operations are writing memory, not synchronously writing data to the disk .)
Let's take a look at the benefits of persisting Hazelcast data to MongoDB:
1) IMap and MapStore
The read/write operations of Hazelcast are MapLoader and MapStore. If you only need to read data from the database, developers only need 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 1: obtain the value of the given key name. If the distributed Map does not contain the value of the given key name, Hazelcast calls the load (key) method to obtain the value.
- 2) Method 2: obtain all the key values corresponding to the key name set. This is a batch read operation that optimizes the values of multiple read given key names.
- 3) method 3: obtain all the key names in the bucket.
The MapStore interface inherits the MapLoader interface and allows you to save 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 can optimize the operations of multiple storage key-value pairs
- 3) delete an entry with a given key name
- 4) Delete multiple entries in the given key name set
To learn more about MapLoader and MapStore interfaces, see: http://docs.hazelcast.org/docs/3.5/manual/html-single/index.html#map-persistence
To interact with MongoDB, use the mongo-java-driver package.
Maven dependency configuration is as follows:
<dependency>
<groupId>org.mongodb</groupId>
<artifactId>mongo-java-driver</artifactId>
<version>${mongo-java-driver.version}</version>
</dependency>
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) Use the given URI (such as mongodb: // localhost: 27017) to establish a connection to the MongoDB instance
- 2) The MongoClient class provides methods to connect to MongoDB instances, access databases, access sets, and access documents.
- 3) the document collection class allows you to add, delete, modify, and query documents in the set.
Summary
MongoDB works with Hazelcast to provide a solution for low-latency distributed and non-pattern data access. If you are looking for a NoSQL data storage solution, MongoDB is very suitable. Hazelcast's fenbushii data structure and distributed computing capabilities are not available in MongoDB.
For more MongoDB tutorials, see the following:
CentOS compilation and installation of php extensions for MongoDB and mongoDB
CentOS 6 install MongoDB and server configuration using yum
Install MongoDB2.4.3 in Ubuntu 13.04
MongoDB beginners must read (both concepts and practices)
MongoDB Installation Guide for Ubunu 14.04
MongoDB authoritative Guide (The Definitive Guide) in English [PDF]
Nagios monitoring MongoDB sharded cluster service practice
Build MongoDB Service Based on CentOS 6.5 Operating System
MongoDB details: click here
MongoDB: click here
This article permanently updates the link address: