Flink编程模型

Flink编程模型

分层API

分层API
1、ProcessFunction

public class MyProcessFunction extends ProcessFunction<String, String> {
    private ValueState<Integer> count = null;

    @Override
    public void open(Configuration parameters) throws Exception {
        ValueStateDescriptor<Integer> descriptor = new ValueStateDescriptor<>("count", Integer.class);
        count = getRuntimeContext().getState(descriptor);
    }

    @Override
    public void processElement(String value, Context ctx, Collector<String> out) throws Exception {
        Integer currentCount = count.value();
        if (currentCount == null) {
            currentCount = 0;
        }
        currentCount += 1;
        count.update(currentCount);
        out.collect("Input: " + value + ", Count: " + currentCount);
    }
}

2、DataStream API

DataStream<String> input = env.addSource(new FlinkKafkaConsumer<>("input-topic", new SimpleStringSchema(), properties));
DataStream<Tuple2<String, Integer>> result = input.flatMap(new FlatMapFunction<String, Tuple2<String, Integer>>() {
    @Override
    public void flatMap(String value, Collector<Tuple2<String, Integer>> out) throws Exception {
        for (String word : value.split("\\s")) {
            out.collect(new Tuple2<>(word, 1));
        }
    }
}).keyBy(0).sum(1);
result.print();

3、SQL & Table API

TableEnvironment tableEnv = TableEnvironment.create(env);

// register a table named "input"
DataStreamSource<Tuple2<String, Integer>> input = env.fromElements(new Tuple2<>("a", 1), new Tuple2<>("b", 2));
tableEnv.registerDataStream("input", input, $("word"), $("frequency"));

// execute a SQL query
Table result = tableEnv.sqlQuery("SELECT word, SUM(frequency) as frequency FROM input GROUP BY word");
DataStream<Tuple2<Boolean, Row>> resultStream = tableEnv.toRetractStream(result, Row.class);
resultStream.print();
编程模型

在这里插入图片描述

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值