Search in sources :

Example 1 with Dml

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

the class KuduAdapter method sync.

@Override
public void sync(List<Dml> dmls) {
    if (dmls == null || dmls.isEmpty()) {
        return;
    }
    for (Dml dml : dmls) {
        if (dml == null) {
            return;
        }
        String destination = StringUtils.trimToEmpty(dml.getDestination());
        String groupId = StringUtils.trimToEmpty(dml.getGroupId());
        String database = dml.getDatabase();
        String table = dml.getTable();
        Map<String, KuduMappingConfig> configMap;
        if (envProperties != null && !"tcp".equalsIgnoreCase(envProperties.getProperty("canal.conf.mode"))) {
            configMap = mappingConfigCache.get(destination + "-" + groupId + "_" + database + "-" + table);
        } else {
            configMap = mappingConfigCache.get(destination + "_" + database + "-" + table);
        }
        if (configMap != null) {
            List<KuduMappingConfig> configs = new ArrayList<>();
            configMap.values().forEach(config -> {
                if (StringUtils.isNotEmpty(config.getGroupId())) {
                    if (config.getGroupId().equals(dml.getGroupId())) {
                        configs.add(config);
                    }
                } else {
                    configs.add(config);
                }
            });
            if (!configs.isEmpty()) {
                configs.forEach(config -> kuduSyncService.sync(config, dml));
            } else {
                logger.error("groupID didn't mach,please check your gruopId ");
            }
        } else {
            logger.error("{} config didn't get,please check your map key ", destination + "_" + database + "-" + table);
        }
    }
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) KuduMappingConfig(com.alibaba.otter.canal.client.adapter.kudu.config.KuduMappingConfig) ArrayList(java.util.ArrayList)

Example 2 with Dml

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

the class AdapterProcessor method batchSync.

/**
 * 分批同步
 *
 * @param dmls
 * @param adapter
 */
private void batchSync(List<Dml> dmls, OuterAdapter adapter) {
    // 分批同步
    if (dmls.size() <= canalClientConfig.getSyncBatchSize()) {
        adapter.sync(dmls);
    } else {
        int len = 0;
        List<Dml> dmlsBatch = new ArrayList<>();
        for (Dml dml : dmls) {
            dmlsBatch.add(dml);
            if (dml.getData() == null || dml.getData().isEmpty()) {
                len += 1;
            } else {
                len += dml.getData().size();
            }
            if (len >= canalClientConfig.getSyncBatchSize()) {
                adapter.sync(dmlsBatch);
                dmlsBatch.clear();
                len = 0;
            }
        }
        if (!dmlsBatch.isEmpty()) {
            adapter.sync(dmlsBatch);
        }
    }
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) ArrayList(java.util.ArrayList)

Example 3 with Dml

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

the class PhoenixSyncService method sync.

/**
 * 批量同步回调
 *
 * @param dmls     批量 DML
 * @param function 回调方法
 */
