Search in sources :

Example 1 with TableStoreWriter

use of com.alicloud.openservices.tablestore.TableStoreWriter in project canal by alibaba.

the class TablestoreAdapter method init.

@Override
public void init(OuterAdapterConfig configuration, Properties envProperties) {
    this.envProperties = envProperties;
    this.configuration = configuration;
    Map<String, MappingConfig> tablestoreMappingTmp = ConfigLoader.load(envProperties);
    // 过滤不匹配的key的配置
    tablestoreMappingTmp.forEach((key, mappingConfig) -> {
        if ((mappingConfig.getOuterAdapterKey() == null && configuration.getKey() == null) || (mappingConfig.getOuterAdapterKey() != null && mappingConfig.getOuterAdapterKey().equalsIgnoreCase(configuration.getKey()))) {
            tablestoreMapping.put(key, mappingConfig);
            mappingConfig.getDbMapping().init(mappingConfig);
        }
    });
    if (tablestoreMapping.isEmpty()) {
        throw new RuntimeException("No tablestore adapter found for config key: " + configuration.getKey());
    }
    Map<String, String> properties = configuration.getProperties();
    for (Map.Entry<String, MappingConfig> entry : tablestoreMapping.entrySet()) {
        String configName = entry.getKey();
        MappingConfig mappingConfig = entry.getValue();
        String key;
        if (envProperties != null && !"tcp".equalsIgnoreCase(envProperties.getProperty("canal.conf.mode"))) {
            key = StringUtils.trimToEmpty(mappingConfig.getDestination()) + "-" + StringUtils.trimToEmpty(mappingConfig.getGroupId()) + "_" + mappingConfig.getDbMapping().getDatabase() + "-" + mappingConfig.getDbMapping().getTable();
        } else {
            key = StringUtils.trimToEmpty(mappingConfig.getDestination()) + "_" + mappingConfig.getDbMapping().getDatabase() + "-" + mappingConfig.getDbMapping().getTable();
        }
        Map<String, MappingConfig> configMap = mappingConfigCache.computeIfAbsent(key, k1 -> new ConcurrentHashMap<>());
        configMap.put(configName, mappingConfig);
        // 构建对应的 TableStoreWriter
        ServiceCredentials credentials = new DefaultCredentials(properties.get(PropertyConstants.TABLESTORE_ACCESSSECRETID), properties.get(PropertyConstants.TABLESTORE_ACCESSSECRETKEY));
        WriterConfig config = getWriterConfig(mappingConfig);
        TableStoreWriter writer = new DefaultTableStoreWriter(properties.get(PropertyConstants.TABLESTORE_ENDPOINT), credentials, properties.get(PropertyConstants.TABLESTORE_INSTANCENAME), mappingConfig.getDbMapping().getTargetTable(), config, null);
        Map<String, TableStoreWriter> config2writerMap = writerCache.computeIfAbsent(key, k1 -> new ConcurrentHashMap<>());
        config2writerMap.put(configName, writer);
    }
    tablestoreSyncService = new TablestoreSyncService();
}
Also used : DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter) TableStoreWriter(com.alicloud.openservices.tablestore.TableStoreWriter) ServiceCredentials(com.alicloud.openservices.tablestore.core.auth.ServiceCredentials) TablestoreSyncService(com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreSyncService) WriterConfig(com.alicloud.openservices.tablestore.writer.WriterConfig) DefaultCredentials(com.alicloud.openservices.tablestore.core.auth.DefaultCredentials) MappingConfig(com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig) DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter)

Example 2 with TableStoreWriter

use of com.alicloud.openservices.tablestore.TableStoreWriter in project canal by alibaba.

the class TablestoreAdapter method etl.

@Override
public EtlResult etl(String task, List<String> params) {
    EtlResult etlResult = new EtlResult();
    MappingConfig config = tablestoreMapping.get(task);
    if (config == null) {
        etlResult.setErrorMessage("can not find config for " + task);
        etlResult.setSucceeded(false);
        return etlResult;
    }
    TableStoreWriter writer = null;
    try {
        writer = buildEtlWriter(configuration, config);
        TablestoreEtlService rdbEtlService = new TablestoreEtlService(writer, config);
        rdbEtlService.importData(params);
        etlResult.setSucceeded(true);
        return etlResult;
    } catch (Exception e) {
        logger.error("Error while etl for task " + task, e);
        etlResult.setSucceeded(false);
        etlResult.setErrorMessage(e.getMessage());
        return etlResult;
    } finally {
        if (writer != null) {
            writer.close();
        }
    }
}
Also used : DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter) TableStoreWriter(com.alicloud.openservices.tablestore.TableStoreWriter) MappingConfig(com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig) EtlResult(com.alibaba.otter.canal.client.adapter.support.EtlResult) TablestoreEtlService(com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreEtlService)

