Skip to content
Advertisement

apache flink 0.10 how to get the first occurence of a composite key from an unbounded input dataStream?

I am a newbie with apache flink. I have an unbound data stream in my input (fed into flink 0.10 via kakfa).

I want to get the 1st occurence of each primary key (the primary key is the contract_num and the event_dt).
These “duplicates” occur nearly immediately after each other. The source system cannot filter this for me, so flink has to do it.

Here is my input data:

JavaScript

Here is the output data I want:

JavaScript

Note the 2nd row has been removed as the key combination of A001 and ‘2016-02-24 10:25:08’ already occurred in the 1st row.

How can I do this with flink 0.10?

I was thinking about using keyBy(0,1) but after that I don’t know what to do!

(I used joda-time and org.flinkspector to setup these tests).

JavaScript

Advertisement

Answer

Filtering duplicates over an infinite stream will eventually fail if your key space is larger than your available storage space. The reason is that you have to store the already seen keys somewhere to filter out the duplicates. Thus, it would be good to define a time window after which you can purge the current set of seen keys.

If you’re aware of this problem but want to try it anyway, you can do it by applying a stateful flatMap operation after the keyBy call. The stateful mapper uses Flink’s state abstraction to store whether it has already seen an element with this key or not. That way, you will also benefit from Flink’s fault tolerance mechanism because your state will be automatically checkpointed.

A Flink program doing your job could look like

JavaScript

where the implementation of DuplicateFilter depends on the version of Flink.

Version >= 1.0 implementation

JavaScript

Version 0.10 implementation

JavaScript

Update: Using a tumbling time window

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement