com.github.shyiko.mysql.binlog.event.EventType Java Examples

The following examples show how to use com.github.shyiko.mysql.binlog.event.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: IncrementListener.java    From ad with Apache License 2.0 6 votes vote down vote up
/**
 * 事件处理逻辑, 投放增量数据
 * @param eventData binlog监听到的数据, 许哟啊对其解析
 */
@Override
public void onEvent(BinlogRowData eventData) {
    TableTemplate tableTemplate = eventData.getTableTemplate();
    EventType eventType = eventData.getEventType();

    // 将数据包装为需要投递的数据格式 MySQLRowData
    MysqlRowData mySQLRowData = new MysqlRowData();
    mySQLRowData.setTableName(tableTemplate.getTableName());
    mySQLRowData.setLevel(tableTemplate.getLevel());
    mySQLRowData.setOpType(OpType.to(eventType));

    // 取出模板中操作对应的字段列表
    List<String> fieldList = tableTemplate.getOpTypeFieldSetMap().get(mySQLRowData.getOpType());
    if (fieldList == null) {
        // template.json中表示的表中不存在该操作类型。数据不应该被处理
        log.warn("{} not support for {}", mySQLRowData.getOpType(), mySQLRowData.getTableName());
        return;
    }
    eventData.getAfter().forEach(afterMap ->
            mySQLRowData.getFieldValueMapList().add(Maps.newHashMap(afterMap))
    );
    iSender.sender(mySQLRowData);
}
 
Example #2
Source File: BinLogEventHandlerFactory.java    From kkbinlog with Apache License 2.0 6 votes vote down vote up
public BinLogEventHandler getHandler(EventHeader header) {
    //考虑到状态映射的问题,只在增删改是更新位置
    if (EventType.isUpdate(header.getEventType())) {
        return binLogUpdateEventHandler;
    } else if (EventType.isWrite(header.getEventType())) {
        return binLogWriteEventHandler;
    } else if (EventType.isDelete(header.getEventType())) {
        return binLogDeleteEventHandler;
    } else if (EventType.TABLE_MAP.equals(header.getEventType())) {
        log.debug("TableMapEvent-header:{}", header);
        return binLogTableMapEventHandler;
    } else if (EventType.ROTATE.equals(header.getEventType())) {
        log.debug("RotateEvent-header:{}", header);
        return binLogRotateEventHandler;
    }else if(EventType.QUERY.equals(header.getEventType())){
        return binLogDDLEventHandler;
    } else {
        log.debug("不处理事件,{}", header);
        return binLogDefaultEventHandler;
    }
}
 
Example #3
Source File: BinaryLogConnectorEventMapperTest.java    From SpinalTap with Apache License 2.0 6 votes vote down vote up
@Test
public void testWriteEvent() {
  eventHeader.setEventType(EventType.EXT_WRITE_ROWS);
  WriteRowsEventData eventData = new WriteRowsEventData();
  eventData.setTableId(TABLE_ID);
  eventData.setRows(ImmutableList.of(ROW));

  Optional<BinlogEvent> binlogEvent =
      BinaryLogConnectorEventMapper.INSTANCE.map(
          new Event(eventHeader, eventData), BINLOG_FILE_POS);
  assertTrue(binlogEvent.isPresent());
  assertTrue(binlogEvent.get() instanceof WriteEvent);
  WriteEvent writeEvent = (WriteEvent) (binlogEvent.get());
  assertEquals(BINLOG_FILE_POS, writeEvent.getBinlogFilePos());
  assertEquals(ImmutableList.of(ROW), writeEvent.getRows());
  assertEquals(SERVER_ID, writeEvent.getServerId());
  assertEquals(TABLE_ID, writeEvent.getTableId());
  assertEquals(TIMESTAMP, writeEvent.getTimestamp());
}
 
