Interface SqlService
In order to use the service, Jet engine must be enabled - SQL statements are executed as
Jet jobs. On members, the hazelcast-sql.jar
must be on the classpath, otherwise
an exception will be thrown; on client, it is not necessary.
Overview
Hazelcast is currently able to execute distributed SQL queries using the following connectors:- IMap
- Kafka
- Files
hazelcast-sql
module,
that is based on Apache Calcite. During optimization a statement is
converted into a directed acyclic graph (DAG) that is sent to cluster members for execution. Results are sent
back to the originating member asynchronously and returned to the user via SqlResult
.
SQL statements are not atomic. INSERT/SINK can fail and commit part of the data.
Usage
Before you can access any object using SQL, a mapping has to be created. See the reference manual for the CREATE MAPPING command.
When a query is executed, an SqlResult
is returned. You may get row iterator from the result. The result must
be closed at the end. The code snippet below demonstrates a typical usage pattern:
HazelcastInstance instance = ...; try (SqlResult result = instance.sql().execute("SELECT * FROM person")) { for (SqlRow row : result) { long personId = row.getObject("personId"); String name = row.getObject("name"); ... } }
-
Method Summary
Modifier and TypeMethodDescriptionexecute
(SqlStatement statement) Executes an SQL statement.default SqlResult
Convenient method to execute a distributed query with the given parameter values.default long
executeUpdate
(String sql, Object... arguments) Convenience method to execute a distributed non-DQL statement (that is a statement that does not return rows) with the given parameter values.
-
Method Details
-
execute
Convenient method to execute a distributed query with the given parameter values.Converts passed SQL string and parameter values into an
SqlStatement
object and invokesexecute(SqlStatement)
.- Parameters:
sql
- SQL stringarguments
- query parameter values that will be passed toSqlStatement.setParameters(List)
- Returns:
- result
- Throws:
NullPointerException
- if the SQL string is nullIllegalArgumentException
- if the SQL string is emptyHazelcastSqlException
- in case of execution error- See Also:
-
executeUpdate
Convenience method to execute a distributed non-DQL statement (that is a statement that does not return rows) with the given parameter values.Converts passed SQL string and parameter values into an
SqlStatement
object and invokesexecute(SqlStatement)
.This method can be used for statements other than "SELECT" queries. The returned value is the
SqlResult.updateCount()
value.- Parameters:
sql
- SQL stringarguments
- query parameter values that will be passed toSqlStatement.setParameters(List)
- Returns:
- The number of updated(/inserted/deleted) rows.
- Throws:
NullPointerException
- if the SQL string is nullIllegalArgumentException
- if the SQL string is emptyHazelcastSqlException
- in case of execution error- Since:
- 5.3
- See Also:
-
execute
Executes an SQL statement.- Parameters:
statement
- statement to be executed- Returns:
- result
- Throws:
NullPointerException
- if the statement is nullHazelcastSqlException
- in case of execution error- See Also:
-