Flink 连接zeroMQ

废话不多说,思路如下:

1 实现 SourceFunction接口 中的run函数和cancel函数

推荐:Ctrl + Alt +B 发现 SocketTextStreamFunction也是实现当前接口,着这个SocketTextStreamFunction实现类实现即可

代码

本人暂且给其命名为 TcpTextStreamFuntion

package com.cetc.stream;

import org.apache.flink.streaming.api.functions.source.SocketTextStreamFunction;
import org.apache.flink.streaming.api.functions.source.SourceFunction;
import org.apache.flink.util.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.zeromq.ZMQ;
import java.net.Socket;

public class TcpTextStreamFuntion implements SourceFunction<String> {
    private static final long serialVersionUID = 1L;

    private static final Logger LOG = LoggerFactory.getLogger(SocketTextStreamFunction.class);

    /**
     * Default delay between successive connection attempts.
     */
    private static final int DEFAULT_CONNECTION_RETRY_SLEEP = 500;

    /**
     * Default connection timeout when connecting to the server socket (infinite).
     */
    private static final int CONNECTION_TIMEOUT_TIME = 0;


    private final String hostname;
    private final int port;

    private volatile boolean isRunning = true;
    private transient Socket currentSocket;

    public TcpTextStreamFuntion(String hostname, int port) {
        this.hostname = hostname;
        this.port = port;
    }

    public void run(SourceContext<String> ctx) throws Exception {

        ZMQ.Context context = ZMQ.context(1);
        ZMQ.Socket sub = context.socket(ZMQ.SUB);
        String path = "tcp://" + hostname + ":" + port;
        sub.connect(path);
        sub.subscribe("".getBytes());

        while (isRunning) {
            byte[] recv = sub.recv(0);
            String value = new String(recv);
            ctx.collect(value);
        }
    }

    public void cancel() {
        isRunning = false;
        Socket theSocket = this.currentSocket;
        if (theSocket != null) {
            IOUtils.closeSocket(theSocket);
        }
    }
}

测试效果:

client

package com.cetc.zeromq;


import com.cetc.entity.Student;
import org.zeromq.ZMQ;

import java.util.Random;


public class Server {
    public static void main(String[] args) throws InterruptedException {
        ZMQ.Context context = ZMQ.context(1);
        ZMQ.Socket pub = context.socket(ZMQ.PUB);
        pub.bind("tcp://*:7798");
        Random random = new Random();

        while (true) {
            Student stu = new Student("hello_" + random.nextGaussian(), random.nextDouble(), random.nextDouble(), random.nextInt());
            System.out.println("send:" + stu.toString());
            pub.send(stu.toString());
            Thread.sleep(1000L);
        }
    }
}

Student实体类

package com.cetc.entity;

import com.alibaba.fastjson.JSON;

public class Student {
    private String name;
    private double hight;
    private double weight;
    private int age;

    public Student() {
    }


    public Student(String name, double hight, double weight, int age) {
        this.name = name;
        this.hight = hight;
        this.weight = weight;
        this.age = age;
    }


    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getHight() {
        return hight;
    }

    public void setHight(double hight) {
        this.hight = hight;
    }

    public double getWeight() {
        return weight;
    }

    public void setWeight(double weight) {
        this.weight = weight;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return JSON.toJSONString(this);
    }
}

Flink Source端

package com.cetc.zeromq;

import com.cetc.stream.TcpTextStreamFuntion;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

public class FlinkConnect {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
        env.addSource(new TcpTextStreamFuntion("127.0.0.1",7798)).print();
        env.execute("hjj");
    }
}

Reponse

https://github.com/tangbiao098/FlinkConnectZeroMq.git

Gitgub上找到2份的flink连接zeroMQ的代码

https://github.com/amisev/ZMQSource

https://github.com/omaralvarez/flink-zeromq

So Far、All Over

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值