前言
在Flink中比如某些算子(join,coGroup,keyBy,groupBy)要求在數(shù)據(jù)元上定義key。另外有些算子操作,例如reduce,groupReduce,Aggregate,Windows需要數(shù)據(jù)在處理之前根據(jù)key進(jìn)行分組。
If you want to use keyed state, you first need to specify a key on a?DataStream?that should be used to partition the state (and also the records in the stream themselves). You can specify a key using?keyBy(KeySelector)?on a?DataStream. This will yield a?KeyedDataStream, which then allows operations that use keyed state.
A key selector function takes a single record as input and returns the key for that record. The key can be of any type and?must?be derived from deterministic computations.
The data model of Flink is not based on key-value pairs. Therefore, you do not need to physically pack the data set types into keys and values. Keys are “virtual”: they are defined as functions over the actual data to guide the grouping operator.
創(chuàng)建環(huán)境:

一、根據(jù)上一個(gè)算子輸出的順序獲取
java中第一個(gè)是0,第二個(gè)是1,以此類推,scala第一個(gè)是_1,第二個(gè)是_2,以此類推

二、根據(jù)上一個(gè)算子輸出的順序的默認(rèn)field值獲取
java中第一個(gè)是f0,第二個(gè)是f1,以此類推

三、根據(jù)上一次算子輸出的pojo類來獲取對(duì)應(yīng)的屬性
如果pojo類有嵌套,則用“.”嵌套獲取。注意必須是pojo類


四、根據(jù)上一個(gè)算子輸出的2個(gè)及以上值來做組合鍵

五、通過創(chuàng)建KeySelector類,繼承g(shù)etKey方法來創(chuàng)建key

六、測(cè)試
在cmd中啟動(dòng)nc:

控制臺(tái)輸出:

七、源碼
Flink源碼:https://github.com/apache/flink
Flink官網(wǎng):https://ci.apache.org/projects/flink/flink-docs-release-1.12/