Search in sources :

Example 6 with RowChange

use of com.alibaba.otter.canal.protocol.CanalEntry.RowChange in project otter by alibaba.

the class MessageParser method internParse.

private List<EventData> internParse(Pipeline pipeline, Entry entry) {
    RowChange rowChange = null;
    try {
        rowChange = RowChange.parseFrom(entry.getStoreValue());
    } catch (Exception e) {
        throw new SelectException("parser of canal-event has an error , data:" + entry.toString(), e);
    }
    if (rowChange == null) {
        return null;
    }
    String schemaName = entry.getHeader().getSchemaName();
    String tableName = entry.getHeader().getTableName();
    EventType eventType = EventType.valueOf(rowChange.getEventType().name());
    // 处理下DDL操作
    if (eventType.isQuery()) {
        // 直接忽略query事件
        return null;
    }
    // 首先判断是否为系统表
    if (StringUtils.equalsIgnoreCase(pipeline.getParameters().getSystemSchema(), schemaName)) {
        // do noting
        if (eventType.isDdl()) {
            return null;
        }
        if (StringUtils.equalsIgnoreCase(pipeline.getParameters().getSystemDualTable(), tableName)) {
            // 心跳表数据直接忽略
            return null;
        }
    } else {
        if (eventType.isDdl()) {
            boolean notExistReturnNull = false;
            if (eventType.isRename()) {
                notExistReturnNull = true;
            }
            DataMedia dataMedia = ConfigHelper.findSourceDataMedia(pipeline, schemaName, tableName, notExistReturnNull);
            // DataMediaInfo;并且把CREATE/ALTER类型的事件丢弃掉.
            if (dataMedia != null && (eventType.isCreate() || eventType.isAlter() || eventType.isRename())) {
                DbDialect dbDialect = dbDialectFactory.getDbDialect(pipeline.getId(), (DbMediaSource) dataMedia.getSource());
                // 更新下meta信息
                dbDialect.reloadTable(schemaName, tableName);
            }
            boolean ddlSync = pipeline.getParameters().getDdlSync();
            if (ddlSync) {
                // 处理下ddl操作
                EventData eventData = new EventData();
                eventData.setSchemaName(schemaName);
                eventData.setTableName(tableName);
                eventData.setEventType(eventType);
                eventData.setExecuteTime(entry.getHeader().getExecuteTime());
                eventData.setSql(rowChange.getSql());
                eventData.setDdlSchemaName(rowChange.getDdlSchemaName());
                eventData.setTableId(dataMedia.getId());
                return Arrays.asList(eventData);
            } else {
                return null;
            }
        }
    }
    List<EventData> eventDatas = new ArrayList<EventData>();
    for (RowData rowData : rowChange.getRowDatasList()) {
        EventData eventData = internParse(pipeline, entry, rowChange, rowData);
        if (eventData != null) {
            eventDatas.add(eventData);
        }
    }
    return eventDatas;
}
Also used : RowData(com.alibaba.otter.canal.protocol.CanalEntry.RowData) RowChange(com.alibaba.otter.canal.protocol.CanalEntry.RowChange) EventType(com.alibaba.otter.shared.etl.model.EventType) DbDialect(com.alibaba.otter.node.etl.common.db.dialect.DbDialect) SelectException(com.alibaba.otter.node.etl.select.exceptions.SelectException) ArrayList(java.util.ArrayList) SelectException(com.alibaba.otter.node.etl.select.exceptions.SelectException) TransformException(com.alibaba.otter.node.etl.transform.exception.TransformException) DataMedia(com.alibaba.otter.shared.common.model.config.data.DataMedia) EventData(com.alibaba.otter.shared.etl.model.EventData)

Aggregations

RowChange (com.alibaba.otter.canal.protocol.CanalEntry.RowChange)6 Entry (com.alibaba.otter.canal.protocol.CanalEntry.Entry)5 RowData (com.alibaba.otter.canal.protocol.CanalEntry.RowData)5 EventType (com.alibaba.otter.canal.protocol.CanalEntry.EventType)4 AbstractCanalEventSinkTest (com.alibaba.otter.canal.parse.stub.AbstractCanalEventSinkTest)2 AbstractCanalLogPositionManager (com.alibaba.otter.canal.parse.stub.AbstractCanalLogPositionManager)2 AuthenticationInfo (com.alibaba.otter.canal.parse.support.AuthenticationInfo)2 EntryPosition (com.alibaba.otter.canal.protocol.position.EntryPosition)2 LogPosition (com.alibaba.otter.canal.protocol.position.LogPosition)2 CanalSinkException (com.alibaba.otter.canal.sink.exception.CanalSinkException)2 SelectException (com.alibaba.otter.node.etl.select.exceptions.SelectException)2 TransformException (com.alibaba.otter.node.etl.transform.exception.TransformException)2 EventData (com.alibaba.otter.shared.etl.model.EventData)2 InetSocketAddress (java.net.InetSocketAddress)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 List (java.util.List)2 Test (org.junit.Test)2 CanalParseException (com.alibaba.otter.canal.parse.exception.CanalParseException)1 TableIdNotFoundException (com.alibaba.otter.canal.parse.exception.TableIdNotFoundException)1