javahazelcasthazelcast-imap

Create SQL Mapping for an inner java.util.Map in Hazelcast


I have a structure like this in Hazelcast:


class User {

    private Map<String, String> customAttributes = new HashMap<>();

    private String name;

}

and an IMap<Long, User> users

How can I create an SQL mapping so that I can get these customAttributes? Something like select customAttributes from user.

If I had to query name, I would create something like this:

CREATE
OR REPLACE EXTERNAL MAPPING "hazelcast"."public"."user" EXTERNAL NAME "user" (
  "name" VARCHAR) 
  TYPE "IMap" OPTIONS (
  'keyFormat' = 'java',
  'keyJavaClass' = 'java.lang.Long',
  'valueFormat' = 'compact',
  'valueCompactTypeName' = 'my.package.User'
)

and then I could issue:

SELECT name from user

How would I hav to write the mapping in case of customAttributes, so that I could write:

SELECT customAttributes from user

We use compact serializers, if that matters.


Solution

  • It depends on your serialization.

    If you are storing your User object as binary then there is no way to select by an attribute. You can only select the User object

    If you are storing your User object as JSON, then I think you can select by an attribute

    I hope this helps

    --
    Updated answer

    According to SQL data type documentation
    https://docs.hazelcast.com/hazelcast/5.5/sql/data-types

    You should be able to create a mapping like this. I am using OBJECT column type

    CREATE OR REPLACE 
    EXTERNAL MAPPING "hazelcast"."public"."user" 
    EXTERNAL NAME "user" ("name" VARCHAR , "customAttributes" OBJECT)
    TYPE "IMap" OPTIONS (
      'keyFormat' = 'java',
      'keyJavaClass' = 'java.lang.Long',
      'valueFormat' = 'compact',
      'valueCompactTypeName' = 'com.mypackage.User'
    )
    

    But unfortunately it throws an exception here https://github.com/hazelcast/hazelcast/blob/master/hazelcast-sql/src/main/java/com/hazelcast/jet/sql/impl/connector/map/MetadataCompactResolver.java#L106-L108

    So Object column type is buggy in my opinion. Feel free to create an issue to have it fixed