您好,登錄后才能下訂單哦!
這篇文章主要講解了“Flink Reduce怎么用”,文中的講解內(nèi)容簡單清晰,易于學(xué)習(xí)與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“Flink Reduce怎么用”吧!
Reduce算子:對數(shù)據(jù)流進(jìn)行滾動聚合計(jì)算,并返回每次滾動聚合計(jì)算合并后的結(jié)果
示例環(huán)境
java.version: 1.8.x flink.version: 1.11.1
示例數(shù)據(jù)源 (項(xiàng)目碼云下載)
Flink 系例 之 搭建開發(fā)環(huán)境與數(shù)據(jù)
Reduce.java
import com.flink.examples.DataSource; import org.apache.flink.api.common.functions.ReduceFunction; import org.apache.flink.api.java.functions.KeySelector; import org.apache.flink.api.java.tuple.Tuple3; import org.apache.flink.streaming.api.datastream.KeyedStream; import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import java.util.List; /** * @Description Reduce算子:對數(shù)據(jù)流進(jìn)行滾動聚合計(jì)算,并返回每次滾動聚合計(jì)算合并后的結(jié)果 */ public class Reduce { /** * 遍歷集合,分區(qū)打印每一次滾動聚合的結(jié)果 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.setParallelism(4); List<Tuple3<String,String,Integer>> tuple3List = DataSource.getTuple3ToList(); //注意:使用Integer進(jìn)行分區(qū)時(shí),會導(dǎo)致分區(qū)結(jié)果不對,轉(zhuǎn)換成String類型輸出key即可正確輸出 KeyedStream<Tuple3<String,String,Integer>, String> keyedStream = env.fromCollection(tuple3List).keyBy(new KeySelector<Tuple3<String,String,Integer>, String>() { @Override public String getKey(Tuple3<String, String, Integer> tuple3) throws Exception { //f1為性別字段,以相同f1值(性別)進(jìn)行分區(qū) return String.valueOf(tuple3.f1); } }); SingleOutputStreamOperator<Tuple3<String, String, Integer>> result = keyedStream.reduce(new ReduceFunction<Tuple3<String, String, Integer>>() { @Override public Tuple3<String, String, Integer> reduce(Tuple3<String, String, Integer> t0, Tuple3<String, String, Integer> t1) throws Exception { int totalAge = t0.f2 + t1.f2; return new Tuple3<>("", t0.f1, totalAge); } }); result.print(); env.execute("flink Reduce job"); } }
打印結(jié)果
## 說明:為什么每一個(gè)分區(qū)的第一個(gè)數(shù)據(jù)對象每一個(gè)參數(shù)有值,是因?yàn)闈L動聚合返回的是從第二數(shù)據(jù)對象向前疊加第一個(gè)數(shù)據(jù)對象,開始計(jì)算,所以第一個(gè)數(shù)據(jù)對象根本就不進(jìn)入reduce方法; 2> (張三,man,20) 2> (,man,49) 2> (,man,79) 4> (李四,girl,24) 4> (,girl,56) 4> (,girl,74)
感謝各位的閱讀,以上就是“Flink Reduce怎么用”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對Flink Reduce怎么用這一問題有了更深刻的體會,具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是億速云,小編將為大家推送更多相關(guān)知識點(diǎn)的文章,歡迎關(guān)注!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。