初识Flink之流处理(一)

本文详细介绍了Apache Flink作为流数据处理框架的原理、应用场景,如电商营销、IoT和电信行业,以及为何选择Flink。通过WordCount实例演示了如何使用Flink进行单词词频统计,并解决了遇到的依赖问题。
摘要由CSDN通过智能技术生成

Flink介绍

Flink是一个什么样的框架呢?flink官网是这么介绍的:
Apache Flink is a framework and distributed processing engine for stateful computations over unbounded and bounded data streams.
Apache FIink是一个框架分布式处理引擎,用于对无界和有界数据流进行状态计算。

Flink的使用场景

1.电商和市场营销
数据报表、广告投放、业务流程需要
2.物联网(IOT)
传感器实时数据采集和显示、实时报警、交通运输业
3.电信业
基站流量调配
4.银行和金融业
实时结算和通知推送,实时检测异常行为

为什么选择Flink

1.流数据可以更正式地反应我们的生活方式。
2.传统的数据架构是基于有限数据集的
3.低延迟、高吞吐、结果的准确性和良好的容错性。

Flink简单示例

首先idea新建一个Maven项目
在这里插入图片描述
添加maven依赖

<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-java</artifactId>
    <version>1.12.2</version>
</dependency>

<dependency>
    <groupId>org.apache.flink</groupId>
    <artifactId>flink-streaming-java_2.12</artifactId>
    <version>1.12.2</version>
    <scope>provided</scope>
</dependency>

然后写创建一个类,用来统计单词词频统计

public class WordCount {

    public static void main(String[] args) throws Exception {
        //解析命令行传过来的参数
        ParameterTool params = ParameterTool.fromArgs(args);

        //获取一个执行环境
        final ExecutionEnvironment evn = ExecutionEnvironment.getExecutionEnvironment();
        //读取输入的数据
        DataSet<String> dataSet = null;
        if (params.has("input")) {
            dataSet = evn.readTextFile(params.get("input"));
        } else {
            dataSet = WordCountData.getDefaultTextLineDataSet(evn);
        }

        //单词词频统计
        DataSet<Tuple2<String, Integer>> counts = dataSet.flatMap(new Tokenizer()).groupBy(0).sum(1);
        if (params.has("output")) {
            // 数据输出为csv 格式
            counts.writeAsCsv(params.get("output"), "\n", "");
            // 提交执行flink应用
            evn.execute("wordcount example");
        } else {
            //数据打印到控制台
            counts.print();
        }
    }

    public static final  class Tokenizer implements FlatMapFunction<String , Tuple2<String, Integer>> {

        @Override
        public void flatMap(String s, Collector<Tuple2<String, Integer>> collector) throws Exception {
            String[] tokens = s.toLowerCase().split("\\W+");
            for (String token : tokens) {
                collector.collect(new Tuple2<String, Integer>(token,1));
            }
        }
    }
}

当然还有单词词频文件,这里我们用的flink-example项目的WordCountData文件

public class WordCountData {

    public static final String[] WORDS =
            new String[] {
                "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,",
                "And by opposing end them?--To die,--to sleep,--",
                "No more; and by a sleep to say we end",
                "The heartache, and the thousand natural shocks",
                "That flesh is heir to,--'tis a consummation",
                "Devoutly to be wish'd. To die,--to sleep;--",
                "To sleep! perchance to dream:--ay, there's the rub;",
                "For in that sleep of death what dreams may come,",
                "When we have shuffled off this mortal coil,",
                "Must give us pause: there's the respect",
                "That makes calamity of so long life;",
                "For who would bear the whips and scorns of time,",
                "The oppressor's wrong, the proud man's contumely,",
                "The pangs of despis'd love, the law's delay,",
                "The insolence of office, and the spurns",
                "That patient merit of the unworthy takes,",
                "When he himself might his quietus make",
                "With a bare bodkin? who would these fardels bear,",
                "To grunt and sweat under a weary life,",
                "But that the dread of something after death,--",
                "The undiscover'd country, from whose bourn",
                "No traveller returns,--puzzles the will,",
                "And makes us rather bear those ills we have",
                "Than fly to others that we know not of?",
                "Thus conscience does make cowards of us all;",
                "And thus the native hue of resolution",
                "Is sicklied o'er with the pale cast of thought;",
                "And enterprises of great pith and moment,",
                "With this regard, their currents turn awry,",
                "And lose the name of action.--Soft you now!",
                "The fair Ophelia!--Nymph, in thy orisons",
                "Be all my sins remember'd."
            };

    public static DataSet<String> getDefaultTextLineDataSet(ExecutionEnvironment env) {
        return env.fromElements(WORDS);
    }
}

然后执行main方法,启动后你会发现,控制台报错了:
在这里插入图片描述
接下来,我们根据报错添加依赖:

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

<dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-simple</artifactId>
    <version>1.7.25</version>
    <scope>compile</scope>
</dependency>

然后重新执行代码
在这里插入图片描述
从控制台的执行结果来看,我们成功的运行了flink的一个小案例。
源码:flink流处理demo

评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值