Skip to content

Instantly share code, notes, and snippets.

@alpinegizmo
Created December 17, 2021 09:31
Show Gist options
  • Save alpinegizmo/f134e508f9de8e4cc238cae7b571ffc8 to your computer and use it in GitHub Desktop.
Save alpinegizmo/f134e508f9de8e4cc238cae7b571ffc8 to your computer and use it in GitHub Desktop.
Matching a simple pattern using Flink's CEP library
/*
* Copyright 2021 Ververica GmbH
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.ververica.flink.example;
import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.cep.CEP;
import org.apache.flink.cep.PatternStream;
import org.apache.flink.cep.functions.PatternProcessFunction;
import org.apache.flink.cep.pattern.Pattern;
import org.apache.flink.cep.pattern.conditions.SimpleCondition;
import org.apache.flink.streaming.api.datastream.DataStream;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.util.Collector;
import java.util.List;
import java.util.Map;
public class CEPPatternAB {
public static void main(String[] args) throws Exception {
StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();
DataStream<String> stringInputStream = env
.fromElements("A1", "C", "A2", "A3", "B", "E")
.assignTimestampsAndWatermarks(
WatermarkStrategy // using ingestion time for this example
.<String>forMonotonousTimestamps()
.withTimestampAssigner((e, t) -> System.currentTimeMillis()));
Pattern<String, ?> pattern = Pattern.<String>begin("A")
.where(new SimpleCondition<String>() {
@Override
public boolean filter(String s) throws Exception {
return s.startsWith("A");
}
}).next("B") // next enforces strict contiguity
.where(new SimpleCondition<String>() {
@Override
public boolean filter(String s) throws Exception {
return s.equals("B");
}
});
PatternStream<String> patternStream = CEP.pattern(stringInputStream, pattern);
DataStream<String> processed = patternStream.process(new PatternProcessFunction<String, String>() {
@Override
public void processMatch(
Map<String, List<String>> map,
Context context,
Collector<String> out) throws Exception {
out.collect(map.toString());
}
});
processed.printToErr(); // making the results easier to find amidst a sea of logging
env.execute();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment