Search in sources :

Example 6 with ESMapping

use of com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping in project canal by alibaba.

the class ESEtlService method importData.

public EtlResult importData(List<String> params) {
    ESMapping mapping = config.getEsMapping();
    logger.info("start etl to import data to index: {}", mapping.get_index());
    String sql = mapping.getSql();
    return importData(sql, params);
}
Also used : ESMapping(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping)

Example 7 with ESMapping

use of com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping in project canal by alibaba.

the class ESEtlService method executeSqlImport.

protected boolean executeSqlImport(DataSource ds, String sql, List<Object> values, AdapterConfig.AdapterMapping adapterMapping, AtomicLong impCount, List<String> errMsg) {
    try {
        ESMapping mapping = (ESMapping) adapterMapping;
        Util.sqlRS(ds, sql, values, rs -> {
            int count = 0;
            try {
                ESBulkRequest esBulkRequest = this.esConnection.new ES7xBulkRequest();
                long batchBegin = System.currentTimeMillis();
                while (rs.next()) {
                    Map<String, Object> esFieldData = new LinkedHashMap<>();
                    Object idVal = null;
                    for (FieldItem fieldItem : mapping.getSchemaItem().getSelectFields().values()) {
                        String fieldName = fieldItem.getFieldName();
                        if (mapping.getSkips().contains(fieldName)) {
                            continue;
                        }
                        // 如果是主键字段则不插入
                        if (fieldItem.getFieldName().equals(mapping.get_id())) {
                            idVal = esTemplate.getValFromRS(mapping, rs, fieldName, fieldName);
                        } else {
                            Object val = esTemplate.getValFromRS(mapping, rs, fieldName, fieldName);
                            esFieldData.put(Util.cleanColumn(fieldName), val);
                        }
                    }
                    if (!mapping.getRelations().isEmpty()) {
                        mapping.getRelations().forEach((relationField, relationMapping) -> {
                            Map<String, Object> relations = new HashMap<>();
                            relations.put("name", relationMapping.getName());
                            if (StringUtils.isNotEmpty(relationMapping.getParent())) {
                                FieldItem parentFieldItem = mapping.getSchemaItem().getSelectFields().get(relationMapping.getParent());
                                Object parentVal;
                                try {
                                    parentVal = esTemplate.getValFromRS(mapping, rs, parentFieldItem.getFieldName(), parentFieldItem.getFieldName());
                                } catch (SQLException e) {
                                    throw new RuntimeException(e);
                                }
                                if (parentVal != null) {
                                    relations.put("parent", parentVal.toString());
                                    esFieldData.put("$parent_routing", parentVal.toString());
                                }
                            }
                            esFieldData.put(Util.cleanColumn(relationField), relations);
                        });
                    }
                    if (idVal != null) {
                        String parentVal = (String) esFieldData.remove("$parent_routing");
                        if (mapping.isUpsert()) {
                            ESUpdateRequest esUpdateRequest = this.esConnection.new ES7xUpdateRequest(mapping.get_index(), idVal.toString()).setDoc(esFieldData).setDocAsUpsert(true);
                            if (StringUtils.isNotEmpty(parentVal)) {
                                esUpdateRequest.setRouting(parentVal);
                            }
                            esBulkRequest.add(esUpdateRequest);
                        } else {
                            ESIndexRequest esIndexRequest = this.esConnection.new ES7xIndexRequest(mapping.get_index(), idVal.toString()).setSource(esFieldData);
                            if (StringUtils.isNotEmpty(parentVal)) {
                                esIndexRequest.setRouting(parentVal);
                            }
                            esBulkRequest.add(esIndexRequest);
                        }
                    } else {
                        idVal = esFieldData.get(mapping.getPk());
                        ESSearchRequest esSearchRequest = this.esConnection.new ESSearchRequest(mapping.get_index()).setQuery(QueryBuilders.termQuery(mapping.getPk(), idVal)).size(10000);
                        SearchResponse response = esSearchRequest.getResponse();
                        for (SearchHit hit : response.getHits()) {
                            ESUpdateRequest esUpdateRequest = this.esConnection.new ES7xUpdateRequest(mapping.get_index(), hit.getId()).setDoc(esFieldData);
                            esBulkRequest.add(esUpdateRequest);
                        }
                    }
                    if (esBulkRequest.numberOfActions() % mapping.getCommitBatch() == 0 && esBulkRequest.numberOfActions() > 0) {
                        long esBatchBegin = System.currentTimeMillis();
                        ESBulkResponse rp = esBulkRequest.bulk();
                        if (rp.hasFailures()) {
                            rp.processFailBulkResponse("全量数据 etl 异常 ");
                        }
                        if (logger.isTraceEnabled()) {
                            logger.trace("全量数据批量导入批次耗时: {}, es执行时间: {}, 批次大小: {}, index; {}", (System.currentTimeMillis() - batchBegin), (System.currentTimeMillis() - esBatchBegin), esBulkRequest.numberOfActions(), mapping.get_index());
                        }
                        batchBegin = System.currentTimeMillis();
                        esBulkRequest.resetBulk();
                    }
                    count++;
                    impCount.incrementAndGet();
                }
                if (esBulkRequest.numberOfActions() > 0) {
                    long esBatchBegin = System.currentTimeMillis();
                    ESBulkResponse rp = esBulkRequest.bulk();
                    if (rp.hasFailures()) {
                        rp.processFailBulkResponse("全量数据 etl 异常 ");
                    }
                    if (logger.isTraceEnabled()) {
                        logger.trace("全量数据批量导入最后批次耗时: {}, es执行时间: {}, 批次大小: {}, index; {}", (System.currentTimeMillis() - batchBegin), (System.currentTimeMillis() - esBatchBegin), esBulkRequest.numberOfActions(), mapping.get_index());
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                errMsg.add(mapping.get_index() + " etl failed! ==>" + e.getMessage());
                throw new RuntimeException(e);
            }
            return count;
        });
        return true;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) ESSearchRequest(com.alibaba.otter.canal.client.adapter.es7x.support.ESConnection.ESSearchRequest) SQLException(java.sql.SQLException) LinkedHashMap(java.util.LinkedHashMap) SearchResponse(org.elasticsearch.action.search.SearchResponse) ESUpdateRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESUpdateRequest) ESMapping(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping) ESIndexRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESIndexRequest) ESBulkRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest) FieldItem(com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.FieldItem) ESBulkResponse(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESBulkResponse)

