Flink for java之二:指定鍵Keyed DataStream

前言

在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/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容