flink学习(6)——自定义source和kafka

概述

SourceFunction:非并行数据源(并行度只能=1) --接口

RichSourceFunction:多功能非并行数据源(并行度只能=1) --类

ParallelSourceFunction:并行数据源(并行度能够>=1) --接口

RichParallelSourceFunction:多功能并行数据源(并行度能够>=1) --类 【建议使用的】

------Rich 字样代表富有,在编程中,富有代表可以调用的方法很多,功能很全的意思。

基础案例

复制代码
package com.bigdata.day02;

//1、SourceFunction
// public class ZidingyiSource implements SourceFunction<Student> {
//2、RichSourceFunction
// public class ZidingyiSource extends RichSourceFunction<Student> {
//3、ParallelSourceFunction
//public class ZidingyiSource implements ParallelSourceFunction<Student> {
//4、RichParallelSourceFunction
//public class ZidingyiSource extends RichParallelSourceFunction<Student> {
// 推荐的
public class ZidingyiSource extends RichParallelSourceFunction<Student> {

    // ctrl + o
    private final Random random = new Random();
    private boolean flag = true;

    // 现在不用
    @Override
    public void open(Configuration parameters) throws Exception {
        System.out.println("实现一些资源的开启");
    }

    // 现在不用
    @Override
    public void close() throws Exception {
        System.out.println("实现一些资源的关闭");
    }


    @Override
    public void run(SourceContext<Student> sourceContext) throws Exception {
        while (flag){

            String stu_id = UUID.randomUUID().toString();
            String stu_name = "Student_"+stu_id;
            int stu_age = random.nextInt(8)+10;
            long stu_timestamp = System.currentTimeMillis();
            Student student = new Student(stu_id,stu_name,stu_age,stu_timestamp);
            sourceContext.collect(student);
            Thread.sleep(1000);
        }
    }

    // 具体什么时候 会调用还不知道
    @Override
    public void cancel() {
        flag = false;
        System.out.println("停止运行");
    }
}


//调用
public class ZiDingYi {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        // add + new 
        DataStreamSource<Student> studentDataStreamSource = env.addSource(new ZidingyiSource());
        int parallelism = studentDataStreamSource.getParallelism();
        System.out.println(parallelism);
        // print之前与之后的并行度是不同的
        studentDataStreamSource.print().setParallelism(1);
        env.execute();
    }
}

cancel+open+close的调用时机

复制代码
package com.bigdata.day02;

import java.util.Objects;

/*
* 1、这几个方法都会按照并行度调用多次 调度的次数 按照studentDataStreamSource的并行度
*
*/

public class ZiDingYi {
    public static void main(String[] args) throws Exception {
    // 在上面案例的基础上实现
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        
        DataStreamSource<Student> studentDataStreamSource = env.addSource(new ZidingyiSource());
        
        // 此时就只会调用一次了
        studentDataStreamSource.setParallelism(1);
        
        // 此时打印也会有多个并行度(8个cpu)
        studentDataStreamSource.print();
        

        // 异步调用 此时会调用open方法
        JobExecutionResult execute = env.execute();
        JobClient flink_job = env.executeAsync("Flink Job");
        Thread.sleep(3000);
        // 此时会调用 cancel 和 close 
        flink_job.cancel();
    }
}

kafkaSource

复制代码
package com.bigdata.day02;

import java.util.Properties;

public class KafkaSource {
    public static void main(String[] args) throws Exception{
        //env
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        
        // properties 
        Properties properties = new Properties();
        properties.setProperty("bootstrap.servers", "bigdata01:9092");
        properties.setProperty("group.id", "g1");
        
        // consumer
        FlinkKafkaConsumer<String> consumer= new FlinkKafkaConsumer<String>("yhedu",new SimpleStringSchema(),properties);
        
        // source
        DataStreamSource<String> dataStreamSource = env.addSource(consumer);
        dataStreamSource.filter(new FilterFunction<String>() {
            @Override
            public boolean filter(String s) throws Exception {
                return s.contains("success");
            }
        }).print();

        env.execute();

    }
}
相关推荐
GIS数据转换器15 分钟前
延凡智慧水务系统:引领行业变革的智能引擎
大数据·人工智能·无人机·智慧城市
正经教主33 分钟前
【docker基础】0、系统学习docker之总计划
学习·docker·容器
2601_949539451 小时前
家用新能源 SUV 核心技术科普:后排娱乐、空间工程与混动可靠性解析
大数据·网络·人工智能·算法·机器学习
莫叫石榴姐1 小时前
字节广告数开一面 | 实习
大数据·数据仓库·面试
不知名的老吴1 小时前
计算机基础第一阶段:核心基础学习
学习
skywalk81631 小时前
请学习kotti的前端(kotti其实是没有分离的前端的)实现,做到形似kotti那样的前端页面。
前端·学习
red_redemption1 小时前
自由学习记录(156)
学习
溟有常青木2 小时前
Jupyter notebook学习容易忘的点
学习·jupyter
大虫刷题2 小时前
华为认证(HCIP-AI)五大分类,有何区别及学习难度和从事职业方向
人工智能·学习
T06205142 小时前
【面板数据】地级市人力资本水平测算数据(1990-2024年)
大数据