Java Code Examples for com.alibaba.otter.canal.protocol.CanalEntry#RowData

The following examples show how to use com.alibaba.otter.canal.protocol.CanalEntry#RowData . 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: BinlogEventSink.java    From jlogstash-input-plugin with Apache License 2.0 6 votes vote down vote up
private void processRowChange(CanalEntry.RowChange rowChange, String schema, String table, long ts) {
    CanalEntry.EventType eventType = rowChange.getEventType();

    if(!binlog.accept(eventType.toString())) {
        return;
    }

    for(CanalEntry.RowData rowData : rowChange.getRowDatasList()) {
        Map<String,Object> event = new HashMap<>();
        Map<String,Object> message = new HashMap<>();
        message.put("type", eventType.toString());
        message.put("schema", schema);
        message.put("table", table);
        message.put("ts", ts);
        message.put("before", processColumnList(rowData.getBeforeColumnsList()));
        message.put("after", processColumnList(rowData.getAfterColumnsList()));
        event.put("message", message);
        binlog.process(event);
    }

}
 
Example 2
Source File: DmlCanalEventListener.java    From spring-boot-starter-canal with MIT License 6 votes vote down vote up
@Override
default void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
    Objects.requireNonNull(eventType);
    switch (eventType) {
        case INSERT:
            onInsert(rowData);
            break;
        case UPDATE:
            onUpdate(rowData);
            break;
        case DELETE:
            onDelete(rowData);
            break;
        default:
            break;
    }
}
 
Example 3
Source File: AbstractBasicMessageTransponder.java    From spring-boot-starter-canal with MIT License 6 votes vote down vote up
/**
 * distribute to annotation listener interfaces
 *
 * @param destination destination
 * @param schemaName schema
 * @param tableName table name
 * @param eventType event type
 * @param rowData row data
 */
protected void distributeByAnnotation(String destination,
                                    String schemaName,
                                    String tableName,
                                    CanalEntry.EventType eventType,
                                    CanalEntry.RowData rowData) {
    //invoke the listeners
    annoListeners.forEach(point -> point
            .getInvokeMap()
            .entrySet()
            .stream()
            .filter(getAnnotationFilter(destination, schemaName, tableName, eventType))
            .forEach(entry -> {
                Method method = entry.getKey();
                method.setAccessible(true);
                try {
                    Object[] args = getInvokeArgs(method, eventType, rowData);
                    method.invoke(point.getTarget(), args);
                } catch (Exception e) {
                    logger.error("{}: Error occurred when invoke the listener's interface! class:{}, method:{}",
                            Thread.currentThread().getName(),
                            point.getTarget().getClass().getName(), method.getName());
                }
            }));
}
 
Example 4
Source File: TotoroTransForm.java    From canal-elasticsearch with Apache License 2.0 5 votes vote down vote up
private ElasticsearchMetadata.EsEntry getElasticsearchMetadata(CanalEntry.Entry entry) throws InvalidProtocolBufferException {

        final String database = entry.getHeader().getSchemaName(); // => index
        final String table = entry.getHeader().getTableName();// => type
        final CanalEntry.RowChange change = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
        final List<CanalEntry.RowData> rowDataList = change.getRowDatasList();

        CanalEntry.EventType eventType = entry.getHeader().getEventType();
        final int esEventType = esAdapter.getEsEventType(eventType);


        EsRowDataArrayList esRowDataList = TotoroObjectPool.esRowDataArrayList();

        for (CanalEntry.RowData rowData : rowDataList) {
            List<CanalEntry.Column> columnList = esAdapter.getColumnList(esEventType, rowData);
            ElasticsearchMetadata.EsRowData esRowData = TotoroObjectPool.esRowData();
            EsColumnHashMap columnMap = TotoroObjectPool.esColumnHashMap();
            columnList.forEach(column -> columnMap.put(column.getName(), column.getValue()));
            esRowData.setRowData(columnMap);
            esRowData.setIdColumn(esAdapter.getEsIdColumn(database, table));//获取es对应的id Column
            esRowDataList.add(esRowData);
        }

        ElasticsearchMetadata.EsEntry esEntry = TotoroObjectPool.esEntry();

        esEntry.setIndex(database)
                .setType(table)
                .setEsRowDatas(esRowDataList)
                .setEventType(esEventType);

        return esEntry;
    }
 
Example 5
Source File: SimpleEsAdapter.java    From canal-elasticsearch with Apache License 2.0 5 votes vote down vote up
@Override
public List<CanalEntry.Column> getColumnList(int esEventType, CanalEntry.RowData rowData) {

    List<CanalEntry.Column> columnList;
    if (esEventType == ElasticsearchMetadata.DELETE) {
        columnList = rowData.getBeforeColumnsList();
    } else {
        columnList = rowData.getAfterColumnsList();
    }

    return columnList;

}
 
Example 6
Source File: AbstractBasicMessageTransponder.java    From spring-boot-starter-canal with MIT License 5 votes vote down vote up
/**
 * distribute to listener interfaces
 *
 * @param eventType eventType
 * @param rowData rowData
 */
