Search code examples
javaapache-flinkdeprecated

Flink KeyBy fields


I am trying to use the keyby function with a given field, however, IntelliJ is telling me that this option is deprecated.

// map each job event to a 2-tuple
DataStream<Tuple2<Long, Long>> mappedEvents =  events.map(new AppendOneMapper()).slotSharingGroup("2");

// group the stream of tuples by jobId
KeyedStream<Tuple2<Long, Long>, Tuple> keyedEvents = mappedEvents.keyBy(0);

How would I use new methods to correctly do this?


Solution

  • KeyBy with integers or strings is deprecated. From documentation: "Deprecated. Use keyBy(KeySelector)."

    org.apache.flink.api.java.functions
    Interface KeySelector<IN,KEY>
    Type Parameters:
    IN - Type of objects to extract the key from.
    KEY - Type of key.
    

    KeySelector is a functional interface, so you can just plug in lambda expression.

    Replace

    .keyBy(key)
    

    With

    .keyBy(event -> event.getKey())