Java Code Examples for com.github.shyiko.mysql.binlog.event.TableMapEventData#getTable()

The following examples show how to use com.github.shyiko.mysql.binlog.event.TableMapEventData#getTable() . 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: 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 2
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 3
Source File: ConsumerChannel.java    From syncer with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
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;
}