Skip to content

Commit 087f2ac

Browse files
authored
Update README.md
Improve explanation of custom TypeInfo
1 parent 36473fe commit 087f2ac

File tree

1 file changed

+14
-11
lines changed

1 file changed

+14
-11
lines changed

java/Serialization/CustomTypeInfo/README.md

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,21 +17,22 @@ the application state.
1717

1818
#### Background
1919

20-
Every object handed over between the operators of the application or stored in the application state is serialized using
21-
Flink serialization mechanism.
20+
Every object handed over between operators or stored in application state is serialized using
21+
Flink serialization mechanism. Flink is able to efficiently serialize most of Java simple types, and POJOs where fields are basic types.
2222

23-
Flink is able to efficiently serialize most of Java simple types, and POJOs where fields are basic types.
24-
25-
When Flink cannot natively serialize an object, it falls back to using [Kryo](https://github.com/EsotericSoftware/kryo).
26-
Unfortunately, Kryo serialization is less efficient and has a considerable impact on performance. In particular on CPU
23+
When Flink cannot natively serialize an object, it falls back to [Kryo](https://github.com/EsotericSoftware/kryo).
24+
Unfortunately, Kryo serialization is less efficient and has a considerable impact on performance, in particular on CPU
2725
utilization.
2826

29-
One important case where Flink cannot natively serialize a field is with Collections. Due to Java type erasure, Flink
30-
cannot discover the type of the elements of the collection, and falls back to Kryo to serialize it.
27+
One important case where Flink cannot natively serialize a field is with Collections. Due to Java type erasure, at runtime Flink
28+
cannot discover the type collections' elements, forcing it to fall back to the less efficient Kryo serialization.
29+
30+
You can easily prevent using Kryo for collections, adding a `TypeInfo` to the class and defining a custom TypeInfoFactory that
31+
describe the content of the collection.
3132

3233
#### Defining a TypeInfo
3334

34-
To prevent this, you can explicitly define the type of the collection elements using the `@TypeInfo` annotation and
35+
To prevent this, you can explicitly define the collection'a elements type using the `@TypeInfo` annotation and
3536
defining a custom `TypeInfoFactory`.
3637
This is demonstrated in these two record classes:
3738
[`VehicleEvent`](src/main/java/com/amazonaws/services/msf/domain/VehicleEvent.java)
@@ -40,9 +41,12 @@ and [`AgggregateVehicleEvent`](src/main/java/com/amazonaws/services/msf/domain/A
4041
```java
4142
public class VehicleEvent {
4243
//...
44+
4345
@TypeInfo(SensorDataTypeInfoFactory.class)
4446
private Map<String, Long> sensorData = new HashMap<>();
47+
4548
//...
49+
4650
public static class SensorDataTypeInfoFactory extends TypeInfoFactory<Map<String, Long>> {
4751
@Override
4852
public TypeInformation<Map<String, Long>> createTypeInfo(Type t, Map<String, TypeInformation<?>> genericParameters) {
@@ -52,8 +56,7 @@ public class VehicleEvent {
5256
}
5357
```
5458

55-
For more details about Flink serialization, see [Data Types & Serialization](https://nightlies.apache.org/flink/flink-docs-release-1.20/docs/dev/datastream/fault-tolerance/serialization/types_serialization/#data-types--serialization)
56-
in Flink documentation.
59+
For more details about Flink serialization, see [Data Types & Serialization](https://nightlies.apache.org/flink/flink-docs-release-1.20/docs/dev/datastream/fault-tolerance/serialization/types_serialization/#data-types--serialization) in Flink documentation.
5760

5861
#### Testing serialization
5962

0 commit comments

Comments
 (0)