protected void distributeByImpl(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
    if (listeners != null) {
        for (CanalEventListener listener : listeners) {
            listener.onEvent(eventType, rowData);
        }
    }
}
 
Example 7
Source File: AbstractBasicMessageTransponder.java    From spring-boot-starter-canal with MIT License 5 votes vote down vote up
@Override
protected void distributeEvent(Message message) {
    List<CanalEntry.Entry> entries = message.getEntries();
    for (CanalEntry.Entry entry : entries) {
        //ignore the transaction operations
        List<CanalEntry.EntryType> ignoreEntryTypes = getIgnoreEntryTypes();
        if (ignoreEntryTypes != null
                && ignoreEntryTypes.stream().anyMatch(t -> entry.getEntryType() == t)) {
            continue;
        }
        CanalEntry.RowChange rowChange;
        try {
            rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
        } catch (Exception e) {
            throw new CanalClientException("ERROR ## parser of event has an error , data:" + entry.toString(),
                    e);
        }
        //ignore the ddl operation
        if (rowChange.hasIsDdl() && rowChange.getIsDdl()) {
            processDdl(rowChange);
            continue;
        }
        for (CanalEntry.RowData rowData : rowChange.getRowDatasList()) {
            //distribute to listener interfaces
            distributeByImpl(rowChange.getEventType(), rowData);
            //distribute to annotation listener interfaces
            distributeByAnnotation(destination,
                    entry.getHeader().getSchemaName(),
                    entry.getHeader().getTableName(),
                    rowChange.getEventType(),
                    rowData);
        }
    }
}
 
Example 8
Source File: DefaultMessageTransponder.java    From spring-boot-starter-canal with MIT License 5 votes vote down vote up
@Override
protected Object[] getInvokeArgs(Method method, CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
    return Arrays.stream(method.getParameterTypes())
            .map(p -> p == CanalEntry.EventType.class
                    ? eventType
                    : p == CanalEntry.RowData.class
                    ? rowData : null)
            .toArray();
}
 
Example 9
Source File: MyEventListener2.java    From spring-boot-starter-canal with MIT License 4 votes vote down vote up
@Override
public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
    rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--implements :" + c.getName() + " ::   " + c.getValue()));
}
 
Example 10
Source File: MysqlMessageProcessor.java    From DBus with Apache License 2.0 4 votes vote down vote up
@Override
public int processFullPullerMessage(Map<String, List<IGenericMessage>> map, IGenericMessage obj) throws
        IOException {
    MysqlGenericMessage message = (MysqlGenericMessage) obj;
    CanalEntry.Entry entry = message.getEntry();

    CanalEntry.EventType eventType = entry.getHeader().getEventType();
    if (eventType != CanalEntry.EventType.INSERT) {
        //skip it.
        logger.info("Skipped a FULL_PULL_REQUESTS message which is not INSERT Type! :" + eventType.toString());
        return -1;
    }

    CanalEntry.RowChange rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
    List<CanalEntry.RowData> dataList = rowChange.getRowDatasList();
    if (dataList.size() != 1) {
        throw new RuntimeException(String.format("解压FULL_PULL_REQUESTS 发现 %d 条bach数据,应该只有一条", dataList.size()));
    }

    //为了向下兼容,使用PULL_REMARK 保存dsName
    String dsName = null;
    String schema = null;
    String table = null;
    List<CanalEntry.Column> columns = dataList.get(0).getAfterColumnsList();
    for (CanalEntry.Column column : columns) {
        if (column.getName().equalsIgnoreCase("PULL_REMARK")) {
            dsName = column.getValue();
        } else if (column.getName().equalsIgnoreCase("SCHEMA_NAME")) {
            schema = column.getValue();
        } else if (column.getName().equalsIgnoreCase("TABLE_NAME")) {
            table = column.getValue();
        }
    }

    if (dsName == null || schema == null || table == null) {
        throw new RuntimeException("解压FULL_PULL_REQUESTS 发现 dsName 或 schema 或 table为空.");
    }

    if (!dsName.equalsIgnoreCase(dsInfo.getDbSourceName())) {
        logger.info("Skipped other datasource FULL_PULL_REQUESTS! : {}.{}.{}", dsName, schema, table);
        return -1;
    }

    logger.info(String.format("Get FULL_PULL_REQUESTS message : %s.%s.%s", dsName, schema, table));

    //单独发送一条 拉全量的消息
    List<IGenericMessage> subList = new ArrayList<>();
    subList.add(message);
    map.put(schema, subList);
    return 0;
}
 
