com.alibaba.otter.canal.parse.CanalEventParser Java Examples

The following examples show how to use com.alibaba.otter.canal.parse.CanalEventParser. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: AbstractCanalInstance.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化单个eventParser,不需要考虑group
 */
protected void startEventParserInternal(CanalEventParser eventParser, boolean isGroup) {
    if (eventParser instanceof AbstractEventParser) {
        AbstractEventParser abstractEventParser = (AbstractEventParser) eventParser;
        // 首先启动log position管理器
        CanalLogPositionManager logPositionManager = abstractEventParser.getLogPositionManager();
        if (!logPositionManager.isStart()) {
            logPositionManager.start();
        }
    }

    if (eventParser instanceof MysqlEventParser) {
        MysqlEventParser mysqlEventParser = (MysqlEventParser) eventParser;
        CanalHAController haController = mysqlEventParser.getHaController();

        if (haController instanceof HeartBeatHAController) {
            ((HeartBeatHAController) haController).setCanalHASwitchable(mysqlEventParser);
        }

        if (!haController.isStart()) {
            haController.start();
        }

    }
}
 
Example #2
Source File: AbstractMysqlEventParser.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
public void start() throws CanalParseException {
    if (enableTsdb) {
        if (tableMetaTSDB == null) {
            synchronized (CanalEventParser.class) {
                try {
                    // 设置当前正在加载的通道,加载spring查找文件时会用到该变量
                    System.setProperty("canal.instance.destination", destination);
                    // 初始化
                    tableMetaTSDB = tableMetaTSDBFactory.build(destination, tsdbSpringXml);
                } finally {
                    System.setProperty("canal.instance.destination", "");
                }
            }
        }
    }

    super.start();
}
 
Example #3
Source File: ParserCollector.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
@Override
public void register(CanalInstance instance) {
    final String destination = instance.getDestination();
    ParserMetricsHolder holder;
    CanalEventParser parser = instance.getEventParser();
    if (parser instanceof AbstractMysqlEventParser) {
        holder = singleHolder(destination, (AbstractMysqlEventParser)parser, "0");
    } else if (parser instanceof GroupEventParser) {
        holder = groupHolder(destination, (GroupEventParser)parser);
    } else {
        throw new IllegalArgumentException("CanalEventParser must be either AbstractMysqlEventParser or GroupEventParser.");
    }
    Preconditions.checkNotNull(holder);
    ParserMetricsHolder old = instances.put(destination, holder);
    if (old != null) {
        logger.warn("Remove stale ParserCollector for instance {}.", destination);
    }
}
 
Example #4
Source File: AbstractCanalInstance.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
protected void stopEventParserInternal(CanalEventParser eventParser) {
    if (eventParser instanceof AbstractEventParser) {
        AbstractEventParser abstractEventParser = (AbstractEventParser) eventParser;
        // 首先启动log position管理器
        CanalLogPositionManager logPositionManager = abstractEventParser.getLogPositionManager();
        if (logPositionManager.isStart()) {
            logPositionManager.stop();
        }
    }

    if (eventParser instanceof MysqlEventParser) {
        MysqlEventParser mysqlEventParser = (MysqlEventParser) eventParser;
        CanalHAController haController = mysqlEventParser.getHaController();
        if (haController.isStart()) {
            haController.stop();
        }
    }
}
 
Example #5
Source File: AbstractCanalInstance.java    From canal with Apache License 2.0 6 votes vote down vote up
/**
 * 初始化单个eventParser,不需要考虑group
 */
protected void startEventParserInternal(CanalEventParser eventParser, boolean isGroup) {
    if (eventParser instanceof AbstractEventParser) {
        AbstractEventParser abstractEventParser = (AbstractEventParser) eventParser;
        // 首先启动log position管理器
        CanalLogPositionManager logPositionManager = abstractEventParser.getLogPositionManager();
        if (!logPositionManager.isStart()) {
            logPositionManager.start();
        }
    }

    if (eventParser instanceof MysqlEventParser) {
        MysqlEventParser mysqlEventParser = (MysqlEventParser) eventParser;
        CanalHAController haController = mysqlEventParser.getHaController();

        if (haController instanceof HeartBeatHAController) {
            ((HeartBeatHAController) haController).setCanalHASwitchable(mysqlEventParser);
        }

        if (!haController.isStart()) {
            haController.start();
        }

    }
}
 
