Using Compact Serializer Generator

Allows you to generate compact classes for your POJOs.

Before you Begin

You need the following:

  • Hazelcast CLC

  • Hazelcast cluster

Creating Schemas for Business Objects

Let’s assume you have the following entities in your application:

  • Student

    • ID: an integer

    • Name: a string

  • Classroom

    • ID: an integer

    • Students: an array of Student.

  • School

    • ID: an integer

    • Classrooms: an aray of Classroom.

The schema definitions are as follows:

serializer:
  namespace: org.example.serializers
classes:
  - name: org.example.Student
    fields:
      - name: id
        type: int32
      - name: name
        type: string
serializer:
  namespace: org.example.serializers
classes:
  - name: org.example.Classroom
    fields:
      - name: id
        type: int32
      - name: students
        type: com.people.Student[]
serializer:
  namespace: org.example.serializers
classes:
  - name: org.example.School
    fields:
      - name: id
        type: int32
      - name: classrooms
        type: com.rooms.Classroom[]

Schema definitions contain following information:

  • A description of the content of the Student, Classroom, and School compact classes.

  • The namespaces for the schema files, identified using serializer.namespace.

Generating Compact Classes from Schemas

To generate compact classes, use the following command:

clc serializer generate school.yaml -l java

This generates the classes in the current working directory. To save them to a different directory, use the following command, where <my_directory> identifies the directory in which you want to generate the classes:

clc serializer generate school.yaml -l java -o <my_directory>

The following classes are generated:

  • StudentSerializer.java

  • SchoolSerializer.java

  • ClassroomSerializer.java

in org/example/serializers directory.

Creating Compact Serialization Configuration

You can configure the configuration options in any of the following ways:

  • Java Configuration

  • XML Configuration

  • YAML Configuration

In the following code, we use the Java configuration to register the generated compact classes:

ClientConfig clientConfig = new ClientConfig();
clientConfig.getSerializationConfig()
    .getCompactSerializationConfig()
    .setSerializers(new com.example.serializers.ClassroomSerializer(),
            new com.example.serializers.StudentSerializer(),
            new com.example.serializers.SchoolSerializer());

Using SQL Support

To use SQL support, you must create a mapping, write to the map, and write to the map using a Java client. You can then check the type of the created records and query the map using the Java client.

These steps are described in the following sections.

Creating Mapping

If the keys or values in a map use the Compact format, provide the following options when creating the mapping:

  • keyCompactTypeName

  • valueCompactTypeName

CREATE OR REPLACE MAPPING students(
     id INT EXTERNAL NAME "__key.id",
     name VARCHAR
) TYPE IMap
OPTIONS (
     'keyFormat' = 'compact',
     'keyCompactTypeName' = 'studentId',
     'valueFormat' = 'compact',
     'valueCompactTypeName' = 'student'
);

You can execute the SQL either using CLC or the Java Client.

Writing to a Map using a Java Client

You can write to a Map using a Java Client as follows:

IMap<Integer, Student> studentsMap = client.getMap("students");
students.set("joe", new Student(1, "Joe Dalton"));
students.set("william", new Student(2, "William Dalton"));
students.set("jack", new Student(3, "Jack Dalton"));
students.set("Averell", new Student(4, "Averell Dalton"));

Checking the Created Records' Type

As we used the Compact format when creating the map, we must check whether the format uses the correct type. We do this as follows:

clc map entry-set --name students --show-type -f table
 __key   | __key_type | this | this_type |      id | name
 joe     | STRING     | >    | COMPACT   |       1 | Joe Dalton
 william | STRING     | >    | COMPACT   |       2 | William Dalton
 Averell | STRING     | >    | COMPACT   |       4 | Averell Dalton
 jack    | STRING     | >    | COMPACT   |       3 | Jack Dalton

As you can see, all records are defined as COMPACT in the this_type column.

Querying the Map using Java Client

You can now query the Compact values from the map and assign them to Java object as follows:

SqlResult result = sqlService.execute(String.format("SELECT this FROM students WHERE name = '%s'", "Joe Dalton"))
for (SqlRow row : result) {
    Student s = row.getObject("this");
    System.out.println(s.getId())
    System.out.println(s.getName())
}