Example 11
Source File: MysqlMessageProcessor.java    From DBus with Apache License 2.0 4 votes vote down vote up
@Override
    public int processHeartBeatMessage(Map<String, List<IGenericMessage>> map, IGenericMessage obj) throws
            IOException {
        MysqlGenericMessage message = (MysqlGenericMessage) obj;
        CanalEntry.Entry entry = message.getEntry();

        CanalEntry.EventType eventType = entry.getHeader().getEventType();
        if (eventType != CanalEntry.EventType.INSERT) {
            //skip it.
            logger.info("Skipped a DB_HEARTBEAT_MONITOR message which is not INSERT Type! :" + eventType.toString());
            return -1;
        }

        CanalEntry.RowChange rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
        List<CanalEntry.RowData> dataList = rowChange.getRowDatasList();
        /**
         * 以前代码dataList.size() 必须是1条数据,发现生产中有奇怪数据,心跳表数据居然不止一个心跳数据,推测是 insert ... select ...
         * 这种情况我们不处理,打算直接将这个心跳包抛弃。
         */
//        if (dataList.size() != 1) {
//            throw new RuntimeException(String.format("DB_HEARTBEAT_MONITOR 发现 %d 条bach数据,应该只有一条", dataList.size()));
//        }
        if (dataList.size() != 1) {
            logger.error(String.format("Skipped a DB_HEARTBEAT_MONITOR message. DB_HEARTBEAT_MONITOR 发现 %d 条bach数据,应该只有一条, 语句是%s",
                    dataList.size(), rowChange.getSql()));
            return -1;
        }

        String dsName = null;
        String schemaName = null;
        String tableName = null;
        String packetJson = null;
        List<CanalEntry.Column> columns = dataList.get(0).getAfterColumnsList();
        for (CanalEntry.Column column : columns) {
            if (column.getName().equalsIgnoreCase("DS_NAME")) {
                dsName = column.getValue();
            } else if (column.getName().equalsIgnoreCase("SCHEMA_NAME")) {
                schemaName = column.getValue();
            } else if (column.getName().equalsIgnoreCase("TABLE_NAME")) {
                tableName = column.getValue();
            } else if (column.getName().equalsIgnoreCase("PACKET"))
                packetJson = column.getValue();
        }

        if (dsName == null || schemaName == null || tableName == null || packetJson == null) {
            throw new RuntimeException("DB_HEARTBEAT_MONITOR 发现 dsName 或 schema 或 table, 或 packetJson 为空.");
        }

        if (!dsName.equalsIgnoreCase(dsInfo.getDbSourceName())) {
            logger.info("Skipped other datasource HeartBeat! : {}.{}.{}", dsName, schemaName, tableName);
            return -1;
        }

        logger.debug(String.format("Get DB_HEARTBEAT_MONITOR message : %s.%s, packetJson: %s", schemaName, tableName, packetJson));
        if (packetJson.indexOf("checkpoint") >= 0) {
            logger.debug(String.format("Get DB_HEARTBEAT_MONITOR message, prepare set stat message. "));
            HeartBeatPacket packet = HeartBeatPacket.parse(packetJson);
            statMeter(schemaName, tableName, packet.getTime(), packet.getTxtime());
        }

        List<IGenericMessage> subList = map.get(schemaName);
        if (subList != null) {
            subList.add(message);
        } else {
            subList = new ArrayList<>();
            subList.add(message);
            map.put(schemaName, subList);
        }
        return 0;
    }
 
Example 12
Source File: MyEventListener.java    From spring-boot-starter-canal with MIT License 4 votes vote down vote up
@InsertListenPoint
public void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData) {
    rowData.getAfterColumnsList().forEach((c) -> System.err.println("By--Annotation: " + c.getName() + " ::   " + c.getValue()));
}
 
Example 13
Source File: AbstractBasicMessageTransponder.java    From spring-boot-starter-canal with MIT License 2 votes vote down vote up
/**
 * get the args
 *
 * @param method method
 * @param eventType event type
 * @param rowData row data
 * @return args which will be used by invoking the annotation methods
 */
protected abstract Object[] getInvokeArgs(Method method, CanalEntry.EventType eventType,
                                          CanalEntry.RowData rowData);
 
Example 14
Source File: CanalEventListener.java    From spring-boot-starter-canal with MIT License 2 votes vote down vote up
/**
 * run when event was fired
 *
 * @param eventType eventType
 * @param rowData rowData
 */
void onEvent(CanalEntry.EventType eventType, CanalEntry.RowData rowData);
 
Example 15
Source File: DmlCanalEventListener.java    From spring-boot-starter-canal with MIT License 2 votes vote down vote up
/**
 * fired on insert event
 *
 * @param rowData rowData
 */
void onInsert(CanalEntry.RowData rowData);
 
Example 16
Source File: DmlCanalEventListener.java    From spring-boot-starter-canal with MIT License 2 votes vote down vote up
/**
 * fired on update event
 *
 * @param rowData rowData
 */
void onUpdate(CanalEntry.RowData rowData);
 
Example 17
Source File: ICanalInvoke.java    From canal-client with Apache License 2.0 votes vote down vote up
public void onInsert(CanalEntry.RowData rowData); 
Example 18
Source File: ICanalInvoke.java    From canal-client with Apache License 2.0 votes vote down vote up
public void onDelete(CanalEntry.RowData rowData); 
Example 19
Source File: EsAdapter.java    From canal-elasticsearch with Apache License 2.0 votes vote down vote up
List<CanalEntry.Column> getColumnList(int esEventType, CanalEntry.RowData rowData); 
Example 20
Source File: ICanalInvoke.java    From canal-client with Apache License 2.0 votes vote down vote up
public void onUpdate(CanalEntry.RowData rowData);