Example #6
Source File: AbstractCanalInstance.java    From canal with Apache License 2.0 6 votes vote down vote up
protected void stopEventParserInternal(CanalEventParser eventParser) {
    if (eventParser instanceof AbstractEventParser) {
        AbstractEventParser abstractEventParser = (AbstractEventParser) eventParser;
        // 首先启动log position管理器
        CanalLogPositionManager logPositionManager = abstractEventParser.getLogPositionManager();
        if (logPositionManager.isStart()) {
            logPositionManager.stop();
        }
    }

    if (eventParser instanceof MysqlEventParser) {
        MysqlEventParser mysqlEventParser = (MysqlEventParser) eventParser;
        CanalHAController haController = mysqlEventParser.getHaController();
        if (haController.isStart()) {
            haController.stop();
        }
    }
}
 
Example #7
Source File: AbstractCanalInstance.java    From canal-1.1.3 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean subscribeChange(ClientIdentity identity) {
    if (StringUtils.isNotEmpty(identity.getFilter())) {
        logger.info("subscribe filter change to " + identity.getFilter());
        AviaterRegexFilter aviaterFilter = new AviaterRegexFilter(identity.getFilter());

        boolean isGroup = (eventParser instanceof GroupEventParser);
        if (isGroup) {
            // 处理group的模式
            List<CanalEventParser> eventParsers = ((GroupEventParser) eventParser).getEventParsers();
            for (CanalEventParser singleEventParser : eventParsers) {// 需要遍历启动
                ((AbstractEventParser) singleEventParser).setEventFilter(aviaterFilter);
            }
        } else {
            ((AbstractEventParser) eventParser).setEventFilter(aviaterFilter);
        }

    }

    // filter的处理规则
    // a. parser处理数据过滤处理
    // b. sink处理数据的路由&分发,一份parse数据经过sink后可以分发为多份,每份的数据可以根据自己的过滤规则不同而有不同的数据
    // 后续内存版的一对多分发,可以考虑
    return true;
}
 
Example #8
Source File: AbstractMysqlEventParser.java    From canal with Apache License 2.0 6 votes vote down vote up
public void start() throws CanalParseException {
    if (enableTsdb) {
        if (tableMetaTSDB == null) {
            synchronized (CanalEventParser.class) {
                try {
                    // 设置当前正在加载的通道,加载spring查找文件时会用到该变量
                    System.setProperty("canal.instance.destination", destination);
                    // 初始化
                    tableMetaTSDB = tableMetaTSDBFactory.build(destination, tsdbSpringXml);
                } finally {
                    System.setProperty("canal.instance.destination", "");
                }
            }
        }
    }

    super.start();
}
 
Example #9
Source File: ParserCollector.java    From canal with Apache License 2.0 6 votes vote down vote up
@Override
public void register(CanalInstance instance) {
    final String destination = instance.getDestination();
    ParserMetricsHolder holder;
    CanalEventParser parser = instance.getEventParser();
    if (parser instanceof AbstractMysqlEventParser) {
        holder = singleHolder(destination, (AbstractMysqlEventParser)parser, "0");
    } else if (parser instanceof GroupEventParser) {
        holder = groupHolder(destination, (GroupEventParser)parser);
    } else {
        throw new IllegalArgumentException("CanalEventParser must be either AbstractMysqlEventParser or GroupEventParser.");
    }
    Preconditions.checkNotNull(holder);
    ParserMetricsHolder old = instances.put(destination, holder);
    if (old != null) {
        logger.warn("Remove stale ParserCollector for instance {}.", destination);
    }
}
 