Example #4
Source File: BinaryLogConnectorEventMapperTest.java    From SpinalTap with Apache License 2.0 6 votes vote down vote up
@Test
public void testUpdateEvent() {
  eventHeader.setEventType(EventType.EXT_UPDATE_ROWS);
  UpdateRowsEventData eventData = new UpdateRowsEventData();
  eventData.setTableId(TABLE_ID);
  eventData.setRows(ImmutableList.of(Maps.immutableEntry(PREV_ROW, ROW)));

  Optional<BinlogEvent> binlogEvent =
      BinaryLogConnectorEventMapper.INSTANCE.map(
          new Event(eventHeader, eventData), BINLOG_FILE_POS);
  assertTrue(binlogEvent.isPresent());
  assertTrue(binlogEvent.get() instanceof UpdateEvent);
  UpdateEvent updateEvent = (UpdateEvent) (binlogEvent.get());
  assertEquals(BINLOG_FILE_POS, updateEvent.getBinlogFilePos());
  assertEquals(ImmutableList.of(Maps.immutableEntry(PREV_ROW, ROW)), updateEvent.getRows());
  assertEquals(SERVER_ID, updateEvent.getServerId());
  assertEquals(TABLE_ID, updateEvent.getTableId());
  assertEquals(TIMESTAMP, updateEvent.getTimestamp());
}
 
Example #5
Source File: BinaryLogConnectorEventMapperTest.java    From SpinalTap with Apache License 2.0 6 votes vote down vote up
@Test
public void testDeleteEvent() {
  eventHeader.setEventType(EventType.EXT_DELETE_ROWS);
  DeleteRowsEventData eventData = new DeleteRowsEventData();
  eventData.setTableId(TABLE_ID);
  eventData.setRows(ImmutableList.of(PREV_ROW));

  Optional<BinlogEvent> binlogEvent =
      BinaryLogConnectorEventMapper.INSTANCE.map(
          new Event(eventHeader, eventData), BINLOG_FILE_POS);
  assertTrue(binlogEvent.isPresent());
  assertTrue(binlogEvent.get() instanceof DeleteEvent);
  DeleteEvent deleteEvent = (DeleteEvent) (binlogEvent.get());
  assertEquals(BINLOG_FILE_POS, deleteEvent.getBinlogFilePos());
  assertEquals(ImmutableList.of(PREV_ROW), deleteEvent.getRows());
  assertEquals(SERVER_ID, deleteEvent.getServerId());
  assertEquals(TABLE_ID, deleteEvent.getTableId());
  assertEquals(TIMESTAMP, deleteEvent.getTimestamp());
}
 
Example #6
Source File: BinaryLogConnectorEventMapperTest.java    From SpinalTap with Apache License 2.0 6 votes vote down vote up
@Test
public void testTableMapEvent() {
  eventHeader.setEventType(EventType.TABLE_MAP);
  TableMapEventData eventData = new TableMapEventData();
  eventData.setDatabase(DATABASE);
  eventData.setTable(TABLE);
  eventData.setTableId(TABLE_ID);
  eventData.setColumnTypes(new byte[] {(byte) 0, (byte) 1, (byte) 2});

  Optional<BinlogEvent> binlogEvent =
      BinaryLogConnectorEventMapper.INSTANCE.map(
          new Event(eventHeader, eventData), BINLOG_FILE_POS);
  assertTrue(binlogEvent.isPresent());
  assertTrue(binlogEvent.get() instanceof TableMapEvent);
  TableMapEvent tableMapEvent = (TableMapEvent) (binlogEvent.get());
  assertEquals(BINLOG_FILE_POS, tableMapEvent.getBinlogFilePos());
  assertEquals(DATABASE, tableMapEvent.getDatabase());
  assertEquals(TABLE, tableMapEvent.getTable());
  assertEquals(TABLE_ID, tableMapEvent.getTableId());
  assertEquals(
      ImmutableList.of(ColumnDataType.DECIMAL, ColumnDataType.TINY, ColumnDataType.SHORT),
      tableMapEvent.getColumnTypes());
}
 
