您好,登錄后才能下訂單哦!
這篇文章主要講解了“Flink的Split怎么使用”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“Flink的Split怎么使用”吧!
Split算子:將數據流切分成多個數據流(已過時,并且不能二次切分,不建議使用)
示例環境
java.version: 1.8.x flink.version: 1.11.1
示例數據源 (項目碼云下載)
Flink 系例 之 搭建開發環境與數據
Split.java
package com.flink.examples.functions; import com.flink.examples.DataSource; import org.apache.flink.api.common.functions.MapFunction; import org.apache.flink.api.java.tuple.Tuple3; import org.apache.flink.api.java.tuple.Tuple4; import org.apache.flink.streaming.api.collector.selector.OutputSelector; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.datastream.SplitStream; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import java.util.ArrayList; import java.util.List; /** * @Description Split算子:將數據流切分成多個數據流(已過時,并且不能二次切分,不建議使用) */ public class Split { /** * 遍歷集合,將數據流切分成多個流并打印 * @param args * @throws Exception */ public static void main(String[] args) throws Exception { final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(); env.setParallelism(1); List<Tuple3<String, String, Integer>> tuple3List = DataSource.getTuple3ToList(); //Datastream DataStream<Tuple3<String, String, Integer>> dataStream = env.fromCollection(tuple3List); //按性別進行拆分 //flink.1.11.1顯示SplitStream類過時,推薦用keyBy的方式進行窗口處理或SideOutput側輸出流處理;注意,使用split切分后的流,不可二次切分,否則會拋異常 SplitStream<Tuple3<String, String, Integer>> split = dataStream.split(new OutputSelector<Tuple3<String, String, Integer>>() { @Override public Iterable<String> select(Tuple3<String, String, Integer> value) { List<String> output = new ArrayList<String>(); if (value.f1.equals("man")) { output.add("man"); } else { output.add("girl"); } return output; } }); //查詢指定名稱的數據流 DataStream<Tuple4<String, String, Integer, String>> dataStream1 = split.select("man") .map(new MapFunction<Tuple3<String, String, Integer>, Tuple4<String, String, Integer, String>>() { @Override public Tuple4<String, String, Integer, String> map(Tuple3<String, String, Integer> t3) throws Exception { return Tuple4.of(t3.f0, t3.f1, t3.f2, "男"); } }); DataStream<Tuple4<String, String, Integer, String>> dataStream2 = split.select("girl") .map(new MapFunction<Tuple3<String, String, Integer>, Tuple4<String, String, Integer, String>>() { @Override public Tuple4<String, String, Integer, String> map(Tuple3<String, String, Integer> t3) throws Exception { return Tuple4.of(t3.f0, t3.f1, t3.f2, "女"); } }); //打印:男 dataStream1.print(); //打印:女 dataStream2.print(); env.execute("flink Split job"); } }
打印結果
(張三,man,20,男) (李四,girl,24,女) (王五,man,29,男) (劉六,girl,32,女) (伍七,girl,18,女) (吳八,man,30,男)
感謝各位的閱讀,以上就是“Flink的Split怎么使用”的內容了,經過本文的學習后,相信大家對Flink的Split怎么使用這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。