Flink 入门

Flink 目前最流行的流式引擎,主要是用来替换jstorm和spark streaming的, 实现对实时数据流的处理,很多操作接口和spark的api非常相像。

1. 源码安装

http://archive.apache.org/dist/flink/flink-1.8.0/

 tar -zxf flink-1.7.0-bin-hadoop28-scala_2.11.tgz
 
 
./bin/start-cluster.sh

测试用例:

nc -l 9000

./flink run ../examples/streaming/SocketWindowWordCount.jar --port 9000

tail -f ../log/flink-root-taskexecutor-0-localhost.localdomain.out

体验完毕, 简单的看一下代码:

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

        // 用final修饰符定义端口号,表示不可变
        final int port;
        try {
            final ParameterTool params = ParameterTool.fromArgs(args);
            port = params.getInt("port");
        } catch (Exception e) {
            System.err.println("No port specified. Please run 'SocketWindowWordCount --port <port>'");
            return;
        }

        // (1)获取执行环境
        final StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        // (2)获取数据流,例子中是从指定端口的socket中获取用户输入的文本
        DataStream<String> text = env.socketTextStream("localhost", port, "\n");

        // (3)transformation操作,对数据流实现算法
        DataStream<WordWithCount> windowCounts = text
                //将用户输入的文本流以非空白符的方式拆开来,得到单个的单词,存入命名为out的Collector中
                .flatMap(new FlatMapFunction<String, WordWithCount>() {
                    public void flatMap(String value, Collector<WordWithCount> out) {
                        for (String word : value.split("\\s")) {
                            out.collect(new WordWithCount(word, 1L));
                        }
                    }
                })
                //将输入的文本分为不相交的分区,每个分区包含的都是具有相同key的元素。也就是说,相同的单词被分在了同一个区域,下一步的reduce就是统计分区中的个数
                .keyBy("word")
                //滑动窗口机制,每1秒计算一次最近5秒
                .timeWindow(Time.seconds(5), Time.seconds(1))
                //一个在KeyedDataStream上“滚动”进行的reduce方法。将上一个reduce过的值和当前element结合,产生新的值并发送出。
                //此处是说,对输入的两个对象进行合并,统计该单词的数量和
                .reduce(new ReduceFunction<WordWithCount>() {
                    public WordWithCount reduce(WordWithCount a, WordWithCount b) {
                        return new WordWithCount(a.word, a.count + b.count);
                    }
                });

        // 单线程执行该程序
        windowCounts.print().setParallelism(1);

        env.execute("Socket 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 word + " : " + count;
        }
    }
}

2. idea 开发环境配置

flink example 参考:

https://github.com/streaming-with-flink/examples-scala
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值