Example #10
Source File: GroupEventParser.java    From canal with Apache License 2.0 5 votes vote down vote up
public void start() {
    super.start();
    // 统一启动
    for (CanalEventParser eventParser : eventParsers) {
        if (!eventParser.isStart()) {
            eventParser.start();
        }
    }
}
 
Example #11
Source File: AbstractCanalInstance.java    From canal with Apache License 2.0 5 votes vote down vote up
protected void afterStopEventParser(CanalEventParser eventParser) {

        boolean isGroup = (eventParser instanceof GroupEventParser);
        if (isGroup) {
            // 处理group的模式
            List<CanalEventParser> eventParsers = ((GroupEventParser) eventParser).getEventParsers();
            for (CanalEventParser singleEventParser : eventParsers) {// 需要遍历启动
                stopEventParserInternal(singleEventParser);
            }
        } else {
            stopEventParserInternal(eventParser);
        }
    }
 
Example #12
Source File: CanalInstanceWithManager.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
protected void initEventParser() {
    logger.info("init eventParser begin...");
    SourcingType type = parameters.getSourcingType();

    List<List<DataSourcing>> groupDbAddresses = parameters.getGroupDbAddresses();
    if (!CollectionUtils.isEmpty(groupDbAddresses)) {
        int size = groupDbAddresses.get(0).size();// 取第一个分组的数量,主备分组的数量必须一致
        List<CanalEventParser> eventParsers = new ArrayList<CanalEventParser>();
        for (int i = 0; i < size; i++) {
            List<InetSocketAddress> dbAddress = new ArrayList<InetSocketAddress>();
            SourcingType lastType = null;
            for (List<DataSourcing> groupDbAddress : groupDbAddresses) {
                if (lastType != null && !lastType.equals(groupDbAddress.get(i).getType())) {
                    throw new CanalException(String.format("master/slave Sourcing type is unmatch. %s vs %s",
                        lastType,
                        groupDbAddress.get(i).getType()));
                }

                lastType = groupDbAddress.get(i).getType();
                dbAddress.add(groupDbAddress.get(i).getDbAddress());
            }

            // 初始化其中的一个分组parser
            eventParsers.add(doInitEventParser(lastType, dbAddress));
        }

        if (eventParsers.size() > 1) { // 如果存在分组,构造分组的parser
            GroupEventParser groupEventParser = new GroupEventParser();
            groupEventParser.setEventParsers(eventParsers);
            this.eventParser = groupEventParser;
        } else {
            this.eventParser = eventParsers.get(0);
        }
    } else {
        // 创建一个空数据库地址的parser,可能使用了tddl指定地址,启动的时候才会从tddl获取地址
        this.eventParser = doInitEventParser(type, new ArrayList<InetSocketAddress>());
    }

    logger.info("init eventParser end! \n\t load CanalEventParser:{}", eventParser.getClass().getName());
}
 
Example #13
Source File: GroupEventParser.java    From canal with Apache License 2.0 5 votes vote down vote up
public void stop() {
    super.stop();
    // 统一关闭
    for (CanalEventParser eventParser : eventParsers) {
        if (eventParser.isStart()) {
            eventParser.stop();
        }
    }
}
 
Example #14
Source File: AbstractCanalInstance.java    From canal with Apache License 2.0 5 votes vote down vote up
protected void afterStartEventParser(CanalEventParser eventParser) {
    // 读取一下历史订阅的filter信息
    List<ClientIdentity> clientIdentitys = metaManager.listAllSubscribeInfo(destination);
    for (ClientIdentity clientIdentity : clientIdentitys) {
        subscribeChange(clientIdentity);
    }
}
 
Example #15
Source File: AbstractCanalInstance.java    From canal with Apache License 2.0 5 votes vote down vote up
protected void beforeStartEventParser(CanalEventParser eventParser) {

        boolean isGroup = (eventParser instanceof GroupEventParser);
        if (isGroup) {
            // 处理group的模式
            List<CanalEventParser> eventParsers = ((GroupEventParser) eventParser).getEventParsers();
            for (CanalEventParser singleEventParser : eventParsers) {// 需要遍历启动
                startEventParserInternal(singleEventParser, true);
            }
        } else {
            startEventParserInternal(eventParser, false);
        }
    }
 
