mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
新增完成
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
package com.alibaba.otter.canal.client.adapter.es.test.sync;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.otter.canal.client.adapter.es.ESAdapter;
|
||||
import com.alibaba.otter.canal.client.adapter.support.AdapterConfigs;
|
||||
import com.alibaba.otter.canal.client.adapter.support.DatasourceConfig;
|
||||
import com.alibaba.otter.canal.client.adapter.support.Dml;
|
||||
|
||||
public class RoleSyncJoinOne2Test {
|
||||
|
||||
private ESAdapter esAdapter;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
AdapterConfigs.put("es", "mytest_user_join_one2.yml");
|
||||
esAdapter = Common.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 非子查询从表插入 (确保主表记录必须有数据)
|
||||
*/
|
||||
@Test
|
||||
public void insertTest01() {
|
||||
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", "admin2");
|
||||
|
||||
dml.setData(dataList);
|
||||
|
||||
esAdapter.getEsSyncService().sync(dml);
|
||||
|
||||
GetResponse response = esAdapter.getTransportClient().prepareGet("mytest_user", "_doc", "1").get();
|
||||
Assert.assertEquals("admin2_", response.getSource().get("_role_name"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
esAdapter.destroy();
|
||||
DatasourceConfig.DATA_SOURCES.values().forEach(DruidDataSource::close);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
package com.alibaba.otter.canal.client.adapter.es.test.sync;
|
||||
|
||||
import java.util.*;
|
||||
|
||||
import org.elasticsearch.action.get.GetResponse;
|
||||
import org.junit.After;
|
||||
import org.junit.Assert;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.otter.canal.client.adapter.es.ESAdapter;
|
||||
import com.alibaba.otter.canal.client.adapter.support.AdapterConfigs;
|
||||
import com.alibaba.otter.canal.client.adapter.support.DatasourceConfig;
|
||||
import com.alibaba.otter.canal.client.adapter.support.Dml;
|
||||
|
||||
public class RoleSyncJoinSub2Test {
|
||||
|
||||
private ESAdapter esAdapter;
|
||||
|
||||
@Before
|
||||
public void init() {
|
||||
AdapterConfigs.put("es", "mytest_user_join_sub2.yml");
|
||||
esAdapter = Common.init();
|
||||
}
|
||||
|
||||
/**
|
||||
* 子查询从表插入 (确保主表记录必须有数据)
|
||||
*/
|
||||
@Test
|
||||
public void insertTest01() {
|
||||
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", 1L);
|
||||
data.put("user_id",1L);
|
||||
data.put("label", "a");
|
||||
|
||||
dml.setData(dataList);
|
||||
|
||||
esAdapter.getEsSyncService().sync(dml);
|
||||
|
||||
GetResponse response = esAdapter.getTransportClient().prepareGet("mytest_user", "_doc", "1").get();
|
||||
Assert.assertEquals("a;b_", response.getSource().get("_labels"));
|
||||
}
|
||||
|
||||
@After
|
||||
public void after() {
|
||||
esAdapter.destroy();
|
||||
DatasourceConfig.DATA_SOURCES.values().forEach(DruidDataSource::close);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
dataSourceKey: defaultDS
|
||||
destination: example
|
||||
esMapping:
|
||||
_index: mytest_user
|
||||
_type: _doc
|
||||
_id: _id
|
||||
sql: "select a.id as _id, concat(a.name,'_') as _name, a.role_id as _role_id,
|
||||
concat(b.role_name,'_') as _role_name, a.c_time as _c_time from user a
|
||||
left join role b on b.id=a.role_id"
|
||||
commitBatch: 3000
|
||||
@@ -0,0 +1,11 @@
|
||||
dataSourceKey: defaultDS
|
||||
destination: example
|
||||
esMapping:
|
||||
_index: mytest_user
|
||||
_type: _doc
|
||||
_id: _id
|
||||
sql: "select a.id as _id, concat(a.name,'_') as _name, a.role_id as _role_id,
|
||||
concat(b.labels, '_') as _labels, a.c_time as _c_time from user a
|
||||
left join (select user_id, group_concat(label separator ';') as labels from label
|
||||
group by user_id) b on b.user_id=a.id"
|
||||
commitBatch: 3000
|
||||
Reference in New Issue
Block a user