canal client中Sql解析
原创 2019-08-22 16:37 阅读(1965)次
在使用阿里canal时同步数据,由于现在的canal-adapter满足不了公司需求,于是我用canal-client自己实现同步,这里需要解析ddl语句,涉及到自己写sql解析有点麻烦,所以找了一下这种组件,能用组件的就用组件,不能的就自己写了,这里分享一下,我用的是阿里的druid,他里面有解析sql的代码,直接拿来用。
maven引入:
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.33</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.1.9</version>
</dependency>
测试代码参考别人的:package parse;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat.*;
import com.alibaba.druid.stat.*;
import com.alibaba.druid.util.JdbcConstants;
public class testparse {
public static void main(String[] args) {
String sql= ""
+ "insert into tar select * from boss_table bo, ("
+ "select a.f1, ff from emp_table a "
+ "inner join log_table b "
+ "on a.f2 = b.f3"
+ ") f "
+ "where bo.f4 = f.f5 "
+ "group by bo.f6 , f.f7 having count(bo.f8) > 0 "
+ "order by bo.f9, f.f10;"
+ "select func(f) from test1; "
+ "";
String dbType = JdbcConstants.POSTGRESQL;
//格式化输出
String result = SQLUtils.format(sql, dbType);
System.out.println(result); // 缺省大写格式
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, dbType);
//解析出的独立语句的个数
System.out.println("size is:" + stmtList.size());
for (int i = 0; i < stmtList.size(); i++) {
SQLStatement stmt = stmtList.get(i);
PGSchemaStatVisitor visitor = new PGSchemaStatVisitor();
stmt.accept(visitor);
Map<String, String> aliasmap = visitor.getAliasMap();
for (Iterator iterator = aliasmap.keySet().iterator(); iterator.hasNext();) {
String key = iterator.next().toString();
System.out.println("[ALIAS]" + key + " - " + aliasmap.get(key));
}
Set<Column> groupby_col = visitor.getGroupByColumns();
//
for (Iterator iterator = groupby_col.iterator(); iterator.hasNext();) {
Column column = (Column) iterator.next();
System.out.println("[GROUP]" + column.toString());
}
//获取表名称
System.out.println("table names:");
Map<Name, TableStat> tabmap = visitor.getTables();
for (Iterator iterator = tabmap.keySet().iterator(); iterator.hasNext();) {
Name name = (Name) iterator.next();
System.out.println(name.toString() + " - " + tabmap.get(name).toString());
}
//System.out.println("Tables : " + visitor.getCurrentTable());
//获取操作方法名称,依赖于表名称
System.out.println("Manipulation : " + visitor.getTables());
//获取字段名称
System.out.println("fields : " + visitor.getColumns());
}
}
}
参考https://www.cnblogs.com/blueglass/p/6286814.html