Search code examples
javatuplesapache-flinkflink-streaming

Create a tuple with an optional field (Flink Java)


I want to store an optional value associated with a Tuple2<String, String> key.

To do so, I tried creating a class MyKey extends Tuple3<String, String, String> with the third field optional. It can be null and is not used when checking for equality.

I then overrided the equals() and hashCode() methods:

class MyKey extends Tuple3<String, String, String> {
    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (!(o instanceof myKey)) {
            return false;
        }

        MyKey tuple = (MyKey) o;

        if (f0 != null ? !f0.equals(tuple.f0) : tuple.f0 != null) {
            return false;
        }
        if (f1 != null ? !f1.equals(tuple.f1) : tuple.f1 != null) {
            return false;
        }
        return true;

    }

    @Override
    public int hashCode() {
        int result = f0 != null ? f0.hashCode() : 0;
        result = 31 * result + (f1 != null ? f1.hashCode() : 0);
        return result;
    }
}
  • Are there any gotchas I should be aware of with this method?
  • Are there any other solutions that I can consider?

Solution

  • You should be aware that the tuple serializer Flink uses with the RocksDB state backend cannot serialize records with null fields. I'd say that's a problem.