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

The following examples show how to use com.alibaba.otter.canal.protocol.CanalEntry#EventType . 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: 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 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: 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 4
Source File: HandleExceptionContext.java    From search-commons with Apache License 2.0 5 votes vote down vote up
public HandleExceptionContext(RuntimeException exception, String schema, String table,
                              CanalEntry.EventType eventType, List<RowChangedData> changedData) {
    this.exception = exception;
    this.schema = schema;
    this.table = table;
    this.eventType = eventType;
    this.changedData = CommonsUtils.isEmpty(changedData) ? Collections.<RowChangedData>emptyList()
            : changedData;
}
 
Example 5
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 6
Source File: EventTypeSectionHandle.java    From search-commons with Apache License 2.0 5 votes vote down vote up
@Override
protected void doRowChangeHandle(List<RowChangedData> changedData) {
    //尽量集中处理
    Schema<EventTypeAction>.Table currentTable = getCurrentTable();
    CanalEntry.EventType currentEventType = getCurrentEventType();
    if (!currentTable.equals(lastTable) || currentEventType != lastEventType) {
        runLastRowChangeAction();
        lastTable = currentTable;
        lastEventType = currentEventType;
    }
    rowChangedDataList.addAll(changedData);
}
 
Example 7
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 8
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 9
Source File: MysqlGenericMessage.java    From DBus with Apache License 2.0 5 votes vote down vote up
@Override
public boolean isDML() {
    CanalEntry.EventType eventType = entry.getHeader().getEventType();
    switch (eventType) {
        case INSERT:
        case UPDATE:
        case DELETE:
            return true;
        default:
            return false;
    }
}
 
Example 10
Source File: InstanceRowChangedData.java    From search-commons with Apache License 2.0 5 votes vote down vote up
public InstanceRowChangedData(String schema, String table, CanalEntry.EventType eventType,
                              List<? extends RowChangedData> changedData) {
    this.schema = schema;
    this.table = table;
    this.eventType = eventType;
    this.changedData = Collections.unmodifiableList(changedData);
}
 
Example 11
Source File: Schemas.java    From search-commons with Apache License 2.0 4 votes vote down vote up
/**
 * 添加需要排除的事件类型
 */
public TableBuilder forbidEventType(CanalEntry.EventType eventType) {
    forbidEventType |= RowChangedData.getEventTypeFlag(eventType);
    return this;
}
 
Example 12
Source File: SimpleMessageFilter.java    From canal-elasticsearch with Apache License 2.0 4 votes vote down vote up
private boolean filterEventType(CanalEntry.EventType eventType) {
    return acceptEventType.contains(eventType);
}
 
Example 13
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 14
Source File: InstanceSectionHandle.java    From search-commons with Apache License 2.0 4 votes vote down vote up
/**
 * 添加需要排除的事件类型
 */
public void addForbidEventType(CanalEntry.EventType eventType) {
    forbidEventType |= RowChangedData.getEventTypeFlag(eventType);
}
 
Example 15
Source File: HandleExceptionContext.java    From search-commons with Apache License 2.0 4 votes vote down vote up
public CanalEntry.EventType getEventType() {
    return eventType;
}
 
Example 16
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 17
Source File: InstanceRowChangedData.java    From search-commons with Apache License 2.0 4 votes vote down vote up
public CanalEntry.EventType getEventType() {
    return eventType;
}
 
Example 18
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 19
Source File: AbstractBasicMessageTransponder.java    From spring-boot-starter-canal with MIT License 2 votes vote down vote up
/**
 * get the filters predicate
 *
 * @param destination destination
 * @param schemaName schema
 * @param tableName table name
 * @param eventType event type
 * @return predicate
 */
protected abstract Predicate<Map.Entry<Method, ListenPoint>> getAnnotationFilter(String destination,
                                                                        String schemaName,
                                                                        String tableName,
                                                                        CanalEntry.EventType eventType);
 
Example 20
Source File: EsAdapter.java    From canal-elasticsearch with Apache License 2.0 votes vote down vote up
int getEsEventType(CanalEntry.EventType eventType);