Example 8 with ESMapping

use of com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping in project canal by alibaba.

the class ES7xTemplate method updateByQuery.

@Override
public void updateByQuery(ESSyncConfig config, Map<String, Object> paramsTmp, Map<String, Object> esFieldData) {
    if (paramsTmp.isEmpty()) {
        return;
    }
    ESMapping mapping = config.getEsMapping();
    BoolQueryBuilder queryBuilder = QueryBuilders.boolQuery();
    paramsTmp.forEach((fieldName, value) -> queryBuilder.must(QueryBuilders.termsQuery(fieldName, value)));
    // 查询sql批量更新
    DataSource ds = DatasourceConfig.DATA_SOURCES.get(config.getDataSourceKey());
    StringBuilder sql = new StringBuilder("SELECT * FROM (" + mapping.getSql() + ") _v WHERE ");
    List<Object> values = new ArrayList<>();
    paramsTmp.forEach((fieldName, value) -> {
        sql.append("_v.").append(fieldName).append("=? AND ");
        values.add(value);
    });
    // TODO 直接外部包裹sql会导致全表扫描性能低, 待优化拼接内部where条件
    int len = sql.length();
    sql.delete(len - 4, len);
    Integer syncCount = (Integer) Util.sqlRS(ds, sql.toString(), values, rs -> {
        int count = 0;
        try {
            while (rs.next()) {
                Object idVal = getIdValFromRS(mapping, rs);
                append4Update(mapping, idVal, esFieldData);
                commitBulk();
                count++;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return count;
    });
    if (logger.isTraceEnabled()) {
        logger.trace("Update ES by query affected {} records", syncCount);
    }
}
Also used : DatasourceConfig(com.alibaba.otter.canal.client.adapter.support.DatasourceConfig) StringUtils(org.apache.commons.lang.StringUtils) ESSearchRequest(com.alibaba.otter.canal.client.adapter.es7x.support.ESConnection.ESSearchRequest) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) FieldItem(com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.FieldItem) Util(com.alibaba.otter.canal.client.adapter.support.Util) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) QueryBuilders(org.elasticsearch.index.query.QueryBuilders) ESSyncUtil(com.alibaba.otter.canal.client.adapter.es.core.support.ESSyncUtil) ArrayList(java.util.ArrayList) ConcurrentMap(java.util.concurrent.ConcurrentMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) ResultSet(java.sql.ResultSet) Map(java.util.Map) DataSource(javax.sql.DataSource) SearchResponse(org.elasticsearch.action.search.SearchResponse) ESBulkResponse(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESBulkResponse) ColumnItem(com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.ColumnItem) ESTemplate(com.alibaba.otter.canal.client.adapter.es.core.support.ESTemplate) SearchHit(org.elasticsearch.search.SearchHit) Logger(org.slf4j.Logger) ESUpdateRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESUpdateRequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ESBulkRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest) SchemaItem(com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem) List(java.util.List) ESDeleteRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESDeleteRequest) ESIndexRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESIndexRequest) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) ESMapping(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping) BoolQueryBuilder(org.elasticsearch.index.query.BoolQueryBuilder) ESMapping(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping) ArrayList(java.util.ArrayList) SQLException(java.sql.SQLException) DataSource(javax.sql.DataSource)

