繁体   English   中英

Apache Flink: How do I use a stream of Java Map (or Map containing DTOs)?

[英]Apache Flink: How do I use a stream of Java Map (or Map containing DTOs)?

我正在使用 Flink,并且有一个 JSON 字符串的 stream 到达我的系统,其中包含动态变化的字段和嵌套字段。 所以我不能模拟并将这个传入的 JSON 转换为 static POJO,而我必须依赖 Map。

My first transformation is to convert the JSON string stream into a Map object stream using GSON parsing and then I wrap the map in a DTO called Data.

(inside the first map transformation)
LinkedTreeMap map = gson.fromJson(input, LinkedTreeMap.class);

Data data = new Data(map); // Data has getters, setters for the map and implements Serializable

在此转换处理之后出现问题,我尝试将生成的 stream 输入我的自定义 Flink 接收器。 调用 function 不会在接收器中调用。 但是,如果我从包含 DTO 的 Map 更改为没有 Map 的原始或常规 DTO,则接收器可以工作。

我的 DTO 如下所示:

public class FakeDTO {
    private String id;
    private LinkedTreeMap map; // com.google.gson.internal

    // getters and setters
    // constructors, empty and with fields

我尝试了以下两种解决方案:

env.getConfig().addDefaultKryoSerializer(LinkedTreeMap.class,MapSerializer.class; 
env.getConfig().disableGenericTypes();

任何专家建议我可以在这种情况下使用?

我能够解决这个问题。 在我的 Flink 日志中,我看到没有找到一个名为 ReflectionSerializerFactory class 的 Kryo 文件。 我在 maven 中更新了 Kryo 版本,并为我的 map 使用了 Map 类型,Flink 文档说 Flink 支持。

只需确保在您的代码中指定了泛型类型,并在您的 POJO 中为 Maps 添加 getter 和 setter。

我还使用 .returns(xyz.class) 类型声明来避免类型擦除的影响。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM