Java Code Examples for com.github.shyiko.mysql.binlog.event.Event#getData()

The following examples show how to use com.github.shyiko.mysql.binlog.event.Event#getData() . 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: EventProcessor.java    From openmessaging-connect-odar with Apache License 2.0 6 votes vote down vote up
private void processXidEvent(Event event) {
    EventHeaderV4 header = event.getHeader();
    XidEventData data = event.getData();

    String binlogFilename = binaryLogClient.getBinlogFilename();
    Long position = header.getNextPosition();
    Long xid = data.getXid();

    BinlogPosition binlogPosition = new BinlogPosition(binlogFilename, position);
    transaction.setNextBinlogPosition(binlogPosition);
    transaction.setXid(xid);

    replicator.commit(transaction, true);

    transaction = new Transaction(config);
}
 
Example 2
Source File: BinLogWriteEventHandler.java    From kkbinlog with Apache License 2.0 6 votes vote down vote up
@Override
protected EventBaseDTO formatData(Event event) {
    WriteRowsEventData d = event.getData();
    WriteRowsDTO writeRowsDTO = new WriteRowsDTO();
    writeRowsDTO.setEventType(DatabaseEvent.WRITE_ROWS);
    //添加表信息
    ColumnsTableMapEventData tableMapData = context.getTableMapData(d.getTableId());
    writeRowsDTO.setDatabase(tableMapData.getDatabase());
    writeRowsDTO.setTable(tableMapData.getTable());
    writeRowsDTO.setNamespace(context.getBinaryLogConfig().getNamespace());
    //添加列映射
    int[] includedColumns = d.getIncludedColumns().stream().toArray();
    writeRowsDTO.setRowMaps(d.getRows().stream()
            .map(r -> convert(r,includedColumns,tableMapData)).collect(Collectors.toList()));
    return writeRowsDTO;
}
 
Example 3
Source File: BinLogUpdateEventHandler.java    From kkbinlog with Apache License 2.0 6 votes vote down vote up
@Override
protected EventBaseDTO formatData(Event event) {
    UpdateRowsEventData d = event.getData();
    UpdateRowsDTO updateRowsDTO = new UpdateRowsDTO();
    updateRowsDTO.setEventType(DatabaseEvent.UPDATE_ROWS);
    //添加表信息
    ColumnsTableMapEventData tableMapData = context.getTableMapData(d.getTableId());
    updateRowsDTO.setDatabase(tableMapData.getDatabase());
    updateRowsDTO.setTable(tableMapData.getTable());
    updateRowsDTO.setNamespace(context.getBinaryLogConfig().getNamespace());
    //添加列映射
    int[] includedColumns = d.getIncludedColumns().stream().toArray();
    List<UpdateRow> urs = d.getRows().stream()
            .map(e -> new UpdateRow(convert(e.getKey(),includedColumns,tableMapData),
                    convert(e.getValue(),includedColumns,tableMapData))).collect(Collectors.toList());
    updateRowsDTO.setRows(urs);
    return updateRowsDTO;
}
 
Example 4
Source File: BinLogTableMapEventHandler.java    From kkbinlog with Apache License 2.0 6 votes vote down vote up
@Override
public void handle(Event event) {
    TableMapEventData d = event.getData();
    log.debug("TableMapEventData:{}", d);
    ColumnsTableMapEventData tableMapEventData = context.getTableMapData(d.getTableId());
    //如果表结构有变化,重新设置映射信息
    if (tableMapEventData == null || !ColumnsTableMapEventData.checkEqual(d, tableMapEventData)) {
        log.info("更新表映射:{}-{}", d.getDatabase(), d.getTable());
        ColumnsTableMapEventData data = new ColumnsTableMapEventData(d);
        String sql = "show columns from `" + d.getTable() + "` from `" + d.getDatabase() + "`";
        try (Connection conn = context.getDataSource().getConnection();
             PreparedStatement ps = conn.prepareStatement(sql);
             ResultSet resultSet = ps.executeQuery();) {
            while (resultSet.next()) {
                data.addColumnName(resultSet.getString("Field"));
            }
            //将表id和表映射
            context.addTableMapData(d.getTableId(), data);
        } catch (SQLException e) {
            log.error("获取表数据错误,sql语句为{},异常如下:{}", sql, e);
        }
    }
}
 