Example #7
Source File: BinaryLogConnectorEventMapperTest.java    From SpinalTap with Apache License 2.0 6 votes vote down vote up
@Test
public void testXidEvent() {
  long xid = 88888L;
  eventHeader.setEventType(EventType.XID);
  XidEventData eventData = new XidEventData();
  eventData.setXid(xid);

  Optional<BinlogEvent> binlogEvent =
      BinaryLogConnectorEventMapper.INSTANCE.map(
          new Event(eventHeader, eventData), BINLOG_FILE_POS);
  assertTrue(binlogEvent.isPresent());
  assertTrue(binlogEvent.get() instanceof XidEvent);
  XidEvent xidEvent = (XidEvent) (binlogEvent.get());
  assertEquals(BINLOG_FILE_POS, xidEvent.getBinlogFilePos());
  assertEquals(SERVER_ID, xidEvent.getServerId());
  assertEquals(TIMESTAMP, xidEvent.getTimestamp());
  assertEquals(xid, xidEvent.getXid());
}
 
Example #8
Source File: BinaryLogConnectorEventMapperTest.java    From SpinalTap with Apache License 2.0 6 votes vote down vote up
@Test
public void testQueryEvent() {
  String sql = "CREATE UNIQUE INDEX unique_index ON `my_db`.`my_table` (`col1`, `col2`)";
  eventHeader.setEventType(EventType.QUERY);
  QueryEventData eventData = new QueryEventData();
  eventData.setDatabase(DATABASE);
  eventData.setSql(sql);

  Optional<BinlogEvent> binlogEvent =
      BinaryLogConnectorEventMapper.INSTANCE.map(
          new Event(eventHeader, eventData), BINLOG_FILE_POS);
  assertTrue(binlogEvent.isPresent());
  assertTrue(binlogEvent.get() instanceof QueryEvent);
  QueryEvent queryEvent = (QueryEvent) (binlogEvent.get());
  assertEquals(BINLOG_FILE_POS, queryEvent.getBinlogFilePos());
  assertEquals(DATABASE, queryEvent.getDatabase());
  assertEquals(SERVER_ID, queryEvent.getServerId());
  assertEquals(TIMESTAMP, queryEvent.getTimestamp());
  assertEquals(sql, queryEvent.getSql());
}
 
Example #9
Source File: SyncDeserializer.java    From syncer with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
public static EventDeserializer defaultDeserializer() {
  Map<Long, TableMapEventData> tableMapEventByTableId = new HashMap<>();
  Map<EventType, EventDataDeserializer> eventDataDeserializers = new IdentityHashMap<>();
  eventDataDeserializers.put(EventType.WRITE_ROWS,
      new WriteRowsEventDataDeserializer(tableMapEventByTableId));
  eventDataDeserializers.put(EventType.UPDATE_ROWS,
      new UpdateRowsEventDataDeserializer(tableMapEventByTableId));
  eventDataDeserializers.put(EventType.DELETE_ROWS,
      new DeleteRowsEventDataDeserializer(tableMapEventByTableId));
  eventDataDeserializers.put(EventType.EXT_WRITE_ROWS,
      new WriteRowsEventDataDeserializer(tableMapEventByTableId).
          setMayContainExtraInformation(true));
  eventDataDeserializers.put(EventType.EXT_UPDATE_ROWS,
      new UpdateRowsEventDataDeserializer(tableMapEventByTableId).
          setMayContainExtraInformation(true));
  eventDataDeserializers.put(EventType.EXT_DELETE_ROWS,
      new DeleteRowsEventDataDeserializer(tableMapEventByTableId).
          setMayContainExtraInformation(true));
  eventDataDeserializers.put(EventType.QUERY, new QueryEventDataDeserializer());
  eventDataDeserializers.put(EventType.TABLE_MAP, new TableMapEventDataDeserializer());

  return new EventDeserializer(new EventHeaderV4Deserializer(), new NullEventDataDeserializer(),
      eventDataDeserializers, tableMapEventByTableId);
}
 
