Search in sources :

Example 21 with ESSyncConfig

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

the class RoleSyncJoinOneTest method test03.

/**
 * 主表更新外键值
 */
@Test
public void test03() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "delete from role where id=2");
    Common.sqlExe(ds, "insert into role (id,role_name) values (2,'operator')");
    Common.sqlExe(ds, "update user set role_id=2 where id=1");
    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("role_id", 2L);
    dml.setData(dataList);
    List<Map<String, Object>> oldList = new ArrayList<>();
    Map<String, Object> old = new LinkedHashMap<>();
    oldList.add(old);
    old.put("role_id", 1L);
    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("operator", response.getSource().get("_role_name"));
    Common.sqlExe(ds, "update user set role_id=1 where id=1");
    Dml dml2 = new Dml();
    dml2.setDestination("example");
    dml2.setTs(new Date().getTime());
    dml2.setType("UPDATE");
    dml2.setDatabase("mytest");
    dml2.setTable("user");
    List<Map<String, Object>> dataList2 = new ArrayList<>();
    Map<String, Object> data2 = new LinkedHashMap<>();
    dataList2.add(data2);
    data2.put("id", 1L);
    data2.put("role_id", 1L);
    dml2.setData(dataList2);
    List<Map<String, Object>> oldList2 = new ArrayList<>();
    Map<String, Object> old2 = new LinkedHashMap<>();
    oldList2.add(old2);
    old2.put("role_id", 2L);
    dml2.setOld(oldList2);
    esAdapter.getEsSyncService().sync(esSyncConfigs.values(), dml2);
    GetResponse response2 = esAdapter.getEsConnection().getTransportClient().prepareGet("mytest_user", "_doc", "1").get();
    Assert.assertEquals("admin2", response2.getSource().get("_role_name"));
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) ArrayList(java.util.ArrayList) GetResponse(org.elasticsearch.action.get.GetResponse) Date(java.util.Date) DataSource(javax.sql.DataSource) LinkedHashMap(java.util.LinkedHashMap) ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 22 with ESSyncConfig

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

the class LabelSyncJoinSub2Test method test03.

/**
 * 带函数子查询从表删除
 */
@Test
public void test03() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "delete from label where id=1");
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("DELETE");
    dml.setDatabase("mytest");
    dml.setTable("label");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("user_id", 1L);
    data.put("label", "a");
    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("b_", response.getSource().get("_labels"));
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) ArrayList(java.util.ArrayList) GetResponse(org.elasticsearch.action.get.GetResponse) Date(java.util.Date) DataSource(javax.sql.DataSource) LinkedHashMap(java.util.LinkedHashMap) ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 23 with ESSyncConfig

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

the class RoleSyncJoinOne2Test method test02.

/**
 * 带函数非子查询从表更新
 */
@Test
public void test02() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "update role set role_name='admin3' where id=1");
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("UPDATE");
    dml.setDatabase("mytest");
    dml.setTable("role");
    List<Map<String, Object>> dataList = new ArrayList<>();
    Map<String, Object> data = new LinkedHashMap<>();
    dataList.add(data);
    data.put("id", 1L);
    data.put("role_name", "admin3");
    dml.setData(dataList);
    List<Map<String, Object>> oldList = new ArrayList<>();
    Map<String, Object> old = new LinkedHashMap<>();
    oldList.add(old);
    old.put("role_name", "admin");
    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("admin3_", response.getSource().get("_role_name"));
}
Also used : Dml(com.alibaba.otter.canal.client.adapter.support.Dml) ArrayList(java.util.ArrayList) GetResponse(org.elasticsearch.action.get.GetResponse) Date(java.util.Date) DataSource(javax.sql.DataSource) LinkedHashMap(java.util.LinkedHashMap) ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 24 with ESSyncConfig

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

the class UserSyncJoinOneTest method test02.

/**
 * 主表带函数更新
 */
@Test
public void test02() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "update user set name='Eric2' where id=1");
    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) ArrayList(java.util.ArrayList) GetResponse(org.elasticsearch.action.get.GetResponse) Date(java.util.Date) DataSource(javax.sql.DataSource) LinkedHashMap(java.util.LinkedHashMap) ESSyncConfig(com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) Test(org.junit.Test)

Example 25 with ESSyncConfig

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

the class ES6xTemplate 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) MappingMetaData(org.elasticsearch.cluster.metadata.MappingMetaData) FieldItem(com.alibaba.otter.canal.client.adapter.es.core.config.SchemaItem.FieldItem) ESSearchRequest(com.alibaba.otter.canal.client.adapter.es6x.support.ESConnection.ESSearchRequest) 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