Skip to content

Commit 0178118

Browse files
cooperlytzoemak
authored andcommitted
Fix Configuration property name '-id' is not valid on canal 1.1.7 alpha 3 alibaba#4879 (alibaba#4887)
* Fix Configuration property name '-id' is not valid * Fix Configuration property name '-id' is not valid 读取原配置文件
1 parent 7cf5ef9 commit 0178118

File tree

15 files changed

+198
-171
lines changed

15 files changed

+198
-171
lines changed

client-adapter/common/pom.xml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,11 @@
5454
<artifactId>junit</artifactId>
5555
<scope>test</scope>
5656
</dependency>
57+
<dependency>
58+
<groupId>org.yaml</groupId>
59+
<artifactId>snakeyaml</artifactId>
60+
<scope>test</scope>
61+
</dependency>
5762
<dependency>
5863
<groupId>com.diffblue</groupId>
5964
<artifactId>deeptestutils</artifactId>

client-adapter/common/src/test/java/com/alibaba/otter/canal/client/adapter/support/YamlUtilsTest.java

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
package com.alibaba.otter.canal.client.adapter.support;
22

3+
import org.junit.Assert;
4+
import org.junit.Test;
5+
import org.springframework.beans.factory.annotation.Value;
6+
37
import java.util.LinkedHashMap;
48
import java.util.Map;
59
import java.util.Properties;
610

