Search in sources :

Example 1 with MappingConfig

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

the class HbaseAdapter method count.

@Override
public Map<String, Object> count(String task) {
    MappingConfig config = hbaseMapping.get(task);
    String hbaseTable = config.getHbaseMapping().getHbaseTable();
    long rowCount = 0L;
    try {
        HTable table = (HTable) hbaseTemplate.getConnection().getTable(TableName.valueOf(hbaseTable));
        Scan scan = new Scan();
        scan.setFilter(new FirstKeyOnlyFilter());
        ResultScanner resultScanner = table.getScanner(scan);
        for (Result result : resultScanner) {
            rowCount += result.size();
        }
    } catch (IOException e) {
        logger.error(e.getMessage(), e);
    }
    Map<String, Object> res = new LinkedHashMap<>();
    res.put("hbaseTable", hbaseTable);
    res.put("count", rowCount);
    return res;
}
Also used : MappingConfig(com.alibaba.otter.canal.client.adapter.hbase.config.MappingConfig) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) FirstKeyOnlyFilter(org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter) Scan(org.apache.hadoop.hbase.client.Scan) IOException(java.io.IOException) HTable(org.apache.hadoop.hbase.client.HTable) Result(org.apache.hadoop.hbase.client.Result) EtlResult(com.alibaba.otter.canal.client.adapter.support.EtlResult)

Example 2 with MappingConfig

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

the class HbaseAdapter method init.

@Override
public void init(OuterAdapterConfig configuration, Properties envProperties) {
    try {
        this.envProperties = envProperties;
        Map<String, MappingConfig> hbaseMappingTmp = MappingConfigLoader.load(envProperties);
        // 过滤不匹配的key的配置
        hbaseMappingTmp.forEach((key, mappingConfig) -> {
            if ((mappingConfig.getOuterAdapterKey() == null && configuration.getKey() == null) || (mappingConfig.getOuterAdapterKey() != null && mappingConfig.getOuterAdapterKey().equalsIgnoreCase(configuration.getKey()))) {
                hbaseMapping.put(key, mappingConfig);
            }
        });
        for (Map.Entry<String, MappingConfig> entry : hbaseMapping.entrySet()) {
            String configName = entry.getKey();
            MappingConfig mappingConfig = entry.getValue();
            String k;
            if (envProperties != null && !"tcp".equalsIgnoreCase(envProperties.getProperty("canal.conf.mode"))) {
                k = StringUtils.trimToEmpty(mappingConfig.getDestination()) + "-" + StringUtils.trimToEmpty(mappingConfig.getGroupId()) + "_" + mappingConfig.getHbaseMapping().getDatabase() + "-" + mappingConfig.getHbaseMapping().getTable();
            } else {
                k = StringUtils.trimToEmpty(mappingConfig.getDestination()) + "_" + mappingConfig.getHbaseMapping().getDatabase() + "-" + mappingConfig.getHbaseMapping().getTable();
            }
            Map<String, MappingConfig> configMap = mappingConfigCache.computeIfAbsent(k, k1 -> new ConcurrentHashMap<>());
            configMap.put(configName, mappingConfig);
        }
        Map<String, String> properties = configuration.getProperties();
        Configuration hbaseConfig = HBaseConfiguration.create();
        properties.forEach(hbaseConfig::set);
        hbaseTemplate = new HbaseTemplate(hbaseConfig);
        hbaseSyncService = new HbaseSyncService(hbaseTemplate);
        configMonitor = new HbaseConfigMonitor();
        configMonitor.init(this, envProperties);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : Configuration(org.apache.hadoop.conf.Configuration) HBaseConfiguration(org.apache.hadoop.hbase.HBaseConfiguration) HbaseTemplate(com.alibaba.otter.canal.client.adapter.hbase.support.HbaseTemplate) HbaseConfigMonitor(com.alibaba.otter.canal.client.adapter.hbase.monitor.HbaseConfigMonitor) IOException(java.io.IOException) MappingConfig(com.alibaba.otter.canal.client.adapter.hbase.config.MappingConfig) HbaseSyncService(com.alibaba.otter.canal.client.adapter.hbase.service.HbaseSyncService) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap)

Example 3 with MappingConfig

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

the class HbaseAdapter method etl.

@Override
public EtlResult etl(String task, List<String> params) {
    EtlResult etlResult = new EtlResult();
    MappingConfig config = hbaseMapping.get(task);
    HbaseEtlService hbaseEtlService = new HbaseEtlService(hbaseTemplate, config);
    if (config != null) {
        return hbaseEtlService.importData(params);
    } else {
        StringBuilder resultMsg = new StringBuilder();
        boolean resSucc = true;
        for (MappingConfig configTmp : hbaseMapping.values()) {
            // 取所有的destination为task的配置
            if (configTmp.getDestination().equals(task)) {
                EtlResult etlRes = hbaseEtlService.importData(params);
                if (!etlRes.getSucceeded()) {
                    resSucc = false;
                    resultMsg.append(etlRes.getErrorMessage()).append("\n");
                } else {
                    resultMsg.append(etlRes.getResultMessage()).append("\n");
                }
            }
        }
        if (resultMsg.length() > 0) {
            etlResult.setSucceeded(resSucc);
            if (resSucc) {
                etlResult.setResultMessage(resultMsg.toString());
            } else {
                etlResult.setErrorMessage(resultMsg.toString());
            }
            return etlResult;
        }
    }
    etlResult.setSucceeded(false);
    etlResult.setErrorMessage("Task not found");
    return etlResult;
}
Also used : MappingConfig(com.alibaba.otter.canal.client.adapter.hbase.config.MappingConfig) HbaseEtlService(com.alibaba.otter.canal.client.adapter.hbase.service.HbaseEtlService) EtlResult(com.alibaba.otter.canal.client.adapter.support.EtlResult)

Aggregations

MappingConfig (com.alibaba.otter.canal.client.adapter.hbase.config.MappingConfig)3 EtlResult (com.alibaba.otter.canal.client.adapter.support.EtlResult)2 IOException (java.io.IOException)2 HbaseConfigMonitor (com.alibaba.otter.canal.client.adapter.hbase.monitor.HbaseConfigMonitor)1 HbaseEtlService (com.alibaba.otter.canal.client.adapter.hbase.service.HbaseEtlService)1 HbaseSyncService (com.alibaba.otter.canal.client.adapter.hbase.service.HbaseSyncService)1 HbaseTemplate (com.alibaba.otter.canal.client.adapter.hbase.support.HbaseTemplate)1 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)1 Configuration (org.apache.hadoop.conf.Configuration)1 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)1 HTable (org.apache.hadoop.hbase.client.HTable)1 Result (org.apache.hadoop.hbase.client.Result)1 ResultScanner (org.apache.hadoop.hbase.client.ResultScanner)1 Scan (org.apache.hadoop.hbase.client.Scan)1 FirstKeyOnlyFilter (org.apache.hadoop.hbase.filter.FirstKeyOnlyFilter)1