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();

    }
}
相关推荐
真的想上岸啊3 分钟前
学习C++、QT---21(QT中QFile库的QFile读取文件、写入文件的讲解)
c++·qt·学习
之歆24 分钟前
Python-正则表达式-信息提取-滑动窗口-数据分发-文件加载及分析器-浏览器分析-学习笔记
python·学习·正则表达式
rui锐rui32 分钟前
大数据学习7:Azkaban调度器
学习
Leo.yuan1 小时前
数据分析师如何构建自己的底层逻辑?
大数据·数据仓库·人工智能·数据挖掘·数据分析
Aurora_NeAr1 小时前
Apache Iceberg数据湖高级特性及性能调优
大数据·后端
SoFlu软件机器人1 小时前
飞算科技正在撬动各行业数字化转型的深层变革
大数据·科技
文艺倾年2 小时前
【八股消消乐】浅尝Kafka性能优化
分布式·性能优化·kafka
北风toto3 小时前
python学习DataFrame数据结构
数据结构·python·学习
samLi06204 小时前
【工具变量】全国省市区县土地出让结果公告数据(2000-2024年)
大数据
qq_386322694 小时前
华为网路设备学习-26(BGP协议 一)
学习