Example #16
Source File: AbstractCanalInstance.java    From canal with Apache License 2.0 5 votes vote down vote up
@Override
public boolean subscribeChange(ClientIdentity identity) {
    if (StringUtils.isNotEmpty(identity.getFilter())) {
        logger.info("subscribe filter change to " + identity.getFilter());
        AviaterRegexFilter aviaterFilter = new AviaterRegexFilter(identity.getFilter());

        boolean isGroup = (eventParser instanceof GroupEventParser);
        if (isGroup) {
            // 处理group的模式
            List<CanalEventParser> eventParsers = ((GroupEventParser) eventParser).getEventParsers();
            for (CanalEventParser singleEventParser : eventParsers) {// 需要遍历启动
                if(singleEventParser instanceof AbstractEventParser) {
                    ((AbstractEventParser) singleEventParser).setEventFilter(aviaterFilter);
                }
            }
        } else {
            if(eventParser instanceof AbstractEventParser) {
                ((AbstractEventParser) eventParser).setEventFilter(aviaterFilter);
            }
        }

    }

    // filter的处理规则
    // a. parser处理数据过滤处理
    // b. sink处理数据的路由&分发,一份parse数据经过sink后可以分发为多份,每份的数据可以根据自己的过滤规则不同而有不同的数据
    // 后续内存版的一对多分发,可以考虑
    return true;
}
 
Example #17
Source File: CanalInstanceWithManager.java    From canal with Apache License 2.0 5 votes vote down vote up
protected void startEventParserInternal(CanalEventParser eventParser, boolean isGroup) {
    if (eventParser instanceof AbstractEventParser) {
        AbstractEventParser abstractEventParser = (AbstractEventParser) eventParser;
        abstractEventParser.setAlarmHandler(getAlarmHandler());
    }

    super.startEventParserInternal(eventParser, isGroup);
}
 
Example #18
Source File: CanalInstanceWithManager.java    From canal with Apache License 2.0 5 votes vote down vote up
protected void initEventParser() {
    logger.info("init eventParser begin...");
    SourcingType type = parameters.getSourcingType();

    List<List<DataSourcing>> groupDbAddresses = parameters.getGroupDbAddresses();
    if (!CollectionUtils.isEmpty(groupDbAddresses)) {
        int size = groupDbAddresses.get(0).size();// 取第一个分组的数量,主备分组的数量必须一致
        List<CanalEventParser> eventParsers = new ArrayList<CanalEventParser>();
        for (int i = 0; i < size; i++) {
            List<InetSocketAddress> dbAddress = new ArrayList<InetSocketAddress>();
            SourcingType lastType = null;
            for (List<DataSourcing> groupDbAddress : groupDbAddresses) {
                if (lastType != null && !lastType.equals(groupDbAddress.get(i).getType())) {
                    throw new CanalException(String.format("master/slave Sourcing type is unmatch. %s vs %s",
                        lastType,
                        groupDbAddress.get(i).getType()));
                }

                lastType = groupDbAddress.get(i).getType();
                dbAddress.add(groupDbAddress.get(i).getDbAddress());
            }

            // 初始化其中的一个分组parser
            eventParsers.add(doInitEventParser(lastType, dbAddress));
        }

        if (eventParsers.size() > 1) { // 如果存在分组,构造分组的parser
            GroupEventParser groupEventParser = new GroupEventParser();
            groupEventParser.setEventParsers(eventParsers);
            this.eventParser = groupEventParser;
        } else {
            this.eventParser = eventParsers.get(0);
        }
    } else {
        // 创建一个空数据库地址的parser,可能使用了tddl指定地址,启动的时候才会从tddl获取地址
        this.eventParser = doInitEventParser(type, new ArrayList<InetSocketAddress>());
    }

    logger.info("init eventParser end! \n\t load CanalEventParser:{}", eventParser.getClass().getName());
}
 
