Johnny
Johnny

Reputation: 7341

How to count by property and time window with Apache Flink?

Assume I have a file of the form (one event per line):

Source,Timestamp aa,2014-05-02 22:12:11 bb,2014-05-02 22:22:11

And I'd like to sum up the number of events grouped by source with a continuous time window of 5 minutes. How would I do that with Flink?

What I have right now is:

   final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
    DataStreamSource<Event> stream = env.fromCollection(new EventFileReader(new File("path/to/file")), Event.class);

    stream
        .keyBy("getSource()")
        .timeWindow(Time.minutes(5))
        .sum("getTimestamp()");     

    env.execute();

public class Event {
    private final String source;
    private final long timestamp;

    public Event(String source, long timestamp) {
        this.source = source;
        this.timestamp = timestamp;
    }

    public String getSource() {
        return source;
    }

    public long getTimestamp() {
        return timestamp;
    }
}

I'm missing two things. First, this fails and says the Event class is not a POJO. Second, I don't know how to count the number of events in the window. Right now I'm using .sum("getTimestamp()"), but I'm sure that's not it. Any thoughts?

Upvotes: 1

Views: 1723

Answers (1)

Till Rohrmann
Till Rohrmann

Reputation: 13346

I would recommend using the fold function to do the window aggregation. The following code snippet should do the job:

public class Job {
    public static void main(String[] args) throws Exception {
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.setStreamTimeCharacteristic(TimeCharacteristic.EventTime);
        DataStream<Event> stream = env.fromElements(new Event("a", 1), new Event("b", 2), new Event("a", 2)).assignTimestampsAndWatermarks(new AssignerWithPunctuatedWatermarks<Event>() {
            @Nullable
            @Override
            public Watermark checkAndGetNextWatermark(Event event, long l) {
                return new Watermark(l);
            }

            @Override
            public long extractTimestamp(Event event, long l) {
                return event.getTimestamp();
            }
        });

        DataStream<Tuple2<String, Integer>> count = stream.keyBy(new KeySelector<Event, String>() {
                @Override
                public String getKey(Event event) throws Exception {
                    return event.getSource();
                }
            })
            .timeWindow(Time.minutes(5))
            .fold(Tuple2.of("", 0), new FoldFunction<Event, Tuple2<String, Integer>>() {
                @Override
                public Tuple2<String, Integer> fold(Tuple2<String, Integer> acc, Event o) throws Exception {
                    return Tuple2.of(o.getSource(), acc.f1 + 1);
                }
            });

        count.print();

        env.execute();
    }

    public static class Event {
        private final String source;
        private final long timestamp;

        public Event(String source, long timestamp) {
            this.source = source;
            this.timestamp = timestamp;
        }

        public String getSource() {
            return source;
        }

        public long getTimestamp() {
            return timestamp;
        }
    }
}

Upvotes: 3

Related Questions