Search in sources :

Example 1 with ESSearchRequest

use of com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest in project canal by alibaba.

the class ES6xTemplate method append4Update.

private void append4Update(ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
    if (mapping.get_id() != null) {
        String parentVal = (String) esFieldData.remove("$parent_routing");
        if (mapping.isUpsert()) {
            ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.get_index(), mapping.get_type(), pkVal.toString()).setDoc(esFieldData).setDocAsUpsert(true);
            if (StringUtils.isNotEmpty(parentVal)) {
                esUpdateRequest.setRouting(parentVal);
            }
            getBulk().add(esUpdateRequest);
        } else {
            ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.get_index(), mapping.get_type(), pkVal.toString()).setDoc(esFieldData);
            if (StringUtils.isNotEmpty(parentVal)) {
                esUpdateRequest.setRouting(parentVal);
            }
            getBulk().add(esUpdateRequest);
        }
    } else {
        ESSearchRequest esSearchRequest = this.esConnection.new ESSearchRequest(mapping.get_index(), mapping.get_type()).setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal)).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);
            getBulk().add(esUpdateRequest);
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ESUpdateRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESUpdateRequest) ESSearchRequest(com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 2 with ESSearchRequest

use of com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest in project canal by alibaba.

the class ES6xTemplate method delete.

@Override
public void delete(ESSyncConfig.ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
    if (mapping.get_id() != null) {
        ESDeleteRequest esDeleteRequest = this.esConnection.new ES6xDeleteRequest(mapping.get_index(), mapping.get_type(), pkVal.toString());
        getBulk().add(esDeleteRequest);
        commitBulk();
    } else {
        ESSearchRequest esSearchRequest = this.esConnection.new ESSearchRequest(mapping.get_index(), mapping.get_type()).setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal)).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);
            getBulk().add(esUpdateRequest);
            commitBulk();
        }
    }
}
Also used : ESDeleteRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESDeleteRequest) SearchHit(org.elasticsearch.search.SearchHit) ESUpdateRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESUpdateRequest) ESSearchRequest(com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Example 3 with ESSearchRequest

use of com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest 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 4 with ESSearchRequest

use of com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest in project canal by alibaba.

the class ES6xTemplate method insert.

@Override
public void insert(ESSyncConfig.ESMapping mapping, Object pkVal, Map<String, Object> esFieldData) {
    if (mapping.get_id() != null) {
        String parentVal = (String) esFieldData.remove("$parent_routing");
        if (mapping.isUpsert()) {
            ESUpdateRequest updateRequest = esConnection.new ES6xUpdateRequest(mapping.get_index(), mapping.get_type(), pkVal.toString()).setDoc(esFieldData).setDocAsUpsert(true);
            if (StringUtils.isNotEmpty(parentVal)) {
                updateRequest.setRouting(parentVal);
            }
            getBulk().add(updateRequest);
        } else {
            ESIndexRequest indexRequest = esConnection.new ES6xIndexRequest(mapping.get_index(), mapping.get_type(), pkVal.toString()).setSource(esFieldData);
            if (StringUtils.isNotEmpty(parentVal)) {
                indexRequest.setRouting(parentVal);
            }
            getBulk().add(indexRequest);
        }
        commitBulk();
    } else {
        ESSearchRequest esSearchRequest = this.esConnection.new ESSearchRequest(mapping.get_index(), mapping.get_type()).setQuery(QueryBuilders.termQuery(mapping.getPk(), pkVal)).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);
            getBulk().add(esUpdateRequest);
            commitBulk();
        }
    }
}
Also used : SearchHit(org.elasticsearch.search.SearchHit) ESUpdateRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESUpdateRequest) ESIndexRequest(com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESIndexRequest) ESSearchRequest(com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest) SearchResponse(org.elasticsearch.action.search.SearchResponse)

Aggregations

ESUpdateRequest (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESUpdateRequest)4 ESSearchRequest (com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 SearchHit (org.elasticsearch.search.SearchHit)4 ESIndexRequest (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESIndexRequest)2 ESMapping (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping)1 FieldItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.FieldItem)1 ESBulkRequest (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest)1 ESBulkResponse (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESBulkResponse)1 ESDeleteRequest (com.alibaba.otter.canal.client.adapter.es.core.support.ESBulkRequest.ESDeleteRequest)1 SQLException (java.sql.SQLException)1 HashMap (java.util.HashMap)1 LinkedHashMap (java.util.LinkedHashMap)1