废话不多说,思路如下:
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的代码
So Far、All Over