Flink 真正的流式处理演示(基于Netcat)

文章介绍了如何在ApacheFlink环境中使用DataStreamAPI处理流式文本数据,通过创建StreamWordCount类,实现对输入数据的分词、计数和累加功能。在Netcat中开启监听,程序接收并处理来自7777端口的数据,展示了KeySelector和FlatMapFunction的用法。
摘要由CSDN通过智能技术生成


前面的章节,我们讲了DataSet API和DataStream
API处理文本数据,文本其实就是一个批数据的形式,这个章节我们来操作一下真正的流式的环境

处理文本数据

准备工作

在虚拟机打开Netcat

nc -lk 7777

保持当前的连接持续监听7777端口

代码编写

创建java类StreamWordCount

package org.chad.wordcount;

import org.apache.flink.api.common.functions.FlatMapFunction;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.KeyedStream;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;


public class StreamWordCount {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        DataStreamSource<String> ds = env.socketTextStream("node1", 7777);

        SingleOutputStreamOperator<Tuple2<String, Long>> wordAndOne = ds.flatMap(new FlatMapFunction<String, Tuple2<String, Long>>() {
            @Override
            public void flatMap(String s, Collector<Tuple2<String, Long>> out) throws Exception {
                String[] words = s.split(" ");
                for (String word : words) {
                    out.collect(Tuple2.of(word, 1L));
                }
            }
        }).returns(Types.TUPLE(Types.STRING, Types.LONG));

        KeyedStream<Tuple2<String, Long>, String> tuple2StringKeyedStream = wordAndOne.keyBy(new KeySelector<Tuple2<String, Long>, String>() {
            @Override
            public String getKey(Tuple2<String, Long> stringLongTuple2) throws Exception {
                return stringLongTuple2.f0;
            }
        });

        SingleOutputStreamOperator<Tuple2<String, Long>> sum = tuple2StringKeyedStream.sum(1);

        sum.print();

        env.execute("StreamWordCount");
    }
}

运行以上代码
我们的程序在等待数据的传送
此刻我们可以
在这里插入图片描述
在我们的控制台我们可以看到
在这里插入图片描述
数据在累加,发送一条就统计一条
上面的演示中设计到一个空的单词,是因为我在发送数据的时候多打了一个空格

补充:关于KeySelector中的写法,本篇文章写法是全写,如果想看lamda表达式,简写的方式可以查看文章开头的处理文本数据连接

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chad__chang

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值