Example #10
Source File: BinaryLogConnectorEventMapperTest.java    From SpinalTap with Apache License 2.0 5 votes vote down vote up
@Test
public void testFormatDescriptionEvent() {
  eventHeader.setEventType(EventType.FORMAT_DESCRIPTION);
  FormatDescriptionEventData eventData = new FormatDescriptionEventData();

  Optional<BinlogEvent> binlogEvent =
      BinaryLogConnectorEventMapper.INSTANCE.map(
          new Event(eventHeader, eventData), BINLOG_FILE_POS);
  assertTrue(binlogEvent.isPresent());
  assertTrue(binlogEvent.get() instanceof StartEvent);
  StartEvent startEvent = (StartEvent) (binlogEvent.get());
  assertEquals(BINLOG_FILE_POS, startEvent.getBinlogFilePos());
  assertEquals(SERVER_ID, startEvent.getServerId());
  assertEquals(TIMESTAMP, startEvent.getTimestamp());
}
 
Example #11
Source File: BinaryLogConnectorEventMapperTest.java    From SpinalTap with Apache License 2.0 5 votes vote down vote up
@Test
public void testIgnoredEvents() {
  eventHeader.setEventType(EventType.UNKNOWN);
  XAPrepareEventData eventData = new XAPrepareEventData();

  Optional<BinlogEvent> binlogEvent =
      BinaryLogConnectorEventMapper.INSTANCE.map(
          new Event(eventHeader, eventData), BINLOG_FILE_POS);
  assertFalse(binlogEvent.isPresent());
}
 
Example #12
Source File: RecordConverter.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public List<Record> toRecords(EnrichedEvent event) {
  EventType eventType = event.getEvent().getHeader().getEventType();
  switch (eventType) {
    case PRE_GA_WRITE_ROWS:
    case WRITE_ROWS:
    case EXT_WRITE_ROWS:
      return toRecords(
          event.getTable(),
          event.getEvent().getHeader(),
          event.getEvent().<WriteRowsEventData>getData(),
          event.getOffset()
      );
    case PRE_GA_UPDATE_ROWS:
    case UPDATE_ROWS:
    case EXT_UPDATE_ROWS:
      return toRecords(
          event.getTable(),
          event.getEvent().getHeader(),
          event.getEvent().<UpdateRowsEventData>getData(),
          event.getOffset()
      );
    case PRE_GA_DELETE_ROWS:
    case DELETE_ROWS:
    case EXT_DELETE_ROWS:
      return toRecords(
          event.getTable(),
          event.getEvent().getHeader(),
          event.getEvent().<DeleteRowsEventData>getData(),
          event.getOffset()
      );
    default:
      throw new IllegalArgumentException(String.format("EventType '%s' not supported", eventType));
  }
}
 
Example #13
Source File: RecordConverter.java    From datacollector with Apache License 2.0 5 votes vote down vote up
public List<Record> toRecords(EnrichedEvent event) {
  EventType eventType = event.getEvent().getHeader().getEventType();
  switch (eventType) {
    case PRE_GA_WRITE_ROWS:
    case WRITE_ROWS:
    case EXT_WRITE_ROWS:
      return toRecords(
          event.getTable(),
          event.getEvent().getHeader(),
          event.getEvent().<WriteRowsEventData>getData(),
          event.getOffset()
      );
    case PRE_GA_UPDATE_ROWS:
    case UPDATE_ROWS:
    case EXT_UPDATE_ROWS:
      return toRecords(
          event.getTable(),
          event.getEvent().getHeader(),
          event.getEvent().<UpdateRowsEventData>getData(),
          event.getOffset()
      );
    case PRE_GA_DELETE_ROWS:
    case DELETE_ROWS:
    case EXT_DELETE_ROWS:
      return toRecords(
          event.getTable(),
          event.getEvent().getHeader(),
          event.getEvent().<DeleteRowsEventData>getData(),
          event.getOffset()
      );
    default:
      throw new IllegalArgumentException(String.format("EventType '%s' not supported", eventType));
  }
}
 
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;
  }
}
 
Example #16
Source File: SyncListener.java    From syncer with BSD 3-Clause "New" or "Revised" License 2 votes vote down vote up
/**
 * May return null, should be handled
 *
 * @see #onEvent(Event)
 */
private static SimpleEventType toSimpleEvent(EventType type) {
  return map.getOrDefault(type, null);
}