private void sync(List<Dml> dmls, Function<Dml, Boolean> function) {
    try {
        boolean toExecute = false;
        for (Dml dml : dmls) {
            if (!toExecute) {
                toExecute = function.apply(dml);
            } else {
                function.apply(dml);
            }
        }
        if (toExecute) {
            List<Future<Boolean>> futures = new ArrayList<>();
            for (int i = 0; i < threads; i++) {
                int j = i;
                if (dmlsPartition[j].isEmpty()) {
                    // bypass
                    continue;
                }
                futures.add(executorThreads[i].submit(() -> {
                    try {
                        dmlsPartition[j].forEach(syncItem -> sync(batchExecutors[j], syncItem.config, syncItem.singleDml));
                        // 相对于RDB同步 少了  dmlsPartition[j].clear();
                        // 在 try catch中获取异常后再次执行一次batchExecutors[j].commit()
                        batchExecutors[j].commit();
                        return true;
                    } catch (Throwable e) {
                        batchExecutors[j].rollback();
                        if (!e.getClass().getName().endsWith("ColumnNotFoundException") && !e.getClass().getName().endsWith("TableNotFoundException")) {
                            throw new RuntimeException(e);
                        }
                        logger.info("table or column not found: " + e.getMessage());
                        boolean synced = false;
                        for (SyncItem syncItem : dmlsPartition[j]) {
                            if (PhoenixEtlService.syncSchema(batchExecutors[j].getConn(), syncItem.config)) {
                                synced = true;
                            }
                        }
                        if (!synced) {
                            throw new RuntimeException(e);
                        }
                        dmlsPartition[j].forEach(syncItem -> sync(batchExecutors[j], syncItem.config, syncItem.singleDml));
                        try {
                            batchExecutors[j].commit();
                            return true;
                        } catch (Throwable e1) {
                            batchExecutors[j].rollback();
                            throw new RuntimeException(e1);
                        }
                    } finally {
                        dmlsPartition[j].clear();
                    }
                }));
            }
            futures.forEach(future -> {
                try {
                    future.get();
                } catch (ExecutionException | InterruptedException e) {
                    throw new RuntimeException(e);
                }
            });
        }
    } finally {
        for (BatchExecutor batchExecutor : batchExecutors) {
            if (batchExecutor != null) {
                batchExecutor.close();
            }
        }
    }
}
Also used : BatchExecutor(com.alibaba.otter.canal.client.adapter.phoenix.support.BatchExecutor) StringUtils(org.apache.commons.lang.StringUtils) java.util(java.util) Connection(java.sql.Connection) SerializerFeature(com.alibaba.fastjson.serializer.SerializerFeature) Util(com.alibaba.otter.canal.client.adapter.support.Util) Dml(com.alibaba.otter.canal.client.adapter.support.Dml) LoggerFactory(org.slf4j.LoggerFactory) SQLName(com.alibaba.druid.sql.ast.SQLName) DbMapping(com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig.DbMapping) ParserException(com.alibaba.druid.sql.parser.ParserException) Function(java.util.function.Function) SQLException(java.sql.SQLException) SQLUtils(com.alibaba.druid.sql.SQLUtils) TypeUtil(com.alibaba.otter.canal.client.adapter.phoenix.support.TypeUtil) Logger(org.slf4j.Logger) java.util.concurrent(java.util.concurrent) ConfigurationManager(com.alibaba.otter.canal.client.adapter.phoenix.config.ConfigurationManager) MappingConfig(com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig) SyncUtil(com.alibaba.otter.canal.client.adapter.phoenix.support.SyncUtil) JSON(com.alibaba.fastjson.JSON) SingleDml(com.alibaba.otter.canal.client.adapter.phoenix.support.SingleDml) JdbcConstants(com.alibaba.druid.util.JdbcConstants) SQLStatement(com.alibaba.druid.sql.ast.SQLStatement) ResultSetMetaData(java.sql.ResultSetMetaData) com.alibaba.druid.sql.ast.statement(com.alibaba.druid.sql.ast.statement) BatchExecutor(com.alibaba.otter.canal.client.adapter.phoenix.support.BatchExecutor) Dml(com.alibaba.otter.canal.client.adapter.support.Dml) SingleDml(com.alibaba.otter.canal.client.adapter.phoenix.support.SingleDml)

Example 4 with Dml

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

the class PhoenixSyncTest method test02.

@Test
public void test02() {
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("UPDATE");
    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", "sixPulseExcalibur2");
    dml.setData(dataList);
    List<Map<String, Object>> oldList = new ArrayList<>();
    Map<String, Object> old = new LinkedHashMap<>();
    oldList.add(old);
    old.put("name", "sixPulseExcalibur");
    dml.setOld(oldList);
    phoenixAdapter.sync(Collections.singletonList(dml));
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) Test(org.junit.Test)

Example 5 with Dml

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

the class OracleSyncTest 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", 1L);
    data.put("name", "Eric");
    data.put("role_id", 1L);
    data.put("c_time", new Date());
    data.put("test1", "sdfasdfawe中国asfwef");
    dml.setData(dataList);
    rdbAdapter.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)

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