Flink DataStream常用API

1. Flink API的抽象级别分析

Flink中提供了4种不同层次的API:

  • 低级API:提供了对时间和状态的细粒度控制,简洁性和易用性较差,主要应用在对一些复杂事件的处理逻辑上。
  • 核心API:主要提供了针对流数据和离线数据的处理,对低级API进行了一些封装,提供了filter、sum、max、min等高级函数,简单且易用,所以在工作中应用比较广泛。
  • Table API:一般与DataSet或者DataStream紧密关联,首先通过一个DataSet或DataStream创建出一个Table;然后用类似于filter、join或者select关系型转化操作来转化为一个新的Table对象;最后将一个Table对象转回一个DataSet或DataStream。与SQL不同的是,Table API的查询不是一个指定的SQL字符串,而是调用指定的API方法
  • SQL: Flink的SQL集成是基于Apache Calcite的,Apache Calcite实现了标准的SQL,使用起来比其他API更灵活,因为可以直接使用SQL语句,Table API和SQL可以很容易地结合在一块使用,它们都返回Table对象。

2. Flink DataStream常用API

DataStream API主要分为3块:DataSource、Transformation、Sink。

  • DataSource是程序的数据源输入,可以通过StreamExecutionEnvironment.addSource(sourceFunction)为程序添加一个数据源。
  • Transformation是具体的操作,它对一个或多个输入数据源进行计算处理,比如Map、FlatMap和Filter等操作。
  • Sink是程序的输出,它可以把Tranfomation处理之后的数据输出到指定的存储介质中。

3. DataSource

Flink针对DataStream提供了大量的已经实现的DataSource(数据源)接口。

3.1 基于文件

readTextFile(path)

读取文本文件文件遵循TextInputFormat逐行读取规则并返回。

3.2 基于Socket

socketTextStream

从Socket中读取数据,元素可以通过一个分隔符分开

3.3 基于集合

fromCollection(Collection)

通过Java的Collection集合创建一个数据流,集合中的所有元素必须是相同类型的

3.4 自定义输入

addSource可以实现读取第三方数据源的数据

4. 自定义数据源

4.1 自定义无并行度

可以通过实现SourceFunction接口来自定义无并行度(并行度为1)的数据源

package streaming;

import org.apache.flink.streaming.api.functions.source.SourceFunction;

/**
 * 模拟产生从1开始递增数字,每次递增加1
 * 自定义实现并行度为1的Source
 */
public class MyNoParalleSource implements SourceFunction<Long> {

    private long count = 1L;
    private boolean isRunning = true;

    /**
     * 启动一个Source
     * 大部分情况下,都需要在这个run方法中实现一个循环
     * 这样就可以循环产生数据了
     * @param sourceContext
     * @throws Exception
     */
    public void run(SourceContext<Long> sourceContext) throws Exception {
        while(isRunning){
            sourceContext.collect(count);
            count++;
            //每秒产生一条数据
            Thread.sleep(1000);
        }
    }

    /**
     * 执行cancel操作时候会调用的方法
     */
    public void cancel() {
        isRunning = false;
    }
}

4.2 自定义有并行度

通过实现ParallelSourceFunction接口或者继承RichParallelSourceFunction来自定义有并行度的数据源

实现ParallelSourceFunction接口:

package streaming;

import org.apache.flink.streaming.api.functions.source.ParallelSourceFunction;

/**
 * 自定义实现一个多并行度的Source
 */
public class MyParalleSource implements ParallelSourceFunction<Long> {

    private long count = 1L;
    private boolean isRunning = true;

    /**
     * 启动一个Source
     * 大部分情况下,都需要在这个run方法中实现一个循环,这样就可以产生数据了
     * @param sourceContext
     * @throws Exception
     */
    public void run(SourceContext<Long> sourceContext) throws Exception {
        while(isRunning){
            sourceContext.collect(count);
            count++;
            //每秒产生一条数据
            Thread.sleep(1000);
        }
    }

    /**
     * 取消一个cancel的时候会调用的方法
     */
    public void cancel() {
        isRunning = false;
    }
}

继承RichParallelSourceFunction:

package streaming;

import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.functions.source.RichParallelSourceFunction;

/**
 * 自定义实现一个支持多并行度的source
 * RichParallelSourceFunction会额外提供open和close方法
 * 如果在source中获取其他链接资源,那么可以在open方法中打开资源链接,在close中关闭资源链接
 */
public class MyRichParalleSource extends RichParallelSourceFunction<Long> {

    private long count = 1L;
    private boolean isRunning = true;

    /**
     * 启动一个Source
     * 大部分情况下,都需要在这个run方法中实现一个循环,这样就可以产生数据了
     * @param sourceContext
     * @throws Exception
     */
    public void run(SourceContext<Long> sourceContext) throws Exception {
        while(isRunning){
            sourceContext.collect(count);
            count++;
            //每秒产生一条数据
            Thread.sleep(1000);
        }
    }

    /**
     * 取消一个cancel的时候会调用的方法
     */
    public void cancel() {
        isRunning = false;
    }

    /**
     * 这个方法只会在最开始的时候被调用一次
     * 实现获取链接的代码
     * @param parameters
     * @throws Exception
     */
    public void open(Configuration parameters) throws Exception{
        System.out.println("open.......");
        super.open(parameters);
    }

    /**
     * 实现关闭链接的代码
     * @throws Exception
     */
    public void close() throws Exception{
        super.close();
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值