Apache Flink教程----2.本地开发

Apache Flink教程----2.本地开发

1.pom


            org.apache.flink
            flink-clients_2.12
            1.8.0

2.wordcount

public class FlinkDemo {

    public static void main(String[] args) throws Exception {
        final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();

        // get input data
        DataSet text = env.fromElements(
                "To be, or not to be,--that is the question:--",
                "Whether 'tis nobler in the mind to suffer",
                "The slings and arrows of outrageous fortune",
                "Or to take arms against a sea of troubles,"
        );

        DataSet> counts =
                // split up the lines in pairs (2-tuples) containing: (word,1)
                text.flatMap(new LineSplitter())
                        // group by the tuple field "0" and sum up tuple field "1"
                        .groupBy(0) //(i,1) (am,1) (chinese,1)
                        .sum(1);

        // execute and print result
        counts.print();

    }


    public static final class LineSplitter implements FlatMapFunction> {

        public void flatMap(String value, Collector> out) {
            // normalize and split the line
            String[] tokens = value.toLowerCase().split("\\W+");

            // emit the pairs
            for (String token : tokens) {
                if (token.length() > 0) {
                    out.collect(new Tuple2(token, 1));
                }
            }
        }
    }

}

3.结果

Apache Flink教程----2.本地开发_第1张图片

官方demo: WordCount官方推荐

posted @ 2019-05-05 17:07 诸葛子房 阅读(...) 评论(...) 编辑 收藏

你可能感兴趣的:(大数据,Flink)