sequence
This paper mainly studies Canal eventFilter
CanalEventFilter
Canal – 1.1.4 / filter/SRC/main/Java/com/alibaba/otter/canal/filter/CanalEventFilter. Java
public interface CanalEventFilter<T> {
boolean filter(T event) throws CanalFilterException;
}
Copy the code
- The CanalEventFilter interface defines the filter method
AviaterELFilter
Canal – 1.1.4 / filter/SRC/main/Java/com/alibaba/otter/canal/filter/aviater/AviaterELFilter. Java
public class AviaterELFilter implements CanalEventFilter<CanalEntry.Entry> {
public static final String ROOT_KEY = "entry";
private String expression;
public AviaterELFilter(String expression){
this.expression = expression;
}
public boolean filter(CanalEntry.Entry entry) throws CanalFilterException {
if (StringUtils.isEmpty(expression)) {
return true;
}
Map<String, Object> env = new HashMap<String, Object>();
env.put(ROOT_KEY, entry);
return(Boolean) AviatorEvaluator.execute(expression, env); }}Copy the code
- AviaterELFilter implements the CanalEventFilter interface. Its constructor receives the expression parameter and its filter method evaluates the result through aviatoreValuator.execute (expression, env)
AviaterSimpleFilter
Canal – 1.1.4 / filter/SRC/main/Java/com/alibaba/otter/canal/filter/aviater/AviaterSimpleFilter. Java
public class AviaterSimpleFilter implements CanalEventFilter<String> {
private static final String SPLIT = ",";
private static final String FILTER_EXPRESSION = "include(list,target)";
private final Expression exp = AviatorEvaluator.compile(FILTER_EXPRESSION, true);
private final List<String> list;
public AviaterSimpleFilter(String filterExpression){
if (StringUtils.isEmpty(filterExpression)) {
list = new ArrayList<String>();
} else {
String[] ss = filterExpression.toLowerCase().split(SPLIT);
list = Arrays.asList(ss);
}
}
public boolean filter(String filtered) throws CanalFilterException {
if (list.isEmpty()) {
return true;
}
if (StringUtils.isEmpty(filtered)) {
return true;
}
Map<String, Object> env = new HashMap<String, Object>();
env.put("list", list);
env.put("target", filtered.toLowerCase());
return(Boolean) exp.execute(env); }}Copy the code
- AviaterSimpleFilter implements the CanalEventFilter interface; It defines the Expression property, whose value is aviatoreValuator.pile (FILTER_EXPRESSION, true); Its constructor receives filterExpression; The filter method returns the result via exp. Execute (env)
AviaterRegexFilter
Canal – 1.1.4 / filter/SRC/main/Java/com/alibaba/otter/canal/filter/aviater/AviaterRegexFilter. Java
public class AviaterRegexFilter implements CanalEventFilter<String> {
private static final String SPLIT = ",";
private static final String PATTERN_SPLIT = "|";
private static final String FILTER_EXPRESSION = "regex(pattern,target)";
private static final RegexFunction regexFunction = new RegexFunction();
private final Expression exp = AviatorEvaluator.compile(FILTER_EXPRESSION, true);
static {
AviatorEvaluator.addFunction(regexFunction);
}
private static final Comparator<String> COMPARATOR = new StringComparator();
final private String pattern;
final private boolean defaultEmptyValue;
public AviaterRegexFilter(String pattern){
this(pattern, true);
}
public AviaterRegexFilter(String pattern, boolean defaultEmptyValue){
this.defaultEmptyValue = defaultEmptyValue;
List<String> list = null;
if (StringUtils.isEmpty(pattern)) {
list = new ArrayList<String>();
} else{ String[] ss = StringUtils.split(pattern, SPLIT); list = Arrays.asList(ss); } / / sorting of the pattern from long to short / / because foo | matching foot foot will go wrong, because the foot after matching foo, returns the foo. But foo is not the same length as foot //; collections.sort (list, COMPARATOR); List = completionPattern(list); this.pattern = StringUtils.join(list, PATTERN_SPLIT); } public boolean filter(String filtered) throws CanalFilterException {if (StringUtils.isEmpty(pattern)) {
return defaultEmptyValue;
}
if (StringUtils.isEmpty(filtered)) {
return defaultEmptyValue;
}
Map<String, Object> env = new HashMap<String, Object>();
env.put("pattern", pattern);
env.put("target", filtered.toLowerCase());
return(Boolean) exp.execute(env); } / /... @Override public StringtoString() {
returnpattern; }}Copy the code
- AviaterRegexFilter implements the CanalEventFilter interface; It defines the Expression property, whose value is aviatoreValuator.pile (FILTER_EXPRESSION, true); Its constructor receives the pattern argument, which it sorts and corrects first. The filter method returns the result via exp. Execute (env)
summary
The CanalEventFilter interface defines the filter method. Its three implementation classes are Respectively AviaterELFilter, AviaterSimpleFilter, AviaterRegexFilter
doc
- CanalEventFilter