您好,登錄后才能下訂單哦!
這篇文章主要介紹“Flink SideOutput怎么使用”,在日常操作中,相信很多人在Flink SideOutput怎么使用問(wèn)題上存在疑惑,小編查閱了各式資料,整理出簡(jiǎn)單好用的操作方法,希望對(duì)大家解答”Flink SideOutput怎么使用”的疑惑有所幫助!接下來(lái),請(qǐng)跟著小編一起來(lái)學(xué)習(xí)吧!
SideOutput方法:(側(cè)輸出)從主數(shù)據(jù)流中根據(jù)outputTag獲取額外的輸出流(分流場(chǎng)景下使用)
示例環(huán)境
java.version: 1.8.x flink.version: 1.11.1
示例數(shù)據(jù)源 (項(xiàng)目碼云下載)
Flink 系例 之 搭建開(kāi)發(fā)環(huán)境與數(shù)據(jù)
SideOutput.java
import com.flink.examples.DataSource; import org.apache.flink.api.java.tuple.Tuple3; import org.apache.flink.api.java.tuple.Tuple4; import org.apache.flink.streaming.api.datastream.DataStream; import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator; import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment; import org.apache.flink.streaming.api.functions.ProcessFunction; import org.apache.flink.util.Collector; import org.apache.flink.util.OutputTag; import java.util.List; /** * @Description SideOutput方法:(側(cè)輸出)從主數(shù)據(jù)流中根據(jù)outputTag獲取額外的輸出流(分流場(chǎng)景下使用) */ public class SideOutput { /** * 遍歷集合,將數(shù)據(jù)流切分成多個(gè)流并打印 * @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); //自定義man和girl兩個(gè)性別tag OutputTag<Tuple4<String, String, Integer, String>> manTag = new OutputTag<Tuple4<String, String, Integer, String>>("man"){}; OutputTag<Tuple4<String, String, Integer, String>> girlTag = new OutputTag<Tuple4<String, String, Integer, String>>("girl"){}; //將所有流數(shù)據(jù)輸入到process做處理 SingleOutputStreamOperator<Tuple4<String, String, Integer, String>> output = dataStream.process(new ProcessFunction<Tuple3<String,String,Integer>, Tuple4<String, String, Integer, String>>() { @Override public void processElement(Tuple3<String, String, Integer> value, Context ctx, Collector<Tuple4<String, String, Integer, String>> out) throws Exception { //將數(shù)據(jù)流按不同的性別劃分,創(chuàng)建新的Tuple4,分別綁定到不同性別的tag if (value.f1.equals("man")){ ctx.output(manTag, Tuple4.of(value.f0, value.f1, value.f2, "男")); }else { ctx.output(girlTag, Tuple4.of(value.f0, value.f1, value.f2, "女")); } } }); //獲取指定tag的數(shù)據(jù)流 DataStream<Tuple4<String, String, Integer, String>> dataStream1 = output.getSideOutput(manTag); DataStream<Tuple4<String, String, Integer, String>> dataStream2 = output.getSideOutput(girlTag); //打印 dataStream1.print(); dataStream2.print(); env.execute("flink Split job"); } }
打印結(jié)果
(張三,man,20,男) (李四,girl,24,女) (王五,man,29,男) (劉六,girl,32,女) (伍七,girl,18,女) (吳八,man,30,男)
到此,關(guān)于“Flink SideOutput怎么使用”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識(shí),請(qǐng)繼續(xù)關(guān)注億速云網(wǎng)站,小編會(huì)繼續(xù)努力為大家?guī)?lái)更多實(shí)用的文章!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀(guā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)容。