Example 9 with ESMapping

use of com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping in project canal by alibaba.

the class ESEtlService method executeSqlImport.

protected boolean executeSqlImport(DataSource ds, String sql, List<Object> values, AdapterConfig.AdapterMapping adapterMapping, AtomicLong impCount, List<String> errMsg) {
    try {
        ESMapping mapping = (ESMapping) adapterMapping;
        Util.sqlRS(ds, sql, values, rs -> {
            int count = 0;
            try {
                ESBulkRequest esBulkRequest = this.esConnection.new ES6xBulkRequest();
                long batchBegin = System.currentTimeMillis();
                while (rs.next()) {
                    Map<String, Object> esFieldData = new LinkedHashMap<>();
                    Object idVal = null;
                    for (FieldItem fieldItem : mapping.getSchemaItem().getSelectFields().values()) {
                        String fieldName = fieldItem.getFieldName();
                        if (mapping.getSkips().contains(fieldName)) {
                            continue;
                        }
                        // 如果是主键字段则不插入
                        if (fieldItem.getFieldName().equals(mapping.get_id())) {
                            idVal = esTemplate.getValFromRS(mapping, rs, fieldName, fieldName);
                        } else {
                            Object val = esTemplate.getValFromRS(mapping, rs, fieldName, fieldName);
                            esFieldData.put(Util.cleanColumn(fieldName), val);
                        }
                    }
                    if (!mapping.getRelations().isEmpty()) {
                        mapping.getRelations().forEach((relationField, relationMapping) -> {
                            Map<String, Object> relations = new HashMap<>();
                            relations.put("name", relationMapping.getName());
                            if (StringUtils.isNotEmpty(relationMapping.getParent())) {
                                FieldItem parentFieldItem = mapping.getSchemaItem().getSelectFields().get(relationMapping.getParent());
                                Object parentVal;
                                try {
                                    parentVal = esTemplate.getValFromRS(mapping, rs, parentFieldItem.getFieldName(), parentFieldItem.getFieldName());
                                } catch (SQLException e) {
                                    throw new RuntimeException(e);
                                }
                                if (parentVal != null) {
                                    relations.put("parent", parentVal.toString());
                                    esFieldData.put("$parent_routing", parentVal.toString());
                                }
                            }
                            esFieldData.put(Util.cleanColumn(relationField), relations);
                        });
                    }
                    if (idVal != null) {
                        String parentVal = (String) esFieldData.remove("$parent_routing");
                        if (mapping.isUpsert()) {
                            ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.get_index(), mapping.get_type(), idVal.toString()).setDoc(esFieldData).setDocAsUpsert(true);
                            if (StringUtils.isNotEmpty(parentVal)) {
                                esUpdateRequest.setRouting(parentVal);
                            }
                            esBulkRequest.add(esUpdateRequest);
                        } else {
                            ESIndexRequest esIndexRequest = this.esConnection.new ES6xIndexRequest(mapping.get_index(), mapping.get_type(), idVal.toString()).setSource(esFieldData);
                            if (StringUtils.isNotEmpty(parentVal)) {
                                esIndexRequest.setRouting(parentVal);
                            }
                            esBulkRequest.add(esIndexRequest);
                        }
                    } else {
                        idVal = esFieldData.get(mapping.getPk());
                        ESSearchRequest esSearchRequest = this.esConnection.new ESSearchRequest(mapping.get_index(), mapping.get_type()).setQuery(QueryBuilders.termQuery(mapping.getPk(), idVal)).size(10000);
                        SearchResponse response = esSearchRequest.getResponse();
                        for (SearchHit hit : response.getHits()) {
                            ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.get_index(), mapping.get_type(), hit.getId()).setDoc(esFieldData);
                            esBulkRequest.add(esUpdateRequest);
                        }
                    }
                    if (esBulkRequest.numberOfActions() % mapping.getCommitBatch() == 0 && esBulkRequest.numberOfActions() > 0) {
                        long esBatchBegin = System.currentTimeMillis();
                        ESBulkResponse rp = esBulkRequest.bulk();
                        if (rp.hasFailures()) {
                            rp.processFailBulkResponse("全量数据 etl 异常 ");
                        }
                        if (logger.isTraceEnabled()) {
                            logger.trace("全量数据批量导入批次耗时: {}, es执行时间: {}, 批次大小: {}, index; {}", (System.currentTimeMillis() - batchBegin), (System.currentTimeMillis() - esBatchBegin), esBulkRequest.numberOfActions(), mapping.get_index());
                        }
                        batchBegin = System.currentTimeMillis();
                        esBulkRequest.resetBulk();
                    }
                    count++;
                    impCount.incrementAndGet();
                }
                if (esBulkRequest.numberOfActions() > 0) {
                    long esBatchBegin = System.currentTimeMillis();
                    ESBulkResponse rp = esBulkRequest.bulk();
                    if (rp.hasFailures()) {
                        rp.processFailBulkResponse("全量数据 etl 异常 ");
                    }
                    if (logger.isTraceEnabled()) {
                        logger.trace("全量数据批量导入最后批次耗时: {}, es执行时间: {}, 批次大小: {}, index; {}", (System.currentTimeMillis() - batchBegin), (System.currentTimeMillis() - esBatchBegin), esBulkRequest.numberOfActions(), mapping.get_index());
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage(), e);
                errMsg.add(mapping.get_index() + " etl failed! ==>" + e.getMessage());
                throw new RuntimeException(e);
            }
            return count;
        });
        return true;
    } catch (Exception e) {
        logger.error(e.getMessage(), e);
        return false;
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) SQLException(java.sql.SQLException) ESSearchRequest(com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest) SQLException(java.sql.SQLException) LinkedHashMap(java.util.LinkedHashMap) SearchResponse(org.elasticsearch.action.search.SearchResponse) ESUpdateRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESUpdateRequest) ESMapping(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping) ESIndexRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESIndexRequest) ESBulkRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest) FieldItem(com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.FieldItem) ESBulkResponse(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESBulkResponse)

