mirror of
https://github.com/alibaba/canal.git
synced 2026-11-04 00:30:15 +08:00
预留sync(MappingConfig config, List<Dml> dmlList)方法
pk hash以old pk值为准若存在
This commit is contained in:
@@ -35,8 +35,6 @@ canal.conf:
|
|||||||
# jdbc.url: jdbc:oracle:thin:@localhost:49161:XE
|
# jdbc.url: jdbc:oracle:thin:@localhost:49161:XE
|
||||||
# jdbc.username: mytest
|
# jdbc.username: mytest
|
||||||
# jdbc.password: m121212
|
# jdbc.password: m121212
|
||||||
# threads: 5
|
|
||||||
# commitSize: 3000
|
|
||||||
# - name: rdb
|
# - name: rdb
|
||||||
# key: postgres1
|
# key: postgres1
|
||||||
# properties:
|
# properties:
|
||||||
@@ -44,7 +42,6 @@ canal.conf:
|
|||||||
# jdbc.url: jdbc:postgresql://localhost:5432/postgres
|
# jdbc.url: jdbc:postgresql://localhost:5432/postgres
|
||||||
# jdbc.username: postgres
|
# jdbc.username: postgres
|
||||||
# jdbc.password: 121212
|
# jdbc.password: 121212
|
||||||
# threads: 5
|
|
||||||
# - name: hbase
|
# - name: hbase
|
||||||
# properties:
|
# properties:
|
||||||
# hbase.zookeeper.quorum: 127.0.0.1
|
# hbase.zookeeper.quorum: 127.0.0.1
|
||||||
|
|||||||
@@ -66,6 +66,9 @@ public class RdbSyncService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void sync(MappingConfig config, List<Dml> dmlList) {
|
||||||
|
}
|
||||||
|
|
||||||
public void sync(MappingConfig config, Dml dml) {
|
public void sync(MappingConfig config, Dml dml) {
|
||||||
try {
|
try {
|
||||||
if (config != null) {
|
if (config != null) {
|
||||||
@@ -175,7 +178,7 @@ public class RdbSyncService {
|
|||||||
|
|
||||||
for (Map<String, Object> o : old) {
|
for (Map<String, Object> o : old) {
|
||||||
Map<String, Object> d = data.get(idx - 1);
|
Map<String, Object> d = data.get(idx - 1);
|
||||||
int pkHash = pkHash(dbMapping, d, threads);
|
int pkHash = pkHash(dbMapping, d, o, threads);
|
||||||
|
|
||||||
ThreadPoolExecutor tpe = (ThreadPoolExecutor) threadExecutors[pkHash];
|
ThreadPoolExecutor tpe = (ThreadPoolExecutor) threadExecutors[pkHash];
|
||||||
checkQueue(tpe);
|
checkQueue(tpe);
|
||||||
@@ -206,7 +209,7 @@ public class RdbSyncService {
|
|||||||
updateSql.delete(len - 2, len).append(" WHERE ");
|
updateSql.delete(len - 2, len).append(" WHERE ");
|
||||||
|
|
||||||
// 拼接主键
|
// 拼接主键
|
||||||
appendCondition(dbMapping, updateSql, ctype, values, d);
|
appendCondition(dbMapping, updateSql, ctype, values, d, o);
|
||||||
|
|
||||||
if (logger.isTraceEnabled()) {
|
if (logger.isTraceEnabled()) {
|
||||||
logger.trace("Update target table, sql: {}", updateSql);
|
logger.trace("Update target table, sql: {}", updateSql);
|
||||||
@@ -470,6 +473,11 @@ public class RdbSyncService {
|
|||||||
*/
|
*/
|
||||||
private static void appendCondition(DbMapping dbMapping, StringBuilder sql, Map<String, Integer> ctype,
|
private static void appendCondition(DbMapping dbMapping, StringBuilder sql, Map<String, Integer> ctype,
|
||||||
List<Map<String, ?>> values, Map<String, Object> d) {
|
List<Map<String, ?>> values, Map<String, Object> d) {
|
||||||
|
appendCondition(dbMapping, sql, ctype, values, d, null);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void appendCondition(DbMapping dbMapping, StringBuilder sql, Map<String, Integer> ctype,
|
||||||
|
List<Map<String, ?>> values, Map<String, Object> d, Map<String, Object> o) {
|
||||||
// 拼接主键
|
// 拼接主键
|
||||||
for (Map.Entry<String, String> entry : dbMapping.getTargetPk().entrySet()) {
|
for (Map.Entry<String, String> entry : dbMapping.getTargetPk().entrySet()) {
|
||||||
String targetColumnName = entry.getKey();
|
String targetColumnName = entry.getKey();
|
||||||
@@ -479,8 +487,13 @@ public class RdbSyncService {
|
|||||||
}
|
}
|
||||||
sql.append(targetColumnName).append("=? AND ");
|
sql.append(targetColumnName).append("=? AND ");
|
||||||
Integer type = ctype.get(targetColumnName.toLowerCase());
|
Integer type = ctype.get(targetColumnName.toLowerCase());
|
||||||
|
// 如果有修改主键的情况
|
||||||
|
if (o != null && o.containsKey(srcColumnName)) {
|
||||||
|
BatchExecutor.setValue(values, type, o.get(srcColumnName));
|
||||||
|
} else {
|
||||||
BatchExecutor.setValue(values, type, d.get(srcColumnName));
|
BatchExecutor.setValue(values, type, d.get(srcColumnName));
|
||||||
}
|
}
|
||||||
|
}
|
||||||
int len = sql.length();
|
int len = sql.length();
|
||||||
sql.delete(len - 4, len);
|
sql.delete(len - 4, len);
|
||||||
}
|
}
|
||||||
@@ -489,6 +502,10 @@ public class RdbSyncService {
|
|||||||
* 取主键hash
|
* 取主键hash
|
||||||
*/
|
*/
|
||||||
private static int pkHash(DbMapping dbMapping, Map<String, Object> d, int threads) {
|
private static int pkHash(DbMapping dbMapping, Map<String, Object> d, int threads) {
|
||||||
|
return pkHash(dbMapping, d, null, threads);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static int pkHash(DbMapping dbMapping, Map<String, Object> d, Map<String, Object> o, int threads) {
|
||||||
int hash = 0;
|
int hash = 0;
|
||||||
// 取主键
|
// 取主键
|
||||||
for (Map.Entry<String, String> entry : dbMapping.getTargetPk().entrySet()) {
|
for (Map.Entry<String, String> entry : dbMapping.getTargetPk().entrySet()) {
|
||||||
@@ -497,7 +514,12 @@ public class RdbSyncService {
|
|||||||
if (srcColumnName == null) {
|
if (srcColumnName == null) {
|
||||||
srcColumnName = targetColumnName;
|
srcColumnName = targetColumnName;
|
||||||
}
|
}
|
||||||
Object value = d.get(srcColumnName);
|
Object value;
|
||||||
|
if (o != null && o.containsKey(srcColumnName)) {
|
||||||
|
value = o.get(srcColumnName);
|
||||||
|
} else {
|
||||||
|
value = d.get(srcColumnName);
|
||||||
|
}
|
||||||
if (value != null) {
|
if (value != null) {
|
||||||
hash += value.hashCode();
|
hash += value.hashCode();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -171,8 +171,9 @@ public class FlatMessage implements Serializable {
|
|||||||
try {
|
try {
|
||||||
rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
|
rowChange = CanalEntry.RowChange.parseFrom(entry.getStoreValue());
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
throw new RuntimeException("ERROR ## parser of eromanga-event has an error , data:"
|
throw new RuntimeException(
|
||||||
+ entry.toString(), e);
|
"ERROR ## parser of eromanga-event has an error , data:" + entry.toString(),
|
||||||
|
e);
|
||||||
}
|
}
|
||||||
|
|
||||||
CanalEntry.EventType eventType = rowChange.getEventType();
|
CanalEntry.EventType eventType = rowChange.getEventType();
|
||||||
@@ -284,7 +285,14 @@ public class FlatMessage implements Serializable {
|
|||||||
if (flatMessage.getData() != null) {
|
if (flatMessage.getData() != null) {
|
||||||
int idx = 0;
|
int idx = 0;
|
||||||
for (Map<String, String> row : flatMessage.getData()) {
|
for (Map<String, String> row : flatMessage.getData()) {
|
||||||
String value = row.get(pk);
|
Map<String, String> o = flatMessage.getOld().get(idx);
|
||||||
|
String value;
|
||||||
|
// 如果old中有pk值说明主键有修改, 以旧的主键值hash为准
|
||||||
|
if (o != null && o.containsKey(pk)) {
|
||||||
|
value = o.get(pk);
|
||||||
|
} else {
|
||||||
|
value = row.get(pk);
|
||||||
|
}
|
||||||
if (value == null) {
|
if (value == null) {
|
||||||
value = "";
|
value = "";
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user