memas.kal
memas.kal

Reputation: 33

Flink's POJO serializer for an interface

Let's say we have this contrived example of an interface Shape and its implementations. How I can serialize myPojo (with the POJO serializer) which contains a shape? Both Circle and Rectangle are POJOs.

public interface Shape {
    public double area(); 
}

public class Circle implements Shape{
    // constructors
    // radius
    // implement area();
    // getters, setters
}

public class Rectangle implements Shape {
    // constructors
    // height, width
    // implement area();
    // getters, setters
}

public class MyPojo {
    int anotherField;
    Shape shape;
    // constructors
   // getters, setters
}

My execution config looks like this:

    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.getConfig().disableGenericTypes();
    env.getConfig().registerPojoType(Shape.class);
    env.getConfig().registerPojoType(Circle.class);
    env.getConfig().registerPojoType(Rectangle.class);

Upvotes: 1

Views: 753

Answers (2)

memas.kal
memas.kal

Reputation: 33

The problem is that the Shape interface has no fields, even if creating a dummy field is considered static thus having the same result. The "hack" solution which I resulted in, is to provide an empty HashMap for the fields:

public class ShapeTypeInfoFactory extends TypeInfoFactory<Shape> {
  @Override
  public TypeInformation<Shape> createTypeInfo(
      Type t, Map<String, TypeInformation<?>> genericParameters) {
    return Types.POJO(Shape.class, new HashMap<>());
  }
}

Upvotes: 2

eskabetxe
eskabetxe

Reputation: 86

Shape don't have valid methods to be consider a POJO

All fields are either public or must be accessible through getter and setter functions. For a field called foo the getter and setter methods must be named getFoo() and setFoo().

public interface Shape {
    public double getArea(); 
    public void setArea(double area); 
}

Upvotes: 2

Related Questions