Search in sources :

Example 26 with Dml

use of com.alibaba.otter.canal.client.adapter.support.Dml 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)

Example 27 with Dml

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

the class TestSyncKudu method testSync.

@Test
public void testSync() {
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setGroupId("g1");
    dml.setTs(new Date().getTime());
    dml.setType("DELETE");
    dml.setDatabase("test1");
    dml.setTable("user");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("name", "liuyadong");
    data.put("role_id", 1L);
    data.put("c_time", new Date());
    dml.setData(dataList);
    kuduAdapter.sync(Collections.singletonList(dml));
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 28 with Dml

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

the class PhoenixSyncTest method test01.

@Test
public void test01() {
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("INSERT");
    dml.setDatabase("mytest");
    dml.setTable("user");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1);
    data.put("name", "sixPulseExcalibur");
    data.put("password", "123456");
    dml.setData(dataList);
    phoenixAdapter.sync(Collections.singletonList(dml));
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) Test(org.junit.Test)

Aggregations

Dml (com.alibaba.otter.canal.client.adapter.support.Dml)28 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)22 LinkedHashMap (java.util.LinkedHashMap)21 Map (java.util.Map)21 Date (java.util.Date)20 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)17 GetResponse (org.elasticsearch.action.get.GetResponse)17 DataSource (javax.sql.DataSource)15 StringUtils (org.apache.commons.lang.StringUtils)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 JSON (com.alibaba.fastjson.JSON)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)2 MappingConfig (com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig)2 SingleDml (com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml)2 Util (com.alibaba.otter.canal.client.adapter.support.Util)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1