Browse Source

canal集成

4.0
yaoq 2 years ago
parent
commit
12dc05d38b
3 changed files with 50 additions and 7 deletions
  1. +17
    -1
      smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/common/AbstractFileWork.java
  2. +27
    -1
      smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/impl/KbSqlFileWork.java
  3. +6
    -5
      smtweb-framework/canal/file/src/main/resources/config/application.yaml

+ 17
- 1
smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/common/AbstractFileWork.java View File

@@ -18,6 +18,7 @@ import java.util.*;
@Component @Component
public abstract class AbstractFileWork { public abstract class AbstractFileWork {
private final static String suffix = ".cf"; private final static String suffix = ".cf";
protected final static String idField = "idField";


protected FileDecodeConfig fileConfig; protected FileDecodeConfig fileConfig;


@@ -136,7 +137,7 @@ public abstract class AbstractFileWork {
sql.append("?").append(","); sql.append("?").append(",");
String value = data.get(field); String value = data.get(field);
if (StringUtils.isEmpty(value)) { if (StringUtils.isEmpty(value)) {
value = null;
args.add(null);
} else { } else {
args.add(value); args.add(value);
} }
@@ -170,4 +171,19 @@ public abstract class AbstractFileWork {
protected void doDeleteSql(CanalVO canalVO) { protected void doDeleteSql(CanalVO canalVO) {
getDbEngine().update("delete from " + canalVO.getTableName() + " where " + canalVO.getIdField() + " = ? ", canalVO.getId()); getDbEngine().update("delete from " + canalVO.getTableName() + " where " + canalVO.getIdField() + " = ? ", canalVO.getId());
} }

protected Map<String, String> decodeCreateSql(String sql) {
Map<String, String> map = new HashMap<>();
String data = sql.substring(sql.indexOf("(") + 1, sql.lastIndexOf(")")).trim().replaceAll("`", "");
String[] rows = data.split(",");
for (String row : rows) {
if (row.contains("PRIMARY KEY")) {
map.put(idField, row.substring(row.indexOf("(") + 1, row.lastIndexOf(")")));
continue;
}
String[] fields = row.trim().split(" ");
map.put(fields[0], fields[1]);
}
return map;
}
} }

+ 27
- 1
smtweb-framework/canal/file/src/main/java/cc/smtweb/system/canal/file/impl/KbSqlFileWork.java View File

@@ -3,12 +3,16 @@ package cc.smtweb.system.canal.file.impl;
import cc.smtweb.framework.core.common.CanalVO; import cc.smtweb.framework.core.common.CanalVO;
import cc.smtweb.framework.core.common.SwEnum; import cc.smtweb.framework.core.common.SwEnum;
import cc.smtweb.system.canal.file.common.AbstractFileWork; import cc.smtweb.system.canal.file.common.AbstractFileWork;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;


/** /**
* @Author yaoq * @Author yaoq
* @Date 2022年09月20日 10:29 * @Date 2022年09月20日 10:29
* @Description * @Description
*/ */
@Slf4j
public class KbSqlFileWork extends AbstractFileWork { public class KbSqlFileWork extends AbstractFileWork {


@Override @Override
@@ -18,11 +22,33 @@ public class KbSqlFileWork extends AbstractFileWork {


@Override @Override
protected void doCreateSql(CanalVO canalVO) { protected void doCreateSql(CanalVO canalVO) {

Map<String, String> sqlMap = decodeCreateSql(canalVO.getSql());
StringBuilder sql = new StringBuilder();
sql.append("create table public.").append(canalVO.getTableName()).append(" ( ");
sqlMap.forEach((k, v) -> {
if (idField.equals(k)) return;
sql.append(k).append(" ").append(convertFieldType(v));
if (k.equals(sqlMap.get(idField))) {
sql.append(" ").append("NOT NULL DEFAULT -1");
} else {
sql.append(" ").append("NULL");
}
sql.append(",");
});
sql.append(" CONSTRAINT pk_").append(canalVO.getTableName()).append(" PRIMARY KEY (").append(sqlMap.get(idField)).append(") ENABLE VALIDATE ) ");
log.debug("sql:::" + sql);
getDbEngine().update(sql.toString());
} }


@Override @Override
protected void doAlterSql(CanalVO canalVO) { protected void doAlterSql(CanalVO canalVO) {


} }


private String convertFieldType(String type) {
if (type.contains("bigint")) return "bigint";
if (type.contains("tinyint")) return "tinyint";
return type;
}
} }

+ 6
- 5
smtweb-framework/canal/file/src/main/resources/config/application.yaml View File

@@ -6,7 +6,7 @@ smtweb:
debug: true debug: true
code-java-path: 'd:/work/smtweb2/smtweb-framework/canal/file' code-java-path: 'd:/work/smtweb2/smtweb-framework/canal/file'
db: db:
type: mysql
type: kbsql
default: default:
rule: rule:
prefix: _smt_ prefix: _smt_
@@ -33,11 +33,12 @@ spring:
port: 6379 port: 6379
password: password:
datasource: datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
driver-class-name: com.kingbase8.Driver
# url: jdbc:mysql://139.9.38.43:6032/smt?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false # url: jdbc:mysql://139.9.38.43:6032/smt?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
url: jdbc:mysql://127.0.0.1:3306/tzrs?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
username: root
password: root
# url: jdbc:mysql://127.0.0.1:3306/tzrs?useUnicode=true&characterEncoding=utf-8&allowMultiQueries=true&useSSL=false
url: jdbc:kingbase8://172.28.123.205:54321/HLJTY?useUnicode=true&amp;characterEncoding=utf-8
username: system
password: system
servlet: servlet:
multipart: multipart:
max-file-size: 104857600000 max-file-size: 104857600000


Loading…
Cancel
Save