flink 中自定义类的 hashCode() 和 equals() 方法 [英] hashCode() and equals() method for custom classes in flink

查看:48
本文介绍了flink 中自定义类的 hashCode() 和 equals() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑 Flink 中的自定义类是否需要覆盖 hashCode()equals() 方法,因为我已经阅读了 这个页面 hashCode() 绝不能在分布式系统中实现,Apache Flink 就是其中之一.

my doubt is about if custom classes in Flink with Java needs to override or not hashCode() and equals() methods because I have read in this page that hashCode() MUST never be implemented in distributed systems and Apache Flink is one of them.

示例:我有这个类:

public class EventCounter {
    public String Id;
    public long count;
    public Timestamp firstEvent;
    public Timestamp lastEvent;
    public Date date;

    public EventCounter() {
    }
}

我需要在 Flink 中为此类类实现 hashCode()equals() 还是让 Flink 管理这些方法对性能更好是自己的吗?

Do I need to implement hashCode() and equals() for this kind of classes in Flink or it is better for performance if I let Flink manage those methods on it's own?

亲切的问候!

推荐答案

要在 Flink 中用作键的类型(即,作为从 KeySelector 返回的值)必须具有 hashCode 和 equals 的有效实现.特别是,hashCode 必须在 JVM 之间具有确定性(这就是数组和枚举在 Flink 中不能作为键的原因).

Types that you want to use as keys in Flink (i.e., as values you return from a KeySelector) must have valid implementations of hashCode and equals. In particular, hashCode must be deterministic across JVMs (which is why arrays and enums don't work as keys in Flink).

这篇关于flink 中自定义类的 hashCode() 和 equals() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