Hazelcast - query collections of Map values
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.
Hazelcast IMDG (In-Memory Data Grid) offers a unique approach to handling large data sets in distributed systems by providing a highly available and scalable distributed data structure. Among its versatile data structures, the Hazelcast IMap or distributed map is widely used for storing large sets of data across a cluster of computers. But what stands out in Hazelcast is not just its capability to store massive amounts of data but also its querying functionality that enables developers to handle data efficiently.
Understanding Hazelcast Map Queries
Hazelcast supports various query mechanisms to retrieve data from a map according to specific criteria. This is particularly useful when dealing with large datasets where retrieving the whole dataset to find a small subset is inefficient. Queries in Hazelcast are based on predicates which specify criteria to filter the map entries.
Predicate-based Queries
Hazelcast provides a rich set of predicate implementations to facilitate advanced query capabilities on map entries. These predicates can be combined to form complex query logic. Here are key predicates provided by Hazelcast:
- Equal Predicate: Checks if the value of a specified field is equal to the specified value.
- Like Predicate: For string fields, checks if the field value matches the given pattern (SQL LIKE).
- Greater/Less Than Predicate: Checks if a numeric or comparable field value is greater or less than the specified value.
- Between Predicate: Checks if a comparable field's value is between two specified values.
- And/Or Predicate: Logical AND/OR between two or more other predicates.
- Not Predicate: Inverts the result of another predicate.
- Regex Predicate: Uses regular expression to filter entries based on the pattern matching on the field.
These predicates can be used to retrieve map values without retrieving the entire data set from the cluster, thus optimizing the network usage and computational cost.
SQL Predicate
Hazelcast also provides an SQL-like querying capability through the SqlPredicate. This allows queries to be formed as SQL query strings, making it simple for developers familiar with SQL to use Hazelcast. For example:
This example demonstrates how to fetch all employees whose age is greater than 40 using a SQL-like string inside the SqlPredicate.
Continuous Query with Listeners
Beyond simple data fetching, Hazelcast allows registering listeners to map entries based on query results. This means applications can react to data changes that meet a specific criterion. This feature is known as Continuous Query Caching (CQC).
Here's how you could use it:
This code adds an entry listener that triggers every time a product with a price less than 100 is added to the map.
Performance Considerations
Query performance in Hazelcast is directly related to the size of the dataset and the complexity of the query. Using indexes can significantly increase query performance by avoiding full scans of values. Setting up an index is straightforward:
Here, an index is added on the age field allowing Hazelcast to quickly access the data sorted or filtered by age, which enhances the performance of age-related queries.
Summary Table
| Feature | Description |
| Predicate-based Queries | Allows complex logical, comparison operations on fields. |
| SQL Predicate | Offers SQL-like query strings for ease of use. |
| Continuous Query Caching | Notifies changes in real-time based on query results. |
| Indexing Support | Enhances query performance by avoiding full data scans. |
| Data Structure | Uses IMap for distributed storage and retrieval. |
Conclusion
Querying in Hazelcast is both flexible and powerful, allowing for efficient data retrieval and real-time data handling in a distributed environment. With its combination of SQL-like syntax, continuous query capabilities, and the ability to add indexes, Hazelcast stands out as a prime solution for managing large-scale in-memory data effectively. Whether you are handling simple queries or need robust real-time data handling, Hazelcast provides the tools necessary to meet various application requirements.
Related reading
- Heap or Red-Black Tree?
- Heap vs Binary Search Tree BST
- heapq with custom compare predicate
- Height of a binary tree
- helm error when updating UPGRADE FAILED The order in patch list
- Help me understand Inorder Traversal without using recursion
- Help Understanding Cross Validation and Decision Trees
- Help Understanding Cross Validation and Decision Trees

DSA Fundamentals
Master algorithmic patterns and data structures through hands-on LeetCode-style problems - from arrays and hashing to dynamic programming and advanced graphs.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Data Structures & Algorithms practice on Codemia
Step through 300 algorithm problems with animated visualisers that show the data structure changing as the code runs.