Flink 흐름 처리 입문 (자바)

13726 단어 Flink
간단 한 흐름 처리 코드
package com.heiheihei.flink;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.functions.ReduceFunction;
import org.apache.flink.api.java.utils.ParameterTool;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.windowing.time.Time;

/**
 * @author    1212
 * @version 1.0
 * @date 2020/3/31 17:22
 */
public class SocketWindowWordCount {
    public static void main(String[] args) throws Exception {
        //      
        int port = 8081;
        try {
            //       
            final ParameterTool params = ParameterTool.fromArgs(args);
            port = params.getInt("port");
        } catch (Exception e) {
            System.err.println("No port specified. Please run 'SocketWindowWordCount --port");
        }
        //    
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        //    
        DataStream text = env.socketTextStream("localhost", port, "
"
); // DataStream<WordWithCount> windowCounts = text.flatMap((FlatMapFunction<String, WordWithCount>) (s, collector) -> { for (String word : s.split(" ")) { collector.collect(new WordWithCount(word, 1L)); } }); // windowCounts.keyBy("word") .timeWindow(Time.seconds(5), Time.seconds(1)) .reduce(new ReduceFunction<WordWithCount>() { @Override public WordWithCount reduce(WordWithCount wordWithCount, WordWithCount t1) throws Exception { return new WordWithCount(wordWithCount.word, wordWithCount.count + t1.count); } }); windowCounts.print().setParallelism(1); env.execute("Soket Window WordCount"); } public static class WordWithCount { public String word; public long count; public WordWithCount() { } public WordWithCount(String word, long count) { this.word = word; this.count = count; } @Override public String toString() { return "WordWithCount{" + "word='" + word + '\'' + ", count=" + count + '}'; } } }

좋은 웹페이지 즐겨찾기