Search in sources :

Example 51 with Collector

use of org.apache.flink.util.Collector 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

Collector (org.apache.flink.util.Collector)51 Test (org.junit.Test)38 Tuple2 (org.apache.flink.api.java.tuple.Tuple2)20 ExecutionConfig (org.apache.flink.api.common.ExecutionConfig)16 Configuration (org.apache.flink.configuration.Configuration)16 ExecutionEnvironment (org.apache.flink.api.java.ExecutionEnvironment)15 StreamExecutionEnvironment (org.apache.flink.streaming.api.environment.StreamExecutionEnvironment)15 RuntimeContext (org.apache.flink.api.common.functions.RuntimeContext)14 TimeWindow (org.apache.flink.streaming.api.windowing.windows.TimeWindow)11 HashMap (java.util.HashMap)9 ArrayList (java.util.ArrayList)8 FlatMapFunction (org.apache.flink.api.common.functions.FlatMapFunction)8 Plan (org.apache.flink.api.common.Plan)7 HashSet (java.util.HashSet)6 RichGroupReduceFunction (org.apache.flink.api.common.functions.RichGroupReduceFunction)6 Tuple3 (org.apache.flink.api.java.tuple.Tuple3)6 Map (java.util.Map)5 GroupReduceFunction (org.apache.flink.api.common.functions.GroupReduceFunction)5 TaskInfo (org.apache.flink.api.common.TaskInfo)4 CoGroupFunction (org.apache.flink.api.common.functions.CoGroupFunction)4