zoukankan      html  css  js  c++  java
  • Apache Flink教程----2.本地开发

    1.pom

    <dependency>
                <groupId>org.apache.flink</groupId>
                <artifactId>flink-clients_2.12</artifactId>
                <version>1.8.0</version>
    </dependency>
    

    2.wordcount

    public class FlinkDemo {
    
        public static void main(String[] args) throws Exception {
            final ExecutionEnvironment env = ExecutionEnvironment.getExecutionEnvironment();
    
            // get input data
            DataSet<String> 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<Tuple2<String, Integer>> 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<String, Tuple2<String, Integer>> {
    
            public void flatMap(String value, Collector<Tuple2<String, Integer>> 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<String, Integer>(token, 1));
                    }
                }
            }
        }
    
    }

    3.结果

    官方demo: WordCount官方推荐

  • 相关阅读:
    CCF 201712-4 90分
    hdu2159
    ch11 持有对象
    ch7复用类
    PLSQL Developer 9注册码
    vue + typespript + webpack
    JavaScript 复制内容到剪贴板
    在不同电脑设备之间, 同步 VSCode 的插件和配置
    常用正则:提取内容, 格式化数字
    js css3 固定点拖拽旋转
  • 原文地址:https://www.cnblogs.com/zgzf/p/10814794.html
Copyright © 2011-2022 走看看