Using Compact Serializer Generator
Allows you to generate compact classes for your POJOs.
Creating Schemas for Business Objects
Let’s assume we have the following entities in our application:
-
Student
-
ID
-
Name
-
-
Classroom
-
ID
-
Students
-
-
School
-
ID
-
Classrooms
-
The relationships between these entities are as follows:
-
School has classrooms (1→n classrooms)
-
Classrooms have students (n→n students)
The schema definitions are as follows:
namespace: "org.example"
classes:
- name: Student
fields:
- name: id
type: int32
- name: name
type: string
namespace: "org.example"
imports:
- people.yaml
classes:
- name: Classroom
fields:
- name: id
type: int32
- name: students
type: com.people.Student[]
namespace: "org.example"
imports:
- classroom.yaml
classes:
- name: 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
, andSchool
compact classes. -
Any imported schemas, identified using
imports
. -
The namespaces for the schema files, identified using
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 com/education
directory.
Creating Compact Serialization Configuration
The generate
command lists the configuration options. 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.education.ClassroomSerializer(),
new com.education.StudentSerializer(),
new com.education.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())
}