7-
import org.junit.Assert;
8-
import org.junit.Test;
9-
1011
public class YamlUtilsTest {
1112

13+
14+
1215
@Test
1316
public void testLoadConfigToYml() {
1417
String configStr="dataSourceKey: defaultDS\n"
@@ -17,6 +20,7 @@ public void testLoadConfigToYml() {
1720
+ "outerAdapterKey: mysql1\n"
1821
+ "concurrent: true\n"
1922
+ "dbMapping:\n"
23+
+ " _id: _id\n"
2024
+ " database: mytest\n"
2125
+ " table: user\n"
2226
+ " targetTable: mytest2.user\n"
@@ -35,6 +39,7 @@ public void testLoadConfigToYml() {
3539
MappingConfig config = YamlUtils.ymlToObj(null, configStr, MappingConfig.class, null, new Properties());
3640

3741
Assert.assertNotNull(config);
42+
Assert.assertEquals(config.getDbMapping().getId(), "_id");
3843
Assert.assertEquals(config.getDestination(), "example");
3944
Assert.assertEquals(config.getOuterAdapterKey(), "mysql1");
4045
Assert.assertEquals(config.getDbMapping().getDatabase(), "mytest");
@@ -104,6 +109,9 @@ public void setDbMapping(DbMapping dbMapping) {
104109
}
105110

106111
private static class DbMapping {
112+
113+
@Value("${_id}")
114+
private String id ;
107115
private boolean mirrorDb = false; // 是否镜像库
108116
private String database; // 数据库名或schema名
109117
private String table; // 表名
@@ -225,5 +233,13 @@ public Map<String, String> getAllMapColumns() {
225233
public void setAllMapColumns(Map<String, String> allMapColumns) {
226234
this.allMapColumns = allMapColumns;
227235
}
236+
237+
public String getId() {
238+
return id;
239+
}
240+
241+
public void setId(String id) {
242+
this.id = id;
243+
}
228244
}
229245
}

client-adapter/es6x/src/main/java/com/alibaba/otter/canal/client/adapter/es6x/ES6xAdapter.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,12 @@ public void init(OuterAdapterConfig configuration, Properties envProperties) {
5959
public Map<String, Object> count(String task) {
6060
ESSyncConfig config = esSyncConfig.get(task);
6161
ESSyncConfig.ESMapping mapping = config.getEsMapping();
62-
SearchResponse response = this.esConnection.new ESSearchRequest(mapping.get_index(), mapping.get_type()).size(0)
62+
SearchResponse response = this.esConnection.new ESSearchRequest(mapping.getIndex(), mapping.getType()).size(0)
6363
.getResponse();
6464

6565
long rowCount = response.getHits().getTotalHits();
6666
Map<String, Object> res = new LinkedHashMap<>();
67-
res.put("esIndex", mapping.get_index());
67+
res.put("esIndex", mapping.getIndex());
6868
res.put("count", rowCount);
6969
return res;
7070
}

client-adapter/es6x/src/main/java/com/alibaba/otter/canal/client/adapter/es6x/etl/ESEtlService.java

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ public ESEtlService(ESConnection esConnection, ESSyncConfig config){
5151

5252
public EtlResult importData(List<String> params) {
5353
ESMapping mapping = config.getEsMapping();
54-
logger.info("start etl to import data to index: {}", mapping.get_index());
54+
logger.info("start etl to import data to index: {}", mapping.getIndex());
5555
String sql = mapping.getSql();
5656
return importData(sql, params);
5757
}
@@ -78,7 +78,7 @@ protected boolean executeSqlImport(DataSource ds, String sql, List<Object> value
7878
}
7979

8080
// 如果是主键字段则不插入
81-
if (fieldItem.getFieldName().equals(mapping.get_id())) {
81+
if (fieldItem.getFieldName().equals(mapping.getId())) {
8282
idVal = esTemplate.getValFromRS(mapping, rs, fieldName, fieldName);
8383
} else {
8484
Object val = esTemplate.getValFromRS(mapping, rs, fieldName, fieldName);
@@ -117,8 +117,8 @@ protected boolean executeSqlImport(DataSource ds, String sql, List<Object> value
117117
if (idVal != null) {
118118
String parentVal = (String) esFieldData.remove("$parent_routing");
119119
if (mapping.isUpsert()) {
120-
ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.get_index(),
121-
mapping.get_type(),
120+
ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.getIndex(),
121+
mapping.getType(),
122122
idVal.toString()).setDoc(esFieldData).setDocAsUpsert(true);
123123

124124
if (StringUtils.isNotEmpty(parentVal)) {
@@ -127,8 +127,8 @@ protected boolean executeSqlImport(DataSource ds, String sql, List<Object> value
127127

128128
esBulkRequest.add(esUpdateRequest);
129129
} else {
130-
ESIndexRequest esIndexRequest = this.esConnection.new ES6xIndexRequest(mapping.get_index(),
131-
mapping.get_type(),
130+
ESIndexRequest esIndexRequest = this.esConnection.new ES6xIndexRequest(mapping.getIndex(),
131+
mapping.getType(),
132132
idVal.toString()).setSource(esFieldData);
133133
if (StringUtils.isNotEmpty(parentVal)) {
134134
esIndexRequest.setRouting(parentVal);
@@ -137,12 +137,12 @@ protected boolean executeSqlImport(DataSource ds, String sql, List<Object> value
137137
}
138138
} else {
139139
idVal = esFieldData.get(mapping.getPk());
140-
ESSearchRequest esSearchRequest = this.esConnection.new ESSearchRequest(mapping.get_index(),
141-
mapping.get_type()).setQuery(QueryBuilders.termQuery(mapping.getPk(), idVal)).size(10000);
140+
ESSearchRequest esSearchRequest = this.esConnection.new ESSearchRequest(mapping.getIndex(),
141+
mapping.getType()).setQuery(QueryBuilders.termQuery(mapping.getPk(), idVal)).size(10000);
142142
SearchResponse response = esSearchRequest.getResponse();
143143
for (SearchHit hit : response.getHits()) {
144-
ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.get_index(),
145-
mapping.get_type(),
144+
ESUpdateRequest esUpdateRequest = this.esConnection.new ES6xUpdateRequest(mapping.getIndex(),
145+
mapping.getType(),
146146
hit.getId()).setDoc(esFieldData);
147147
esBulkRequest.add(esUpdateRequest);
148148
}
@@ -160,7 +160,7 @@ protected boolean executeSqlImport(DataSource ds, String sql, List<Object> value
160160
(System.currentTimeMillis() - batchBegin),
161161
(System.currentTimeMillis() - esBatchBegin),
162162
esBulkRequest.numberOfActions(),
163-
mapping.get_index());
163+
mapping.getIndex());
164164
}
165165
batchBegin = System.currentTimeMillis();
166166
esBulkRequest.resetBulk();
@@ -180,12 +180,12 @@ protected boolean executeSqlImport(DataSource ds, String sql, List<Object> value
180180
(System.currentTimeMillis() - batchBegin),
181181
(System.currentTimeMillis() - esBatchBegin),
182182
esBulkRequest.numberOfActions(),
183-
mapping.get_index());
183+
mapping.getIndex());
184184
}
185185
}
186186
} catch (Exception e) {
187187
logger.error(e.getMessage(), e);
188-
errMsg.add(mapping.get_index() + " etl failed! ==>" + e.getMessage());
188+
errMsg.add(mapping.getIndex() + " etl failed! ==>" + e.getMessage());
189189
throw new RuntimeException(e);
190190
}
191191
return count;

0 commit comments

Comments
 (0)