Search in sources :

Example 1 with RocketMQSink

use of org.apache.rocketmq.flink.RocketMQSink in project rocketmq-externals by apache.

the class RocketMQFlinkExample method main.

public static void main(String[] args) {
    StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
    // enable checkpoint
    env.enableCheckpointing(3000);
    Properties consumerProps = new Properties();
    consumerProps.setProperty(RocketMQConfig.NAME_SERVER_ADDR, "localhost:9876");
    consumerProps.setProperty(RocketMQConfig.CONSUMER_GROUP, "c002");
    consumerProps.setProperty(RocketMQConfig.CONSUMER_TOPIC, "flink-source2");
    Properties producerProps = new Properties();
    producerProps.setProperty(RocketMQConfig.NAME_SERVER_ADDR, "localhost:9876");
    env.addSource(new RocketMQSource(new SimpleKeyValueDeserializationSchema("id", "address"), consumerProps)).name("rocketmq-source").setParallelism(2).process(new ProcessFunction<Map, Map>() {

        @Override
        public void processElement(Map in, Context ctx, Collector<Map> out) throws Exception {
            HashMap result = new HashMap();
            result.put("id", in.get("id"));
            String[] arr = in.get("address").toString().split("\\s+");
            result.put("province", arr[arr.length - 1]);
            out.collect(result);
        }
    }).name("upper-processor").setParallelism(2).addSink(new RocketMQSink(new SimpleKeyValueSerializationSchema("id", "province"), new DefaultTopicSelector("flink-sink2"), producerProps).withBatchFlushOnCheckpoint(true)).name("rocketmq-sink").setParallelism(2);
    try {
        env.execute("rocketmq-flink-example");
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : HashMap(java.util.HashMap) SimpleKeyValueDeserializationSchema(org.apache.rocketmq.flink.common.serialization.SimpleKeyValueDeserializationSchema) Properties(java.util.Properties) ProcessFunction(org.apache.flink.streaming.api.functions.ProcessFunction) RocketMQSink(org.apache.rocketmq.flink.RocketMQSink) Collector(org.apache.flink.util.Collector) StreamExecutionEnvironment(org.apache.flink.streaming.api.environment.StreamExecutionEnvironment) DefaultTopicSelector(org.apache.rocketmq.flink.common.selector.DefaultTopicSelector) RocketMQSource(org.apache.rocketmq.flink.RocketMQSource) SimpleKeyValueSerializationSchema(org.apache.rocketmq.flink.common.serialization.SimpleKeyValueSerializationSchema) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

HashMap (java.util.HashMap)1 Map (java.util.Map)1 Properties (java.util.Properties)1 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)1 ProcessFunction (org.apache.flink.streaming.api.functions.ProcessFunction)1 Collector (org.apache.flink.util.Collector)1 RocketMQSink (org.apache.rocketmq.flink.RocketMQSink)1 RocketMQSource (org.apache.rocketmq.flink.RocketMQSource)1 DefaultTopicSelector (org.apache.rocketmq.flink.common.selector.DefaultTopicSelector)1 SimpleKeyValueDeserializationSchema (org.apache.rocketmq.flink.common.serialization.SimpleKeyValueDeserializationSchema)1 SimpleKeyValueSerializationSchema (org.apache.rocketmq.flink.common.serialization.SimpleKeyValueSerializationSchema)1