SQL
The SQL service provided by Hazelcast allows you to query data stored in IMap declaratively.
| The SQL feature is currently in beta. The compatibility between versions is not guaranteed. API might change between versions without notice. |
If you’re using the hazelcast-all or hazelcast-enterprise-all packages, the hazelcast-sql
module is included in them by default. If not, i.e., you are using hazelcast or hazelcast-enterprise,
then you need to have hazelcast-sql in the classpath.
|
Example: How to Query an IMap using SQL
Consider that we have a map called emp that contains values of type Employee:
public class Employee implements Serializable {
private String name;
private int age;
public Employee(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
The following code prints names of the employees whose age is less than 30:
try (SqlResult result = hazelcastInstance.getSql().execute("SELECT name FROM emp WHERE age < ?", 30)) {
for (SqlRow row : result) {
String name = row.getObject(0);
System.out.println(name);
}
}
You can run the same code snippet from a member or a client.