Search in sources :

Example 16 with ESSyncConfig

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

the class ES7xAdapter method count.

@Override
public Map<String, Object> count(String task) {
    ESSyncConfig config = esSyncConfig.get(task);
    ESSyncConfig.ESMapping mapping = config.getEsMapping();
    SearchResponse response = this.esConnection.new ESSearchRequest(mapping.get_index()).size(0).getResponse();
    long rowCount = response.getHits().getTotalHits().value;
    Map<String, Object> res = new LinkedHashMap<>();
    res.put("esIndex", mapping.get_index());
    res.put("count", rowCount);
    return res;
}
Also used : ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) SearchResponse(org.elasticsearch.action.search.SearchResponse) LinkedHashMap(java.util.LinkedHashMap)

Example 17 with ESSyncConfig

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

the class UserSyncSingleTest method test02.

/**
 * 单表更新
 */
@Test
public void test02() {
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("UPDATE");
    dml.setDatabase("mytest");
    dml.setTable("user");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("name", "Eric2");
    dml.setData(dataList);
    List<Map<String, Object>> oldList = new ArrayList<>();
    Map<String, Object> old = new LinkedHashMap<>();
    oldList.add(old);
    old.put("name", "Eric");
    dml.setOld(oldList);
    String database = dml.getDatabase();
    String table = dml.getTable();
    Map<String, ESSyncConfig> esSyncConfigs = esAdapter.getDbTableEsSyncConfig().get(database + "-" + table);
    esAdapter.getEsSyncService().sync(esSyncConfigs.values(), dml);
    GetResponse response = esAdapter.getEsConnection().getTransportClient().prepareGet("mytest_user", "_doc", "1").get();
    Assert.assertEquals("Eric2", response.getSource().get("_name"));
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GetResponse(org.elasticsearch.action.get.GetResponse) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 18 with ESSyncConfig

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

the class UserSyncSingleTest method test01.

/**
 * 单表插入
 */
@Test
public void test01() {
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("INSERT");
    dml.setDatabase("mytest");
    dml.setTable("user");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("name", "Eric");
    data.put("role_id", 1L);
    data.put("c_time", new Date());
    dml.setData(dataList);
    String database = dml.getDatabase();
    String table = dml.getTable();
    Map<String, ESSyncConfig> esSyncConfigs = esAdapter.getDbTableEsSyncConfig().get(database + "-" + table);
    esAdapter.getEsSyncService().sync(esSyncConfigs.values(), dml);
    GetResponse response = esAdapter.getEsConnection().getTransportClient().prepareGet("mytest_user", "_doc", "1").get();
    Assert.assertEquals("Eric", response.getSource().get("_name"));
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GetResponse(org.elasticsearch.action.get.GetResponse) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 19 with ESSyncConfig

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

the class UserSyncSingleTest method test03.

/**
 * 单表删除
 */
@Test
public void test03() {
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("DELETE");
    dml.setDatabase("mytest");
    dml.setTable("user");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("name", "Eric");
    data.put("role_id", 1L);
    data.put("c_time", new Date());
    dml.setData(dataList);
    String database = dml.getDatabase();
    String table = dml.getTable();
    Map<String, ESSyncConfig> esSyncConfigs = esAdapter.getDbTableEsSyncConfig().get(database + "-" + table);
    esAdapter.getEsSyncService().sync(esSyncConfigs.values(), dml);
    GetResponse response = esAdapter.getEsConnection().getTransportClient().prepareGet("mytest_user", "_doc", "1").get();
    Assert.assertNull(response.getSource());
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) GetResponse(org.elasticsearch.action.get.GetResponse) Date(java.util.Date) LinkedHashMap(java.util.LinkedHashMap) Test(org.junit.Test)

Example 20 with ESSyncConfig

use of com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig 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)

Aggregations

ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)27 LinkedHashMap (java.util.LinkedHashMap)21 Map (java.util.Map)21 ArrayList (java.util.ArrayList)19 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)18 DataSource (javax.sql.DataSource)18 Test (org.junit.Test)18 Date (java.util.Date)17 GetResponse (org.elasticsearch.action.get.GetResponse)17 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)4 SearchResponse (org.elasticsearch.action.search.SearchResponse)4 SchemaItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem)3 ESTemplate (com.alibaba.otter.canal.client.adapter.es.core.support.ESTemplate)3 DatasourceConfig (com.alibaba.otter.canal.client.adapter.support.DatasourceConfig)3 EtlResult (com.alibaba.otter.canal.client.adapter.support.EtlResult)3 List (java.util.List)3 StringUtils (org.apache.commons.lang.StringUtils)3 ESMapping (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig.ESMapping)2 ColumnItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.ColumnItem)2 FieldItem (com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.FieldItem)2