finalStreamExecutionEnvironmentenv=StreamExecutionEnvironment.getExecutionEnvironment();// checkpointing is required for exactly-once or at-least-once guaranteesenv.enableCheckpointing(...);finalRMQConnectionConfigconnectionConfig=newRMQConnectionConfig.Builder().setHost("localhost").setPort(5000)....build();finalDataStream<String>stream=env.addSource(newRMQSource<String>(connectionConfig,// config for the RabbitMQ connection"queueName",// name of the RabbitMQ queue to consumetrue,// use correlation ids; can be false if only at-least-once is requirednewSimpleStringSchema()))// deserialization schema to turn messages into Java objects.setParallelism(1);// non-parallel source is only required for exactly-once

valenv=StreamExecutionEnvironment.getExecutionEnvironment// checkpointing is required for exactly-once or at-least-once guarantees
env.enableCheckpointing(...)valconnectionConfig=newRMQConnectionConfig.Builder().setHost("localhost").setPort(5000)....buildvalstream=env.addSource(newRMQSource[String](connectionConfig,// config for the RabbitMQ connection
"queueName",// name of the RabbitMQ queue to consume
true,// use correlation ids; can be false if only at-least-once is required
newSimpleStringSchema))// deserialization schema to turn messages into Java objects
.setParallelism(1)//non-parallelsourceisonlyrequiredforexactly-once

RabbitMQ Sink

该连接器提供了一个 RMQSink 类，用来向 RabbitMQ 队列发送数据。下面是设置 RabbitMQ sink 的代码示例：

finalDataStream<String>stream=...finalRMQConnectionConfigconnectionConfig=newRMQConnectionConfig.Builder().setHost("localhost").setPort(5000)....build();stream.addSink(newRMQSink<String>(connectionConfig,// config for the RabbitMQ connection"queueName",// name of the RabbitMQ queue to send messages tonewSimpleStringSchema()));// serialization schema to turn Java objects to messages

valstream:DataStream[String]=...valconnectionConfig=newRMQConnectionConfig.Builder().setHost("localhost").setPort(5000)....buildstream.addSink(newRMQSink[String](connectionConfig,// config for the RabbitMQ connection
"queueName",// name of the RabbitMQ queue to send messages to
newSimpleStringSchema))//serializationschematoturnJavaobjectstomessages