flink之WordCount

1.代码如下

public class WordCount {

public static void main(String[] args)throws Exception {

final ParameterTool params = ParameterTool.fromArgs(args);

        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.getConfig().setGlobalJobParameters(params);

        DataStream text;

        if (params.has("input")){

                text = env.readTextFile(params.get("input"));

        }else {

                text = env.fromElements(WordCountData.WORDS);

        }

DataStream> counts =

                        text.flatMap(new FlatMapFunction>() {

                                public void flatMap(String s, Collector> collector)throws Exception {

                                        String[] toknes = s.toLowerCase().split(" ");

                                        for (String token : toknes){

                                                if (token.length() >0) {

                                                collector.collect(new Tuple2(token,1));

                                        }

                                    }

                          }

                    }).keyBy(0).sum(1);

        if (params.has("output")){

                counts.writeAsText(params.get("output"));

        }else {

                counts.print();

        }

env.execute("StreamWodCount");

    }

}

2.打包执行

flink run --class com.hui.learn.WordCount target/word-count-with-input-1.0-SNAPSHOT.jar --input /Users/didi/IdeaProjects/flink/wordcountwithinput/input --output /Users/didi/IdeaProjects/flink/wordcountwithinput/output2

最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容