Example 3 with TableStoreWriter

use of com.alicloud.openservices.tablestore.TableStoreWriter in project canal by alibaba.

the class TablestoreAdapter method sync.

@Override
public void sync(List<Dml> dmls) {
    if (dmls == null || dmls.isEmpty()) {
        return;
    }
    try {
        Set<TableStoreWriter> writerSet = new HashSet<>();
        List<Future<WriterResult>> futureList = new ArrayList<>();
        for (Dml dml : dmls) {
            String destination = StringUtils.trimToEmpty(dml.getDestination());
            String groupId = StringUtils.trimToEmpty(dml.getGroupId());
            String database = dml.getDatabase();
            String table = dml.getTable();
            String key;
            if (envProperties != null && !"tcp".equalsIgnoreCase(envProperties.getProperty("canal.conf.mode"))) {
                key = destination + "-" + groupId + "_" + database + "-" + table;
            } else {
                key = destination + "_" + database + "-" + table;
            }
            Map<String, MappingConfig> configMap = mappingConfigCache.get(key);
            if (configMap == null) {
                // 可能有dml中涉及到的表并没有出现在配置中,说明此类dml并不需要同步
                continue;
            }
            Map<String, TableStoreWriter> writerMap = writerCache.get(key);
            for (Map.Entry<String, MappingConfig> entry : configMap.entrySet()) {
                TableStoreWriter w = writerMap.get(entry.getKey());
                // 拿到所有future用于判定失败的记录
                Future<WriterResult> futureTemp = tablestoreSyncService.sync(entry.getValue(), dml, w);
                if (futureTemp != null) {
                    writerSet.add(w);
                    futureList.add(futureTemp);
                }
            }
        }
        if (writerSet.isEmpty()) {
            return;
        }
        writerSet.forEach(e -> e.flush());
        List<WriterResult.RowChangeStatus> totalFailedRows = new ArrayList<>();
        for (Future<WriterResult> future : futureList) {
            try {
                WriterResult result = future.get();
                List<WriterResult.RowChangeStatus> failedRows = result.getFailedRows();
                if (!CollectionUtils.isEmpty(failedRows)) {
                    totalFailedRows.addAll(failedRows);
                }
            } catch (InterruptedException e) {
                logger.info("InterruptedException", e);
                Thread.currentThread().interrupt();
            } catch (ExecutionException e) {
                throw new RuntimeException(e);
            }
        }
        if (!CollectionUtils.isEmpty(totalFailedRows)) {
            // 认为有失败的请求
            List<String> msgs = totalFailedRows.stream().map(e -> buildErrorMsgForFailedRowChange(e)).collect(Collectors.toList());
            throw new RuntimeException("Failed rows:" + org.springframework.util.StringUtils.collectionToDelimitedString(msgs, ",", "[", "]"));
        }
    } catch (Exception e) {
        throw e;
    }
}
Also used : DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter) TableStoreWriter(com.alicloud.openservices.tablestore.TableStoreWriter) java.util(java.util) StringUtils(org.apache.commons.lang.StringUtils) OuterAdapter(com.alibaba.otter.canal.client.adapter.OuterAdapter) WriteMode(com.alicloud.openservices.tablestore.writer.enums.WriteMode) Dml(com.alibaba.otter.canal.client.adapter.support.Dml) MappingConfig(com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig) LoggerFactory(org.slf4j.LoggerFactory) EtlResult(com.alibaba.otter.canal.client.adapter.support.EtlResult) DefaultCredentials(com.alicloud.openservices.tablestore.core.auth.DefaultCredentials) ServiceCredentials(com.alicloud.openservices.tablestore.core.auth.ServiceCredentials) PropertyConstants(com.alibaba.otter.canal.client.adapter.tablestore.common.PropertyConstants) DispatchMode(com.alicloud.openservices.tablestore.writer.enums.DispatchMode) WriterRetryStrategy(com.alicloud.openservices.tablestore.writer.enums.WriterRetryStrategy) WriterConfig(com.alicloud.openservices.tablestore.writer.WriterConfig) SPI(com.alibaba.otter.canal.client.adapter.support.SPI) BatchRequestType(com.alicloud.openservices.tablestore.writer.enums.BatchRequestType) DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter) Logger(org.slf4j.Logger) java.util.concurrent(java.util.concurrent) TablestoreSyncService(com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreSyncService) OuterAdapterConfig(com.alibaba.otter.canal.client.adapter.support.OuterAdapterConfig) Collectors(java.util.stream.Collectors) TableStoreWriter(com.alicloud.openservices.tablestore.TableStoreWriter) WriterResult(com.alicloud.openservices.tablestore.writer.WriterResult) TablestoreEtlService(com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreEtlService) CollectionUtils(org.springframework.util.CollectionUtils) ConfigLoader(com.alibaba.otter.canal.client.adapter.tablestore.config.ConfigLoader) Dml(com.alibaba.otter.canal.client.adapter.support.Dml) MappingConfig(com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig) WriterResult(com.alicloud.openservices.tablestore.writer.WriterResult)

