您好,登錄后才能下訂單哦!
本篇內(nèi)容主要講解“怎么用Flink Sum獲取一組數(shù)據(jù)流中的數(shù)值之和”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實用性強。下面就讓小編來帶大家學(xué)習(xí)“怎么用Flink Sum獲取一組數(shù)據(jù)流中的數(shù)值之和”吧!
sum算子:獲取一組數(shù)據(jù)流中的數(shù)值之和
示例環(huán)境
java.version: 1.8.xflink.version: 1.11.1
示例數(shù)據(jù)源 (項目碼云下載)
Flink 系例 之 搭建開發(fā)環(huán)境與數(shù)據(jù)
Sum.java
import com.flink.examples.DataSource; import org.apache.flink.api.common.functions.MapFunction; import org.apache.flink.api.common.typeinfo.Types; import org.apache.flink.api.java.functions.KeySelector; import org.apache.flink.api.java.tuple.Tuple2; import org.apache.flink.api.java.tuple.Tuple3; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import java.util.List; /** * @Description sum算子:獲取一組數(shù)據(jù)流中的數(shù)值之和 */ public class Sum { /** * 遍歷集合,返回每個性別分區(qū)下年齡之和 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); List<Tuple3<String, String, Integer>> tuple3List = DataSource.getTuple3ToList(); DataStream<Tuple2<String, Integer>> dataStream = env.fromCollection(tuple3List) .map(new MapFunction<Tuple3<String, String, Integer>, Tuple2<String, Integer>>() { @Override public Tuple2<String, Integer> map(Tuple3<String, String, Integer> tuple3) throws Exception { return new Tuple2<>(tuple3.f1,tuple3.f2); } }) .returns(Types.TUPLE(Types.STRING,Types.INT)) .keyBy((KeySelector<Tuple2<String, Integer>, String>) k ->k.f0) //按數(shù)量窗口滾動,每3個輸入數(shù)據(jù)流,計算一次 .countWindow(3) .sum(1); dataStream.print(); env.execute("flink Sum job"); } }
打印結(jié)果
4> (girl,74) 2> (man,79)
到此,相信大家對“怎么用Flink Sum獲取一組數(shù)據(jù)流中的數(shù)值之和”有了更深的了解,不妨來實際操作一番吧!這里是億速云網(wǎng)站,更多相關(guān)內(nèi)容可以進入相關(guān)頻道進行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。