Search in sources :

Example 6 with DbMapping

use of com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig.DbMapping in project canal by alibaba.

the class PhoenixEtlService method importData.

/**
 * 导入数据
 */
public static EtlResult importData(DataSource srcDS, Connection targetDSConnection, MappingConfig config, List<String> params) {
    EtlResult etlResult = new EtlResult();
    AtomicLong successCount = new AtomicLong();
    List<String> errMsg = new ArrayList<>();
    String hbaseTable = "";
    try {
        if (config == null) {
            logger.error("Config is null!");
            etlResult.setSucceeded(false);
            etlResult.setErrorMessage("Config is null!");
            return etlResult;
        }
        boolean debug = params != null && params.get(0).equals("_debug");
        if (debug) {
            params = params.subList(1, params.size());
        }
        syncSchema(srcDS, targetDSConnection, config);
        DbMapping dbMapping = config.getDbMapping();
        long start = System.currentTimeMillis();
        // 拼接sql
        StringBuilder sql = new StringBuilder("SELECT * FROM " + dbMapping.getDatabase() + "." + dbMapping.getTable());
        // 拼接条件
        appendCondition(params, dbMapping, srcDS, sql);
        // 获取总数
        String countSql = "SELECT COUNT(1) FROM ( " + sql + ") _CNT ";
        long cnt = (Long) Util.sqlRS(srcDS, countSql, rs -> {
            Long count = null;
            try {
                if (rs.next()) {
                    count = ((Number) rs.getObject(1)).longValue();
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
            }
            return count == null ? 0 : count;
        });
        // 当大于1万条记录时开启多线程
        if (cnt >= 10000) {
            int threadCount = 3;
            long perThreadCnt = cnt / threadCount;
            ExecutorService executor = Util.newFixedThreadPool(threadCount, 5000L);
            for (int i = 0; i < threadCount; i++) {
                long offset = i * perThreadCnt;
                Long size = null;
                if (i != threadCount - 1) {
                    size = perThreadCnt;
                }
                String sqlFinal;
                if (size != null) {
                    sqlFinal = sql + " LIMIT " + offset + "," + size;
                } else {
                    sqlFinal = sql + " LIMIT " + offset + "," + cnt;
                }
                executor.execute(() -> executeSqlImport(srcDS, targetDSConnection, sqlFinal, dbMapping, successCount, errMsg, debug));
            }
            executor.shutdown();
            // noinspection StatementWithEmptyBody
            while (!executor.awaitTermination(3, TimeUnit.SECONDS)) ;
        } else {
            executeSqlImport(srcDS, targetDSConnection, sql.toString(), dbMapping, successCount, errMsg, debug);
        }
        logger.info(dbMapping.getTable() + " etl completed in: " + (System.currentTimeMillis() - start) / 1000 + "s!");
        etlResult.setResultMessage("导入目标表 " + SyncUtil.getDbTableName(dbMapping) + " 数据:" + successCount.get() + " 条");
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        errMsg.add(hbaseTable + " etl failed! ==>" + e.getMessage());
    }
    if (errMsg.isEmpty()) {
        etlResult.setSucceeded(true);
    } else {
        etlResult.setErrorMessage(Joiner.on("\n").join(errMsg));
    }
    return etlResult;
}
Also used : DatasourceConfig(com.alibaba.otter.canal.client.adapter.support.DatasourceConfig) java.sql(java.sql) java.util(java.util) TypeUtil(com.alibaba.otter.canal.client.adapter.phoenix.support.TypeUtil) Logger(org.slf4j.Logger) Util(com.alibaba.otter.canal.client.adapter.support.Util) LoggerFactory(org.slf4j.LoggerFactory) DbMapping(com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig.DbMapping) PhoenixSupportUtil(com.alibaba.otter.canal.client.adapter.phoenix.support.PhoenixSupportUtil) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) EtlResult(com.alibaba.otter.canal.client.adapter.support.EtlResult) MappingConfig(com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig) SyncUtil(com.alibaba.otter.canal.client.adapter.phoenix.support.SyncUtil) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) DataSource(javax.sql.DataSource) PhoenixAdapter(com.alibaba.otter.canal.client.adapter.phoenix.PhoenixAdapter) ExecutorService(java.util.concurrent.ExecutorService) Joiner(com.google.common.base.Joiner) AtomicLong(java.util.concurrent.atomic.AtomicLong) DbMapping(com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig.DbMapping) EtlResult(com.alibaba.otter.canal.client.adapter.support.EtlResult) AtomicLong(java.util.concurrent.atomic.AtomicLong) ExecutorService(java.util.concurrent.ExecutorService)

Example 7 with DbMapping

use of com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig.DbMapping in project canal by alibaba.

the class PhoenixSyncService method truncate.

/**
 * truncate操作   没有改动
 *
 * @param config MappingConfig
 */
private void truncate(BatchExecutor batchExecutor, MappingConfig config) throws SQLException {
    DbMapping dbMapping = config.getDbMapping();
    StringBuilder sql = new StringBuilder();
    sql.append("TRUNCATE TABLE ").append(SyncUtil.getDbTableName(dbMapping));
    batchExecutor.execute(sql.toString(), new ArrayList<>());
    if (logger.isTraceEnabled()) {
        logger.trace("Truncate target table, sql: {}", sql);
    }
}
Also used : DbMapping(com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig.DbMapping)

Aggregations

DbMapping (com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig.DbMapping)7 SQLException (java.sql.SQLException)4 SQLName (com.alibaba.druid.sql.ast.SQLName)2 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)2 ParserException (com.alibaba.druid.sql.parser.ParserException)2 MappingConfig (com.alibaba.otter.canal.client.adapter.phoenix.config.MappingConfig)2 SyncUtil (com.alibaba.otter.canal.client.adapter.phoenix.support.SyncUtil)2 TypeUtil (com.alibaba.otter.canal.client.adapter.phoenix.support.TypeUtil)2 Util (com.alibaba.otter.canal.client.adapter.support.Util)2 java.util (java.util)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 com.alibaba.druid.sql.ast.statement (com.alibaba.druid.sql.ast.statement)1 JdbcConstants (com.alibaba.druid.util.JdbcConstants)1 JSON (com.alibaba.fastjson.JSON)1 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)1 PhoenixAdapter (com.alibaba.otter.canal.client.adapter.phoenix.PhoenixAdapter)1 ConfigurationManager (com.alibaba.otter.canal.client.adapter.phoenix.config.ConfigurationManager)1 BatchExecutor (com.alibaba.otter.canal.client.adapter.phoenix.support.BatchExecutor)1