Example #19
Source File: CanalInstanceWithManager.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
protected void startEventParserInternal(CanalEventParser eventParser, boolean isGroup) {
    if (eventParser instanceof AbstractEventParser) {
        AbstractEventParser abstractEventParser = (AbstractEventParser) eventParser;
        abstractEventParser.setAlarmHandler(getAlarmHandler());
    }

    super.startEventParserInternal(eventParser, isGroup);
}
 
Example #20
Source File: GroupEventParser.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public void stop() {
    super.stop();
    // 统一关闭
    for (CanalEventParser eventParser : eventParsers) {
        if (eventParser.isStart()) {
            eventParser.stop();
        }
    }
}
 
Example #21
Source File: GroupEventParser.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
public void start() {
    super.start();
    // 统一启动
    for (CanalEventParser eventParser : eventParsers) {
        if (!eventParser.isStart()) {
            eventParser.start();
        }
    }
}
 
Example #22
Source File: AbstractCanalInstance.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
protected void beforeStartEventParser(CanalEventParser eventParser) {

        boolean isGroup = (eventParser instanceof GroupEventParser);
        if (isGroup) {
            // 处理group的模式
            List<CanalEventParser> eventParsers = ((GroupEventParser) eventParser).getEventParsers();
            for (CanalEventParser singleEventParser : eventParsers) {// 需要遍历启动
                startEventParserInternal(singleEventParser, true);
            }
        } else {
            startEventParserInternal(eventParser, false);
        }
    }
 
Example #23
Source File: AbstractCanalInstance.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
protected void afterStopEventParser(CanalEventParser eventParser) {

        boolean isGroup = (eventParser instanceof GroupEventParser);
        if (isGroup) {
            // 处理group的模式
            List<CanalEventParser> eventParsers = ((GroupEventParser) eventParser).getEventParsers();
            for (CanalEventParser singleEventParser : eventParsers) {// 需要遍历启动
                stopEventParserInternal(singleEventParser);
            }
        } else {
            stopEventParserInternal(eventParser);
        }
    }
 
Example #24
Source File: AbstractCanalInstance.java    From canal-1.1.3 with Apache License 2.0 5 votes vote down vote up
protected void afterStartEventParser(CanalEventParser eventParser) {
    // 读取一下历史订阅的filter信息
    List<ClientIdentity> clientIdentitys = metaManager.listAllSubscribeInfo(destination);
    for (ClientIdentity clientIdentity : clientIdentitys) {
        subscribeChange(clientIdentity);
    }
}
 
Example #25
Source File: CanalInstanceWithSpring.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public void setEventParser(CanalEventParser eventParser) {
    this.eventParser = eventParser;
}
 
Example #26
Source File: GroupEventParser.java    From canal-1.1.3 with Apache License 2.0 4 votes vote down vote up
public List<CanalEventParser> getEventParsers() {
    return eventParsers;
}
 
Example #27
Source File: GroupEventParser.java    From canal with Apache License 2.0 4 votes vote down vote up
public List<CanalEventParser> getEventParsers() {
    return eventParsers;
}
 
Example #28
Source File: GroupEventParser.java    From canal with Apache License 2.0 4 votes vote down vote up
public void removeEventParser(CanalEventParser eventParser) {
    eventParsers.remove(eventParser);
}
 
Example #29
Source File: GroupEventParser.java    From canal with Apache License 2.0 4 votes vote down vote up
public void addEventParser(CanalEventParser eventParser) {
    if (!eventParsers.contains(eventParser)) {
        eventParsers.add(eventParser);
    }
}
 
Example #30
Source File: GroupEventParser.java    From canal with Apache License 2.0 4 votes vote down vote up
public void setEventParsers(List<CanalEventParser> eventParsers) {
    this.eventParsers = eventParsers;
}