编写Java代码,使用druid 1.2.5实现mysql、clickhouse、ElsticSearch的sql解析,实现血缘关系分析,输出 字段-->来源表.来源字段名
时间: 2024-02-27 10:54:56 浏览: 302
Java通过Druid连接mysql
好的,以下是Java代码示例:
```java
import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.sql.ast.SQLExpr;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.clickhouse.parser.ClickhouseStatementParser;
import com.alibaba.druid.sql.dialect.elasticjob.parser.ElasticJobStatementParser;
import com.alibaba.druid.sql.dialect.mysql.parser.MySqlStatementParser;
import com.alibaba.druid.sql.dialect.mysql.visitor.MySqlSchemaStatVisitor;
import com.alibaba.druid.sql.dialect.postgresql.visitor.PGSchemaStatVisitor;
import com.alibaba.druid.sql.dialect.sqlserver.visitor.SQLServerSchemaStatVisitor;
import com.alibaba.druid.stat.TableStat;
import com.alibaba.druid.util.JdbcConstants;
import java.util.List;
import java.util.Map;
public class SqlParserDemo {
public static void main(String[] args) {
String mysqlSql = "SELECT t1.id, t2.name FROM table1 t1, table2 t2 WHERE t1.id = t2.id";
String clickhouseSql = "SELECT id, name FROM table1";
String elasticSearchSql = "SELECT id, title FROM index1 WHERE content LIKE '%keyword%'";
// 解析mysql sql
List<SQLStatement> mysqlStatements = SQLUtils.parseStatements(mysqlSql, JdbcConstants.MYSQL);
MySqlSchemaStatVisitor mysqlVisitor = new MySqlSchemaStatVisitor();
for (SQLStatement mysqlStatement : mysqlStatements) {
mysqlStatement.accept(mysqlVisitor);
}
// 解析clickhouse sql
List<SQLStatement> clickhouseStatements = SQLUtils.parseStatements(clickhouseSql, JdbcConstants.CLICKHOUSE);
ClickhouseStatementParser clickhouseParser = new ClickhouseStatementParser(clickhouseSql);
SQLStatement clickhouseStatement = clickhouseParser.parseStatement();
PGSchemaStatVisitor clickhouseVisitor = new PGSchemaStatVisitor();
clickhouseStatement.accept(clickhouseVisitor);
// 解析elastic search sql
List<SQLStatement> elasticSearchStatements = SQLUtils.parseStatements(elasticSearchSql, JdbcConstants.ELASTIC_SEARCH);
ElasticJobStatementParser esParser = new ElasticJobStatementParser(elasticSearchSql);
SQLStatement esStatement = esParser.parseStatement();
SQLServerSchemaStatVisitor esVisitor = new SQLServerSchemaStatVisitor();
esStatement.accept(esVisitor);
// 输出字段来源表和字段名称
Map<TableStat.Name, TableStat> mysqlTableMap = mysqlVisitor.getTables();
for (Map.Entry<TableStat.Name, TableStat> entry : mysqlTableMap.entrySet()) {
TableStat.Name tableName = entry.getKey();
TableStat tableStat = entry.getValue();
Map<String, TableStat.Column> columnMap = tableStat.getColumns();
for (Map.Entry<String, TableStat.Column> columnEntry : columnMap.entrySet()) {
String columnName = columnEntry.getKey();
TableStat.Column columnStat = columnEntry.getValue();
List<SQLExpr> exprList = columnStat.getExprList();
for (SQLExpr expr : exprList) {
System.out.println(columnName + "-->" + tableName.getName() + "." + expr.toString());
}
}
}
Map<TableStat.Name, TableStat> clickhouseTableMap = clickhouseVisitor.getTables();
for (Map.Entry<TableStat.Name, TableStat> entry : clickhouseTableMap.entrySet()) {
TableStat.Name tableName = entry.getKey();
TableStat tableStat = entry.getValue();
Map<String, TableStat.Column> columnMap = tableStat.getColumns();
for (Map.Entry<String, TableStat.Column> columnEntry : columnMap.entrySet()) {
String columnName = columnEntry.getKey();
TableStat.Column columnStat = columnEntry.getValue();
List<SQLExpr> exprList = columnStat.getExprList();
for (SQLExpr expr : exprList) {
System.out.println(columnName + "-->" + tableName.getName() + "." + expr.toString());
}
}
}
Map<TableStat.Name, TableStat> esTableMap = esVisitor.getTables();
for (Map.Entry<TableStat.Name, TableStat> entry : esTableMap.entrySet()) {
TableStat.Name tableName = entry.getKey();
TableStat tableStat = entry.getValue();
Map<String, TableStat.Column> columnMap = tableStat.getColumns();
for (Map.Entry<String, TableStat.Column> columnEntry : columnMap.entrySet()) {
String columnName = columnEntry.getKey();
TableStat.Column columnStat = columnEntry.getValue();
List<SQLExpr> exprList = columnStat.getExprList();
for (SQLExpr expr : exprList) {
System.out.println(columnName + "-->" + tableName.getName() + "." + expr.toString());
}
}
}
}
}
```
以上代码实现了使用Druid解析MySQL、Clickhouse、ElasticSearch的SQL语句,并输出字段来源表和字段名称的血缘关系信息。注意,代码中需要引入Druid的相关依赖,以及各数据库的驱动包。
阅读全文