您好,登錄后才能下訂單哦!
這篇文章主要介紹了Flink中如何使用TimeWindowAll,具有一定借鑒價(jià)值,感興趣的朋友可以參考下,希望大家閱讀完這篇文章之后大有收獲,下面讓小編帶著大家一起了解一下。
timeWindowAll時(shí)間滾動(dòng)窗口(不分區(qū)時(shí)間滾動(dòng)窗口【滑動(dòng)窗口與滾動(dòng)窗口的區(qū)別,在于滑動(dòng)窗口會(huì)有數(shù)據(jù)元素重疊可能,而滾動(dòng)窗口不存在元素重疊】)
示例環(huán)境
java.version: 1.8.xflink.version: 1.11.1
Flink 系例 之 搭建開(kāi)發(fā)環(huán)境與數(shù)據(jù)
TimeWindowAll.java
import com.flink.examples.DataSource; 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 org.apache.flink.streaming.api.functions.source.RichSourceFunction; import org.apache.flink.streaming.api.windowing.time.Time; import java.util.List; /** * @Description 不分區(qū)時(shí)間滾動(dòng)窗口 */ public class TimeWindowAll { /* 窗口在處理流數(shù)據(jù)時(shí),通常會(huì)對(duì)流進(jìn)行分區(qū); 數(shù)據(jù)流劃分為: keyed(根據(jù)key劃分不同數(shù)據(jù)流區(qū)) non-keyed(指沒(méi)有按key劃分的數(shù)據(jù)流區(qū),指所有原始數(shù)據(jù)流) */ /** * 遍歷集合,返回指定時(shí)間滾動(dòng)窗口下最大年齡數(shù)據(jù)記錄 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); //env.setParallelism(1); DataStream<Tuple3<String, String, Integer>> inStream = env.addSource(new MyRichSourceFunction()); DataStream<Tuple3<String, String, Integer>> dataStream = inStream //按時(shí)間窗口滾動(dòng),對(duì)前6秒內(nèi)的輸入數(shù)據(jù)流,計(jì)算一次 .timeWindowAll(Time.seconds(6)) //注意:計(jì)算變量為f2 .maxBy(2); dataStream.print(); env.execute("flink TimeWindow job"); } /** * 模擬數(shù)據(jù)持續(xù)輸出 */ public static class MyRichSourceFunction extends RichSourceFunction<Tuple3<String, String, Integer>> { @Override public void run(SourceContext<Tuple3<String, String, Integer>> ctx) throws Exception { List<Tuple3<String, String, Integer>> tuple3List = DataSource.getTuple3ToList(); for (Tuple3 tuple3 : tuple3List){ ctx.collect(tuple3); //1秒鐘輸出一個(gè) Thread.sleep(1 * 1000); } } @Override public void cancel() { try{ super.close(); }catch (Exception e){ e.printStackTrace(); } } } }
打印結(jié)果
2> (王五,man,29)
感謝你能夠認(rèn)真閱讀完這篇文章,希望小編分享的“Flink中如何使用TimeWindowAll”這篇文章對(duì)大家有幫助,同時(shí)也希望大家多多支持億速云,關(guān)注億速云行業(yè)資訊頻道,更多相關(guān)知識(shí)等著你來(lái)學(xué)習(xí)!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。