Search in sources :

Example 76 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project canal by alibaba.

the class LabelSyncJoinSubTest method test01.

/**
 * 子查询从表插入
 */
@Test
public void test01() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "delete from label where id=1 or id=2");
    Common.sqlExe(ds, "insert into label (id,user_id,label) values (1,1,'a')");
    Common.sqlExe(ds, "insert into label (id,user_id,label) values (2,1,'b')");
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("INSERT");
    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", 2L);
    data.put("user_id", 1L);
    data.put("label", "b");
    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;a", 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 77 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project canal by alibaba.

the class RoleSyncJoinOneTest method test01.

/**
 * 非子查询从表插入
 */
@Test
public void test01() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "delete from role where id=1");
    Common.sqlExe(ds, "insert into role (id,role_name) values (1,'admin')");
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("INSERT");
    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", "admin");
    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("admin", 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 78 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project canal by alibaba.

the class RoleSyncJoinOneTest method test04.

/**
 * 非子查询从表删除
 */
@Test
public void test04() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "delete from role where id=1");
    Dml dml = new Dml();
    dml.setDestination("example");
    dml.setTs(new Date().getTime());
    dml.setType("DELETE");
    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", "admin");
    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().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 79 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project canal by alibaba.

the class RoleSyncJoinOneTest method test02.

/**
 * 非子查询从表更新
 */
@Test
public void test02() {
    DataSource ds = DatasourceConfig.DATA_SOURCES.get("defaultDS");
    Common.sqlExe(ds, "update role set role_name='admin2' 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", "admin2");
    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("admin2", 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 80 with GetResponse

use of org.graylog.shaded.elasticsearch7.org.elasticsearch.action.get.GetResponse in project canal by alibaba.

the class LabelSyncJoinSubTest 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

GetResponse (org.elasticsearch.action.get.GetResponse)167 Test (org.junit.Test)50 GetRequest (org.elasticsearch.action.get.GetRequest)28 Map (java.util.Map)27 MultiGetResponse (org.elasticsearch.action.get.MultiGetResponse)26 ArrayList (java.util.ArrayList)23 HashMap (java.util.HashMap)21 Date (java.util.Date)18 ESSyncConfig (com.alibaba.otter.canal.client.adapter.es.core.config.ESSyncConfig)17 Dml (com.alibaba.otter.canal.client.adapter.support.Dml)17 LinkedHashMap (java.util.LinkedHashMap)17 IOException (java.io.IOException)16 DataSource (javax.sql.DataSource)14 GetRequestBuilder (org.elasticsearch.action.get.GetRequestBuilder)14 SearchResponse (org.elasticsearch.action.search.SearchResponse)14 DeleteResponse (org.elasticsearch.action.delete.DeleteResponse)12 ElasticsearchException (org.elasticsearch.ElasticsearchException)10 Settings (org.elasticsearch.common.settings.Settings)10 Alias (org.elasticsearch.action.admin.indices.alias.Alias)7 IndexResponse (org.elasticsearch.action.index.IndexResponse)7