Overview
Hazelcast is a powerful in-memory data grid that provides distributed data structures and services, enabling the building of scalable applications. Hazelcast Predicate Functions one of its key features is its ability to perform queries on distributed data using Predicate Functions. Hazelcast predicates offer a flexible, SQL-like approach to filter and retrieve data from Hazelcast’s IMap or MultiMap structures.
In this guide, we’ll dive deep into the various Hazelcast Predicate Functions available in Hazelcast and how they can be used effectively to perform advanced queries. Whether you’re filtering simple fields or executing complex, nested conditions, Hazelcast predicates provide an efficient way to extract the data you need.
What Are Predicates in Hazelcast?
Hazelcast Predicate Functions are conditions used to filter or query data stored in distributed maps. They allow you to specify criteria that the data must meet to be retrieved. This is similar to filtering results in SQL queries using the WHERE
clause.
For instance, if you store employee data in a Hazelcast map and want to find employees over a certain age, you can use a predicate to filter the results based on the age field.
Types of Predicates in Hazelcast
In Hazelcast Predicate Functions, Hazelcast provides a range of built-in predicate methods, allowing you to perform queries based on specific conditions. Here are the most commonly used predicates:
1. SQL Predicate
The SQL Predicate allows you to use an SQL-like syntax to perform queries on Hazelcast maps. It supports field comparisons, logical operators, and even complex conditions. For more details check here.
Example:
Collection<Employee> employees = map.values(Predicates.sql("age > 30 AND department = 'Engineering'"));
This query retrieves all employees who are older than 30 and work in the Engineering department.
2. Equal Predicate
In Hazelcast Predicate Functions, the EqualPredicate is used to filter data based on the equality of a field’s value. It is equivalent to the =
operator in SQL.
Example:
Collection<Employee> employees = map.values(Predicates.equal("name", "Rahul"));
This query retrieves all entries where the name
field is Rahul.
3. Not Equal Predicate
In Hazelcast Predicate Functions, the NotEqualPredicate is the opposite of the EqualPredicate, and is used to filter out entries where a field’s value does not match a specified value.
Example:
Collection<Employee> employees = map.values(Predicates.notEqual("name", "Sachin"));
This query returns all entries where the name
is not Sachin.
4. Greater Than Predicate
The GreaterThanPredicate allows you to filter data based on whether a field’s value is greater than a specified value. It is the equivalent of the >
operator in SQL.
Example:
Collection<Employee> employees = map.values(Predicates.greaterThan("age", 30));
This query retrieves all employees older than 30.
5. Greater Than or Equal Predicate
The GreaterEqualPredicate is used to filter data based on whether a field’s value is greater than or equal to a specified value. This is equivalent to the >=
operator in SQL.
Example:
Collection<Employee> employees = map.values(Predicates.greaterEqual("age", 30));
This query retrieves all employees who are 30 years old or older.
6. Less Than Predicate
The LessThanPredicate allows you to filter data based on whether a field’s value is less than a specified value. It’s the equivalent of the <
operator in SQL.
Example:
Collection<Employee> employees = map.values(Predicates.lessThan("age", 30));
This query retrieves all employees younger than 30.
7. Less Than or Equal Predicate
The LessEqualPredicate is used to filter data where a field’s value is less than or equal to a specified value. This is the equivalent of the <=
operator in SQL.
Example:
Collection<Employee> employees = map.values(Predicates.lessEqual("age", 30));
This query retrieves all employees who are 30 years old or younger.
8. Between Predicate
In Hazelcast Predicate Functions, BetweenPredicate is useful for filtering data where a field’s value falls within a specified range.
Example:
Collection<Employee> employees = map.values(Predicates.between("age", 25, 35));
This query retrieves all employees between the ages of 25 and 35.
9. Like Predicate
The LikePredicate is used for pattern matching, similar to the LIKE
operator in SQL. It supports wildcard characters like %
for multiple characters and _
for a single character.
Example:
Collection<Employee> employees = map.values(Predicates.like("name", "Sa%"));
This query retrieves all employees whose name starts with Sa (e.g., Sachin, Saurav, Sameer).
10. In Predicate
In Hazelcast Predicate Functions, InPredicate allows you to filter data where a field’s value matches one of the values in a given list. This is equivalent to the IN
operator in SQL.
Example:
Collection<Employee> employees = map.values(Predicates.in("department", "HR", "Engineering", "Sales"));
This query retrieves all employees who work in either HR, Engineering, or Sales.
11. Instance Of Predicate
The InstanceOfPredicate allows you to filter entries based on whether the stored value is an instance of a particular class.
Example:
Collection<Manager> managers = map.values(Predicates.instanceOf(Manager.class));
This query retrieves all entries where the stored value is an instance of the Manager class.
12. Regex Predicate
The RegexPredicate allows you to filter data based on whether a field’s value matches a given regular expression.
Example:
Collection<Employee> employees = map.values(Predicates.regex("name", "[A-Za-z]*roh"));
This query retrieves all employees whose names end with roh (e.g., Rohit, Rohan).
13. True Predicate
In Hazelcast Predicate Functions, TruePredicate is a simple predicate that always evaluates to true, returning all entries from the map.
Example:
Collection<Employee> employees = map.values(Predicates.alwaysTrue());
This query retrieves all employees in the map.
14. False Predicate
The FalsePredicate always evaluates to false, returning no entries from the map.
Example:
Collection<Employee> employees = map.values(Predicates.alwaysFalse());
This query will return an empty collection.
15. And Predicate
The AndPredicate allows you to combine two or more predicates using the logical AND operator. This is equivalent to the AND
operator in SQL.
Example:
Collection<Employee> employees = map.values(Predicates.and(
Predicates.equal("department", "Engineering"),
Predicates.greaterThan("age", 25)
));
This query retrieves employees in the Engineering department who are older than 25.
16. Or Predicate
The OrPredicate allows you to combine two or more predicates using the logical OR operator. This is equivalent to the OR
operator in SQL.
Example:
Collection<Employee> employees = map.values(Predicates.or(
Predicates.equal("department", "Engineering"),
Predicates.greaterThan("age", 30)
));
This query retrieves employees who either work in Engineering or are older than 30.
17. Not Predicate
The NotPredicate inverts the result of a given predicate, retrieving entries that do not meet the specified condition.
Example:
Collection<Employee> employees = map.values(Predicates.not(Predicates.equal("department", "HR")));
This query retrieves all employees who do not work in HR.
Conclusion
In Hazelcast Predicate Functions, Hazelcast provides a rich set of predicate methods that allow developers to efficiently query and filter data from distributed maps. Whether you need to perform simple equality checks, range queries, or complex logic with multiple conditions, Hazelcast predicates offer the flexibility and performance required for real-time data querying in large-scale applications.
By leveraging these predicate methods, you can implement powerful and efficient queries that retrieve exactly the data you need from your Hazelcast data grid.