Example 4 with TableStoreWriter

use of com.alicloud.openservices.tablestore.TableStoreWriter in project canal by alibaba.

the class TablestoreAdapter method buildEtlWriter.

/**
 * 构造批量导入的writer
 * @param configuration
 * @param mappingConfig
 * @return
 */
private TableStoreWriter buildEtlWriter(OuterAdapterConfig configuration, MappingConfig mappingConfig) {
    Map<String, String> properties = configuration.getProperties();
    ServiceCredentials credentials = new DefaultCredentials(properties.get(PropertyConstants.TABLESTORE_ACCESSSECRETID), properties.get(PropertyConstants.TABLESTORE_ACCESSSECRETKEY));
    WriterConfig config = getWriterConfig(mappingConfig);
    config.setBucketCount(3);
    config.setAllowDuplicatedRowInBatchRequest(true);
    config.setConcurrency(8);
    config.setWriteMode(WriteMode.PARALLEL);
    TableStoreWriter writer = new DefaultTableStoreWriter(properties.get(PropertyConstants.TABLESTORE_ENDPOINT), credentials, properties.get(PropertyConstants.TABLESTORE_INSTANCENAME), mappingConfig.getDbMapping().getTargetTable(), config, null);
    return writer;
}
Also used : DefaultCredentials(com.alicloud.openservices.tablestore.core.auth.DefaultCredentials) DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter) TableStoreWriter(com.alicloud.openservices.tablestore.TableStoreWriter) ServiceCredentials(com.alicloud.openservices.tablestore.core.auth.ServiceCredentials) DefaultTableStoreWriter(com.alicloud.openservices.tablestore.DefaultTableStoreWriter) WriterConfig(com.alicloud.openservices.tablestore.writer.WriterConfig)

Aggregations

DefaultTableStoreWriter (com.alicloud.openservices.tablestore.DefaultTableStoreWriter)4 TableStoreWriter (com.alicloud.openservices.tablestore.TableStoreWriter)4 MappingConfig (com.alibaba.otter.canal.client.adapter.tablestore.config.MappingConfig)3 DefaultCredentials (com.alicloud.openservices.tablestore.core.auth.DefaultCredentials)3 ServiceCredentials (com.alicloud.openservices.tablestore.core.auth.ServiceCredentials)3 WriterConfig (com.alicloud.openservices.tablestore.writer.WriterConfig)3 EtlResult (com.alibaba.otter.canal.client.adapter.support.EtlResult)2 TablestoreEtlService (com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreEtlService)2 TablestoreSyncService (com.alibaba.otter.canal.client.adapter.tablestore.service.TablestoreSyncService)2 OuterAdapter (com.alibaba.otter.canal.client.adapter.OuterAdapter)1 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)1 OuterAdapterConfig (com.alibaba.otter.canal.client.adapter.support.OuterAdapterConfig)1 SPI (com.alibaba.otter.canal.client.adapter.support.SPI)1 PropertyConstants (com.alibaba.otter.canal.client.adapter.tablestore.common.PropertyConstants)1 ConfigLoader (com.alibaba.otter.canal.client.adapter.tablestore.config.ConfigLoader)1 WriterResult (com.alicloud.openservices.tablestore.writer.WriterResult)1 BatchRequestType (com.alicloud.openservices.tablestore.writer.enums.BatchRequestType)1 DispatchMode (com.alicloud.openservices.tablestore.writer.enums.DispatchMode)1 WriteMode (com.alicloud.openservices.tablestore.writer.enums.WriteMode)1 WriterRetryStrategy (com.alicloud.openservices.tablestore.writer.enums.WriterRetryStrategy)1