Example 5
Source File: EventProcessor.java    From openmessaging-connect-odar with Apache License 2.0 5 votes vote down vote up
private void processTableMapEvent(Event event) {
    TableMapEventData data = event.getData();
    String dbName = data.getDatabase();
    String tableName = data.getTable();
    Long tableId = data.getTableId();

    Table table = schema.getTable(dbName, tableName);
    if (table != null){
        tableMap.put(tableId, table);
    }
}
 
Example 6
Source File: EventProcessor.java    From openmessaging-connect-odar with Apache License 2.0 5 votes vote down vote up
private void processWriteEvent(Event event) {
    WriteRowsEventData data = event.getData();
    Long tableId = data.getTableId();
    List<Serializable[]> list = data.getRows();

    for (Serializable[] row : list) {
        addRow(EntryType.CREATE, tableId, row, null);
    }
}
 
Example 7
Source File: EventProcessor.java    From openmessaging-connect-odar with Apache License 2.0 5 votes vote down vote up
private void processUpdateEvent(Event event) {
    UpdateRowsEventData data = event.getData();
    Long tableId = data.getTableId();
    List<Map.Entry<Serializable[], Serializable[]>> list = data.getRows();

    for (Map.Entry<Serializable[], Serializable[]> entry : list) {
        addRow(EntryType.UPDATE, tableId, entry.getValue(), entry.getKey());
    }
}
 
Example 8
Source File: EventProcessor.java    From openmessaging-connect-odar with Apache License 2.0 5 votes vote down vote up
private void processDeleteEvent(Event event) {
    DeleteRowsEventData data = event.getData();
    Long tableId = data.getTableId();
    List<Serializable[]> list = data.getRows();

    for (Serializable[] row : list) {
        addRow(EntryType.DELETE, tableId, null, row);
    }

}
 
Example 9
Source File: EventProcessor.java    From openmessaging-connect-odar with Apache License 2.0 5 votes vote down vote up
private void processQueryEvent(Event event) {
    QueryEventData data = event.getData();
    String sql = data.getSql();

    if (createTablePattern.matcher(sql).find()) {
        schema.reset();
    }
}
 
Example 10
Source File: BinLogWriteEventHandler.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Set<ClientInfo> filter(Event event) {
    WriteRowsEventData d = event.getData();
    long tableId = d.getTableId();
    TableMapEventData tableMapEventData = context.getTableMapData(tableId);
    String tableKey = tableMapEventData.getDatabase().concat("/").concat(tableMapEventData.getTable());
    return clientInfoMap.get(tableKey);
}
 
Example 11
Source File: BinLogUpdateEventHandler.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
@Override
protected Set<ClientInfo> filter(Event event) {
    UpdateRowsEventData d = event.getData();
    long tableId = d.getTableId();
    TableMapEventData tableMapEventData =  context.getTableMapData(tableId);
    String tableKey = tableMapEventData.getDatabase().concat("/").concat(tableMapEventData.getTable());
    return clientInfoMap.get(tableKey);
}
 
Example 12
Source File: BinLogDDLEventHandler.java    From kkbinlog with Apache License 2.0 5 votes vote down vote up
@Override
public void handle(Event event) {
    boolean ddleventEnable = Boolean.parseBoolean(System.getProperty(DDLEVENT_ENABLE_KEY,DDLEVENT_ENABLE));
    if(ddleventEnable){
        QueryEventData data = event.getData();
        String sql = data.getSql();
        if (sql.contains(SUBSCRIBE_SQL_EVENT)) {
            log.info("数据库:{}发生alter table事件", data.getDatabase());
            String topic = System.getProperty(DEFAULT_TOPIC_KEY,DEFAULT_TOPIC);
            dataPublisher.pushToKafka(topic, data);
        }
    }
    updateBinaryLogStatus(event.getHeader());
}
 