Example 10 with ESMapping

use of com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping in project canal by alibaba.

the class ESSyncService method mainTableInsert.

/**
 * 主表(单表)复杂字段insert
 *
 * @param config es配置
 * @param dml dml信息
 * @param data 单行dml数据
 */
private void mainTableInsert(ESSyncConfig config, Dml dml, Map<String, Object> data) {
    ESMapping mapping = config.getEsMapping();
    String sql = mapping.getSql();
    String condition = ESSyncUtil.pkConditionSql(mapping, data);
    sql = ESSyncUtil.appendCondition(sql, condition);
    DataSource ds = DatasourceConfig.DATA_SOURCES.get(config.getDataSourceKey());
    if (logger.isTraceEnabled()) {
        logger.trace("Main table insert to es index by query sql, destination:{}, table: {}, index: {}, sql: {}", config.getDestination(), dml.getTable(), mapping.get_index(), sql.replace("\n", " "));
    }
    Util.sqlRS(ds, sql, rs -> {
        try {
            while (rs.next()) {
                Map<String, Object> esFieldData = new LinkedHashMap<>();
                Object idVal = esTemplate.getESDataFromRS(mapping, rs, esFieldData);
                if (logger.isTraceEnabled()) {
                    logger.trace("Main table insert to es index by query sql, destination:{}, table: {}, index: {}, id: {}", config.getDestination(), dml.getTable(), mapping.get_index(), idVal);
                }
                esTemplate.insert(mapping, idVal, esFieldData);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return 0;
    });
}
Also used : ESMapping(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping) DataSource(javax.sql.DataSource) LinkedHashMap(java.util.LinkedHashMap)

Aggregations

ESMapping (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping)16 LinkedHashMap (java.util.LinkedHashMap)13 FieldItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.FieldItem)9 Map (java.util.Map)7 DataSource (javax.sql.DataSource)7 ColumnItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.ColumnItem)5 ArrayList (java.util.ArrayList)5 List (java.util.List)5 SchemaItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem)4 ESBulkRequest (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest)4 ESBulkResponse (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESBulkResponse)4 ESIndexRequest (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESIndexRequest)4 ESUpdateRequest (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESUpdateRequest)4 SQLException (java.sql.SQLException)4 HashMap (java.util.HashMap)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 SearchHit (org.elasticsearch.search.SearchHit)4 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)2 TableItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.TableItem)2 ESDeleteRequest (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESDeleteRequest)2