Java Code Examples for com.github.shyiko.mysql.binlog.event.TableMapEventData

The following examples show how to use com.github.shyiko.mysql.binlog.event.TableMapEventData. These examples are extracted from open source projects. 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 Project: kkbinlog   Source File: BinLogTableMapEventHandler.java    License: 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 2
@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 3
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 4
Source Project: openmessaging-connect-odar   Source File: EventProcessor.java    License: 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 5
Source Project: kkbinlog   Source File: ColumnsTableMapEventData.java    License: Apache License 2.0 5 votes vote down vote up
public ColumnsTableMapEventData(TableMapEventData tableMapEventData) {
    setTableId(tableMapEventData.getTableId());
    setTable(tableMapEventData.getTable());
    setDatabase(tableMapEventData.getDatabase());
    setColumnMetadata(tableMapEventData.getColumnMetadata());
    setColumnNullability(tableMapEventData.getColumnNullability());
    setColumnTypes(tableMapEventData.getColumnTypes());
}
 
Example 6
Source Project: kkbinlog   Source File: ColumnsTableMapEventData.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean checkEqual(Object first,Object second){
    if (first == second) return true;
    if (!(first instanceof TableMapEventData)) return false;
    if (!(second instanceof TableMapEventData)) return false;
    TableMapEventData f = (TableMapEventData) first;
    TableMapEventData s = (TableMapEventData) second;
    return Objects.equals(f.getTableId(), s.getTableId()) &&
            Objects.equals(f.getDatabase(), s.getDatabase()) &&
            Objects.equals(f.getTable(), s.getTable()) &&
            Arrays.equals(f.getColumnMetadata(), s.getColumnMetadata()) &&
            Objects.equals(f.getColumnNullability(), s.getColumnNullability()) &&
            Arrays.equals(f.getColumnTypes(), s.getColumnTypes());
}
 
Example 7
Source Project: kkbinlog   Source File: BinLogWriteEventHandler.java    License: 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 8
Source Project: kkbinlog   Source File: BinLogUpdateEventHandler.java    License: 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 9
private SyncData[] toSyncData(SimpleEventType type, BinlogDataId dataId, Event... e) {
  TableMapEventData tableMap = e[0].getData();
  TableMeta table = consumerSchemaMeta.findTable(tableMap.getDatabase(), tableMap.getTable());
  if (table == null) {
    return null;
  }

  List<IndexedFullRow> indexedRow = RowsEvent
      .getIndexedRows(type, e[1].getData(), table.getPrimaryKeys());
  List<NamedFullRow> namedRow = RowsEvent
      .getNamedRows(indexedRow, table.getInterestedAndPkIndex(), table.getInterestedAndPkIndexToName());
  String primaryKey = RowsEvent.getPrimaryKey(table.getInterestedAndPkIndexToName(), table.getPrimaryKeys());
  SyncData[] res = new SyncData[namedRow.size()];
  boolean hasData = false;
  for (int i = 0; i < res.length; i++) {
    NamedFullRow row = namedRow.get(i);
    if (onlyUpdated && type == SimpleEventType.UPDATE && row.getUpdated().isEmpty()) {
      logger.debug("Discard {} because [{}]", dataId.eventId(), row);
      // even though in one update event, multiple rows can have different updated column,
      // so we can only skip one by one
      // e.g. we listening 'name1' but not 'name2' and the following update will make all updates in a single event
      // UPDATE relation
      // SET name1 = CASE WHEN userid1 = 3 THEN 'jack' ELSE name1 END,
      //     name2 = CASE WHEN userid2 = 3 THEN 'jack' ELSE name2 END
      continue;
    }
    Object pk = row.get(primaryKey);
    if (!table.isInterestedPK()) {
      row.remove(primaryKey);
    }
    hasData = true;
    res[i] = new SyncData(dataId.copyAndSetOrdinal(i), type, tableMap.getDatabase(), tableMap.getTable(), primaryKey, pk, row);
  }
  if (hasData) {
    return res;
  }
  return null;
}
 
Example 10
Source Project: datacollector   Source File: BinaryLogConsumer.java    License: 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 11
Source Project: datacollector   Source File: BinaryLogConsumer.java    License: Apache License 2.0 4 votes vote down vote up
private void handleTableMappingEvent(TableMapEventData eventData) {
  tableMapping.put(
      eventData.getTableId(),
      new DatabaseAndTable(eventData.getDatabase(), eventData.getTable())
  );
}
 
Example 12
Source Project: datacollector   Source File: BinaryLogConsumer.java    License: 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 13
Source Project: datacollector   Source File: BinaryLogConsumer.java    License: Apache License 2.0 4 votes vote down vote up
private void handleTableMappingEvent(TableMapEventData eventData) {
  tableMapping.put(
      eventData.getTableId(),
      new DatabaseAndTable(eventData.getDatabase(), eventData.getTable())
  );
}