Example 13
Source File: BinLogRotateEventHandler.java    From kkbinlog with Apache License 2.0 4 votes vote down vote up
@Override
public void handle(Event event) {
    RotateEventData d = event.getData();
    EtcdService etcdService = context.getEtcdService();
    etcdService.updateBinLogStatus(d.getBinlogFilename(), d.getBinlogPosition(), context.getBinaryLogConfig(), System.currentTimeMillis()); // event.header.timestamp = 0
}
 
Example 14
Source File: BinaryLogConsumer.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public void onEvent(Event event) {
  LOG.trace("Received event {}", event);
  EventType eventType = event.getHeader().getEventType();
  currentBinLogFileName = client.getBinlogFilename();
  switch (eventType) {
    case TABLE_MAP:
      handleTableMappingEvent((TableMapEventData) event.getData());
      break;
    case PRE_GA_WRITE_ROWS:
    case WRITE_ROWS:
    case EXT_WRITE_ROWS:
      handleRowEvent(event, event.<WriteRowsEventData>getData().getTableId());
      break;
    case PRE_GA_UPDATE_ROWS:
    case UPDATE_ROWS:
    case EXT_UPDATE_ROWS:
      handleRowEvent(event, event.<UpdateRowsEventData>getData().getTableId());
      break;
    case PRE_GA_DELETE_ROWS:
    case DELETE_ROWS:
    case EXT_DELETE_ROWS:
      handleRowEvent(event, event.<DeleteRowsEventData>getData().getTableId());
      break;
    case QUERY:
      QueryEventData queryEventData = event.getData();
      String query = queryEventData.getSql();
      if (isCommit(query)) {
        finishTx();
      } else if (isSchemaChangeQuery(query)) {
        schemaRepository.evictAll();
      }
      break;
    case XID:
      finishTx();
      break;
    case GTID:
      GtidEventData eventData = event.getData();
      currentGtidSet = client.getGtidSet();
      currentTxGtid = eventData.getGtid();
      currentTxEventSeqNo = 0;
      LOG.trace("Started new tx, gtid: {}", currentTxGtid);
      break;
    default:
      // ignore
      break;
  }
}
 
Example 15
Source File: BinaryLogConsumer.java    From datacollector with Apache License 2.0 4 votes vote down vote up
@Override
public void onEvent(Event event) {
  LOG.trace("Received event {}", event);
  EventType eventType = event.getHeader().getEventType();
  currentBinLogFileName = client.getBinlogFilename();
  switch (eventType) {
    case TABLE_MAP:
      handleTableMappingEvent((TableMapEventData) event.getData());
      break;
    case PRE_GA_WRITE_ROWS:
    case WRITE_ROWS:
    case EXT_WRITE_ROWS:
      handleRowEvent(event, event.<WriteRowsEventData>getData().getTableId());
      break;
    case PRE_GA_UPDATE_ROWS:
    case UPDATE_ROWS:
    case EXT_UPDATE_ROWS:
      handleRowEvent(event, event.<UpdateRowsEventData>getData().getTableId());
      break;
    case PRE_GA_DELETE_ROWS:
    case DELETE_ROWS:
    case EXT_DELETE_ROWS:
      handleRowEvent(event, event.<DeleteRowsEventData>getData().getTableId());
      break;
    case QUERY:
      QueryEventData queryEventData = event.getData();
      String query = queryEventData.getSql();
      if (isCommit(query)) {
        finishTx();
      } else if (isSchemaChangeQuery(query)) {
        schemaRepository.evictAll();
      }
      break;
    case XID:
      finishTx();
      break;
    case GTID:
      GtidEventData eventData = event.getData();
      currentGtidSet = client.getGtidSet();
      currentTxGtid = eventData.getGtid();
      currentTxEventSeqNo = 0;
      LOG.trace("Started new tx, gtid: {}", currentTxGtid);
      break;
    default:
      // ignore
      break;
  }
}