Search in sources :

Example 1 with SimpleKeyValueSerializationSchema

use of org.apache.rocketmq.flink.common.serialization.SimpleKeyValueSerializationSchema in project rocketmq-externals by apache.

the class RocketMQSinkTest method setUp.

@Before
public void setUp() throws Exception {
    KeyValueSerializationSchema serializationSchema = new SimpleKeyValueSerializationSchema("id", "name");
    TopicSelector topicSelector = new DefaultTopicSelector("tpc");
    Properties props = new Properties();
    rocketMQSink = new RocketMQSink(serializationSchema, topicSelector, props);
    producer = mock(DefaultMQProducer.class);
    setFieldValue(rocketMQSink, "producer", producer);
}
Also used : DefaultTopicSelector(org.apache.rocketmq.flink.common.selector.DefaultTopicSelector) TopicSelector(org.apache.rocketmq.flink.common.selector.TopicSelector) KeyValueSerializationSchema(org.apache.rocketmq.flink.common.serialization.KeyValueSerializationSchema) SimpleKeyValueSerializationSchema(org.apache.rocketmq.flink.common.serialization.SimpleKeyValueSerializationSchema) DefaultTopicSelector(org.apache.rocketmq.flink.common.selector.DefaultTopicSelector) SimpleKeyValueSerializationSchema(org.apache.rocketmq.flink.common.serialization.SimpleKeyValueSerializationSchema) Properties(java.util.Properties) DefaultMQProducer(org.apache.rocketmq.client.producer.DefaultMQProducer) Before(org.junit.Before)

Example 2 with SimpleKeyValueSerializationSchema

use of org.apache.rocketmq.flink.common.serialization.SimpleKeyValueSerializationSchema 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

Properties (java.util.Properties)2 DefaultTopicSelector (org.apache.rocketmq.flink.common.selector.DefaultTopicSelector)2 SimpleKeyValueSerializationSchema (org.apache.rocketmq.flink.common.serialization.SimpleKeyValueSerializationSchema)2 HashMap (java.util.HashMap)1 Map (java.util.Map)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 DefaultMQProducer (org.apache.rocketmq.client.producer.DefaultMQProducer)1 RocketMQSink (org.apache.rocketmq.flink.RocketMQSink)1 RocketMQSource (org.apache.rocketmq.flink.RocketMQSource)1 TopicSelector (org.apache.rocketmq.flink.common.selector.TopicSelector)1 KeyValueSerializationSchema (org.apache.rocketmq.flink.common.serialization.KeyValueSerializationSchema)1 SimpleKeyValueDeserializationSchema (org.apache.rocketmq.flink.common.serialization.SimpleKeyValueDeserializationSchema)1 Before (org.junit.Before)1