Search in sources :

Example 1 with SingleDml

use of com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml in project canal by alibaba.

the class RdbSyncService method sync.

/**
 * 批量同步
 *
 * @param mappingConfig 配置集合
 * @param dmls 批量 DML
 */
public void sync(Map<String, Map<String, MappingConfig>> mappingConfig, List<Dml> dmls, Properties envProperties) {
    sync(dmls, dml -> {
        if (dml.getIsDdl() != null && dml.getIsDdl() && StringUtils.isNotEmpty(dml.getSql())) {
            // DDL
            columnsTypeCache.remove(dml.getDestination() + "." + dml.getDatabase() + "." + dml.getTable());
            return false;
        } else {
            // DML
            String destination = StringUtils.trimToEmpty(dml.getDestination());
            String groupId = StringUtils.trimToEmpty(dml.getGroupId());
            String database = dml.getDatabase();
            String table = dml.getTable();
            Map<String, MappingConfig> configMap;
            if (envProperties != null && !"tcp".equalsIgnoreCase(envProperties.getProperty("canal.conf.mode"))) {
                configMap = mappingConfig.get(destination + "-" + groupId + "_" + database + "-" + table);
            } else {
                configMap = mappingConfig.get(destination + "_" + database + "-" + table);
            }
            if (configMap == null) {
                return false;
            }
            if (configMap.values().isEmpty()) {
                return false;
            }
            for (MappingConfig config : configMap.values()) {
                boolean caseInsensitive = config.getDbMapping().isCaseInsensitive();
                if (config.getConcurrent()) {
                    List<SingleDml> singleDmls = SingleDml.dml2SingleDmls(dml, caseInsensitive);
                    singleDmls.forEach(singleDml -> {
                        int hash = pkHash(config.getDbMapping(), singleDml.getData());
                        SyncItem syncItem = new SyncItem(config, singleDml);
                        dmlsPartition[hash].add(syncItem);
                    });
                } else {
                    int hash = 0;
                    List<SingleDml> singleDmls = SingleDml.dml2SingleDmls(dml, caseInsensitive);
                    singleDmls.forEach(singleDml -> {
                        SyncItem syncItem = new SyncItem(config, singleDml);
                        dmlsPartition[hash].add(syncItem);
                    });
                }
            }
            return true;
        }
    });
}
Also used : MappingConfig(com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig) SingleDml(com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml)

Example 2 with SingleDml

use of com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml in project canal by alibaba.

the class RdbMirrorDbSyncService method sync.

/**
 * 批量同步方法
 *
 * @param dmls 批量 DML
 */
public void sync(List<Dml> dmls) {
    List<Dml> dmlList = new ArrayList<>();
    for (Dml dml : dmls) {
        String destination = StringUtils.trimToEmpty(dml.getDestination());
        String database = dml.getDatabase();
        MirrorDbConfig mirrorDbConfig = mirrorDbConfigCache.get(destination + "." + database);
        if (mirrorDbConfig == null) {
            continue;
        }
        if (mirrorDbConfig.getMappingConfig() == null) {
            continue;
        }
        if (dml.getGroupId() != null && StringUtils.isNotEmpty(mirrorDbConfig.getMappingConfig().getGroupId())) {
            if (!mirrorDbConfig.getMappingConfig().getGroupId().equals(dml.getGroupId())) {
                // 如果groupId不匹配则过滤
                continue;
            }
        }
        if (dml.getIsDdl() != null && dml.getIsDdl() && StringUtils.isNotEmpty(dml.getSql())) {
            // DDL
            if (logger.isDebugEnabled()) {
                logger.debug("DDL: {}", JSON.toJSONString(dml, SerializerFeature.WriteMapNullValue));
            }
            executeDdl(mirrorDbConfig, dml);
            rdbSyncService.getColumnsTypeCache().remove(destination + "." + database + "." + dml.getTable());
            // 删除对应库表配置
            mirrorDbConfig.getTableConfig().remove(dml.getTable());
        } else {
            // DML
            initMappingConfig(dml.getTable(), mirrorDbConfig.getMappingConfig(), mirrorDbConfig, dml);
            dmlList.add(dml);
        }
    }
    if (!dmlList.isEmpty()) {
        rdbSyncService.sync(dmlList, dml -> {
            MirrorDbConfig mirrorDbConfig = mirrorDbConfigCache.get(dml.getDestination() + "." + dml.getDatabase());
            if (mirrorDbConfig == null) {
                return false;
            }
            String table = dml.getTable();
            MappingConfig config = mirrorDbConfig.getTableConfig().get(table);
            if (config == null) {
                return false;
            }
            // 是否区分大小写
            boolean caseInsensitive = config.getDbMapping().isCaseInsensitive();
            if (config.getConcurrent()) {
                List<SingleDml> singleDmls = SingleDml.dml2SingleDmls(dml, caseInsensitive);
                singleDmls.forEach(singleDml -> {
                    int hash = rdbSyncService.pkHash(config.getDbMapping(), singleDml.getData());
                    RdbSyncService.SyncItem syncItem = new RdbSyncService.SyncItem(config, singleDml);
                    rdbSyncService.getDmlsPartition()[hash].add(syncItem);
                });
            } else {
                int hash = 0;
                List<SingleDml> singleDmls = SingleDml.dml2SingleDmls(dml, caseInsensitive);
                singleDmls.forEach(singleDml -> {
                    RdbSyncService.SyncItem syncItem = new RdbSyncService.SyncItem(config, singleDml);
                    rdbSyncService.getDmlsPartition()[hash].add(syncItem);
                });
            }
            return true;
        });
    }
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) SingleDml(com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml) SingleDml(com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml) ArrayList(java.util.ArrayList) MirrorDbConfig(com.alibaba.otter.canal.client.adapter.rdb.config.MirrorDbConfig) MappingConfig(com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig)

Aggregations

MappingConfig (com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig)2 SingleDml (com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml)2 MirrorDbConfig (com.alibaba.otter.canal.client.adapter.rdb.config.MirrorDbConfig)1 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)1 ArrayList (java.util.ArrayList)1