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

    }
}
相关推荐
知识分享小能手1 小时前
React学习教程,从入门到精通, React 属性(Props)语法知识点与案例详解(14)
前端·javascript·vue.js·学习·react.js·vue·react
qq_508823403 小时前
金融量化指标--2Alpha 阿尔法
大数据·人工智能
好家伙VCC4 小时前
数学建模模型 全网最全 数学建模常见算法汇总 含代码分析讲解
大数据·嵌入式硬件·算法·数学建模
茯苓gao4 小时前
STM32G4 速度环开环,电流环闭环 IF模式建模
笔记·stm32·单片机·嵌入式硬件·学习
是誰萆微了承諾4 小时前
【golang学习笔记 gin 】1.2 redis 的使用
笔记·学习·golang
DKPT5 小时前
Java内存区域与内存溢出
java·开发语言·jvm·笔记·学习
aaaweiaaaaaa5 小时前
HTML和CSS学习
前端·css·学习·html
c&0xff006 小时前
Flink反压问题
网络·flink
看海天一色听风起雨落6 小时前
Python学习之装饰器
开发语言·python·学习
2301_781668616 小时前
Elasticsearch 02
大数据·elasticsearch·搜索引擎