Mapping to Hazelcast Maps
To query entries in maps, you need to create a mapping with the map connector so that the SQL service knows how to access the entries.
The map connector allows you to create mappings to a distributed map in a local Hazelcast cluster.
To create a mapping to a map in SQL, you must tell Hazelcast how to serialize/deserialize the keys and values by specifying the
valueFormat options in the
CREATE MAPPING statement.
For maps whose keys and values are primitives, you need to set these options to the SQL type that corresponds to the primitive:
For example, to create a mapping for
CREATE MAPPING my_map TYPE IMap OPTIONS ( 'keyFormat'='int', 'valueFormat'='varchar' )
For a reference, see SQL Data Types.
For object formats, you must specify other options, depending on the serialization format:
If your map’s keys or values are portable, you need to provide the following additional options:
valuePortableVersion(optional, default is
For example, to create a mapping for a map where both key and value are Portable:
CREATE MAPPING my_map TYPE IMap OPTIONS ( 'keyFormat' = 'portable', 'keyPortableFactoryId' = '123', 'keyPortableClassId' = '456', 'keyPortableVersion' = '0', -- optional 'valueFormat' = 'portable', 'valuePortableFactoryId' = '123', 'valuePortableClassId' = '789', 'valuePortableVersion' = '0' -- optional )
If you omit a column list from the
CREATE MAPPING statement, Hazelcast will
resolve the column names and types by looking at the
ClassDefinition with the given factory ID, class ID, and version.
ClassDefinition with the given IDs is not known to the cluster,
you must provide a column list so that Hazelcast can use it to create the
For more information about this serialization option, see Implementing Portable Serialization.
If your map’s keys or values have
Compact format, you need to provide the following additional options:
The column list is mandatory and Hazelcast will create the
schema based on the column list.
The benefit of this format is that it doesn’t deserialize the whole key
or value when reading only a subset of fields. Also it doesn’t require a
custom Java class to be defined on the cluster, so it’s usable for
non-Java clients similar to
Portable. And it is more space-efficient than
Example mapping where both key and value are
CREATE MAPPING my_map ( id INT EXTERNAL NAME "__key.id", name VARCHAR, surname VARCHAR, age INT) TYPE IMap OPTIONS ( 'keyFormat' = 'compact', 'keyCompactTypeName' = 'personId', 'valueFormat' = 'compact', 'valueCompactTypeName' = 'person' )
For more information about this serialization option, see Compact Serialization.
If values are in the JSON format, configure the
valueFormat field as
CREATE MAPPING my_map TYPE IMap OPTIONS ( 'keyFormat' = 'bigint', 'valueFormat' = 'json');
CREATE MAPPING my_map( __key BIGINT, ticker VARCHAR, amount INT) TYPE IMap OPTIONS ( 'keyFormat' = 'bigint', 'valueFormat' = 'json-flat');
For maps whose keys or values are serialized with Java serialization,
IdentifiedDataSerializable, you need to provide the name of the Java class into which you want to serialize data, using the following additional options:
CREATE MAPPING my_map TYPE IMap OPTIONS ( 'keyFormat' = 'java', 'keyJavaClass' = 'java.lang.Long', 'valueFormat' = 'java', 'valueJavaClass' = 'com.example.Person')
If the Java class corresponds to one of the basic data types (numbers,
dates, strings), that type will be used for the key or value
and mapped as a column named
__key for keys and
this for values. In
the example above, the key will be mapped with the
BIGINT type. In
fact, the above
keyJavaClass duo is equivalent to
If the Java class is not one of the basic types:
Hazelcast will analyze the class using reflection and use its properties as column names.
Hazelcast recognizes public fields and JavaBean-style getters. If some property has a non-primitive type, it will be mapped under the
The class must be available to the cluster.
You can either add the class to the members' classpaths by creating a JAR file and adding it to the
libdirectory, or you can use user code deployment. User code deployment must be enabled on the members, see Deploying User Code from Clients for details.