Search in sources :

Example 16 with Dml

use of com.alibaba.otter.canal.client.adapter.support.Dml 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 17 with Dml

use of com.alibaba.otter.canal.client.adapter.support.Dml 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 18 with Dml

use of com.alibaba.otter.canal.client.adapter.support.Dml 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 19 with Dml

use of com.alibaba.otter.canal.client.adapter.support.Dml 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 20 with Dml

use of com.alibaba.otter.canal.client.adapter.support.Dml 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)

Aggregations

Dml (com.alibaba.otter.canal.client.adapter.support.Dml)28 ArrayList (java.util.ArrayList)24 Test (org.junit.Test)22 LinkedHashMap (java.util.LinkedHashMap)21 Map (java.util.Map)21 Date (java.util.Date)20 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)17 GetResponse (org.elasticsearch.action.get.GetResponse)17 DataSource (javax.sql.DataSource)15 StringUtils (org.apache.commons.lang.StringUtils)3 Logger (org.slf4j.Logger)3 LoggerFactory (org.slf4j.LoggerFactory)3 JSON (com.alibaba.fastjson.JSON)2 SerializerFeature (com.alibaba.fastjson.serializer.SerializerFeature)2 MappingConfig (com.alibaba.otter.canal.client.adapter.rdb.config.MappingConfig)2 SingleDml (com.alibaba.otter.canal.client.adapter.rdb.support.SingleDml)2 Util (com.alibaba.otter.canal.client.adapter.support.Util)2 SQLUtils (com.alibaba.druid.sql.SQLUtils)1 SQLName (com.alibaba.druid.sql.ast.SQLName)1 SQLStatement (com.alibaba.druid.sql.ast.SQLStatement)1